aboutsummaryrefslogtreecommitdiff
path: root/challenge-141/abigail/pascal/ch-2.p
blob: e2919ebbf50ee2667193a78ad708da935b6c4bb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Program XXX;

(*                  *)
(* See ../README.md *)
(*                  *)

(*                                                        *)
(* Run as: fpc -och-2.out ch-2.p; ./ch-2.out < input-file *)
(*                                                        *)

uses
    sysutils;

function substrings (n: string; m, prefix, max: integer): integer;
    var
        fc, n_prefix: integer;
        tail: string;

    begin
        if length (n) = 0 then begin
            if (prefix > -1)  and
               (prefix < max) and
               (prefix mod m = 0) then begin
                substrings := 1;
            end
            else begin
                substrings := 0;
            end;
        end
        else begin
            fc := strtoint (copy (n, 1, 1));
            if prefix = -1 then begin
                n_prefix := fc;
            end
            else begin
                n_prefix := 10 * prefix + fc;
            end;

            tail := copy (n, 2, length (n));

            substrings := substrings (tail, m, n_prefix, max) +
                          substrings (tail, m,   prefix, max);
        end
    end;

var
    n, m: Integer;

begin
    while not eof do begin
        readln (n, m);
        writeln (substrings (inttostr (n), m, -1, n));
    end;
end.