Cod sursa(job #13658)

Utilizator gurneySachelarie Bogdan gurney Data 7 februarie 2007 12:30:51
Problema Diviz Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.7 kb
program diviz;
  const
    fin='diviz.in';
    fout='diviz.out';
    eps=30103;
var
  cif,k,a,b,i,j,l,m,n,r:longint;
  nr:array[0..201] of integer;
  first:array[0..9,0..201] of integer;
  ch:char;
  sum:longint;
  cc:array[0..200,0..200,0..100] of byte;
  c:array[0..200,0..200,0..100] of longint;
begin
  assign(input,fin);
    reset(input);
    readln(k,a,b);
    nr[0]:=0;
    while not(seekeof(input)) do
      begin
        read(ch);
        inc(nr[0]);
        nr[nr[0]]:=ord(ch)-48;
      end;
  close(input);
  assign(output,fout);
    rewrite(output);
    for cif:=0 to 9 do
      first[cif,nr[0]+1]:=maxint;
    for cif:=0 to 9 do
      for i:=nr[0] downto 1 do
        begin
          if nr[i]=cif then
            first[cif,i]:=i
          else
            first[cif,i]:=first[cif,i+1];
        end;
    for i:=1 to nr[0] do
      begin
        inc(c[1,i,nr[i] mod k]);
        for j:=i+1 to nr[0] do
          inc(c[1,j,nr[i] mod k]);
      end;
    for l:=1 to b do
      begin
        for i:=1 to nr[0] do
          for r:=0 to k-1 do
            for cif:=0 to 9 do
              if c[l,i,r]-c[l,i-1,r]<>0 then
              begin
                if first[cif,i+1]<>maxint then
                  if first[nr[i],i+1]>=first[cif,i+1] then
                  begin
                    c[l+1,first[cif,i+1],(r*10+cif)mod k]:=(c[l+1,first[cif,i+1],(r*10+cif)mod k]+c[l,i,r]-c[l,i-1,r])mod eps;
                  end;
              end;
        for i:=1 to nr[0] do
          for r:=0 to k-1 do
            c[l+1,i,r]:=c[l+1,i-1,r]+c[l+1,i,r];
      end;
    sum:=0;
    for l:=a to b do
      sum:=(sum+c[l,nr[0],0])mod eps;
    writeln(sum);
  close(output);
end.