Cod sursa(job #37218)

Utilizator andrewgPestele cel Mare andrewg Data 24 martie 2007 18:12:22
Problema Diviz Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.84 kb
const maxn = 202;
      maxk = 102;
      modulo = 30103;

type huge = array[1..maxn]of integer;

var f:text;
    n,k,a,b,i,j,l,p,sol,x:longint;
    d:huge;
    ap:array[0..9,1..maxn]of integer;
    c,c1:array[1..maxn,0..maxk]of integer;

procedure readdata;
var s:string;
    aux:integer;
begin
   assign(f,'diviz.in');
   reset(f);
   readln(f,k,a,b);
   readln(f,s);
   n:=length(s);
   for i:=1 to length(s) do
   begin
      val(s[i],d[i],aux);
   end;
   close(f);
end;

procedure solve;
begin
   fillchar(c,sizeof(c),0);
   for i:=0 to 9 do
   begin
      if d[n]=i then
      begin
         ap[i,n]:=n;
      end
         else ap[i,n]:=0;
      for j:=n-1 downto 1 do
      begin
         if d[j]=i then
         begin
            ap[i,j]:=j;
         end
            else ap[i,j]:=ap[i,j+1];
      end;
   end;
   for i:=1 to 9 do
   begin
      if ap[i,1]<>0 then
      begin
         c[ap[i,1],i mod k]:=1;
      end;
   end;
   for i:=1 to n do
   begin
      fillchar(c1,sizeof(c1),0);
      for j:=1 to n do
      begin
         for l:=0 to k-1 do
         begin
            if c[j,l]<>0 then
            begin
               if (l=0) and (a<=i) and (i<=b) then
               begin
                  sol:=(sol+c[j,l]) mod modulo;
               end;
               for p:=0 to 9 do
               begin
                  if (ap[p,j+1]<>0) and (ap[p,j+1]<=n) and (j+1<=n) then
                  begin
                     x:=(10*l+p) mod k;
                     c1[ap[p,j+1],x]:=(c1[ap[p,j+1],x]+c[j,l]) mod modulo;
                  end;
               end;
            end;
         end;
      end;
      c:=c1;
   end;
end;

procedure writedata;
begin
   assign(f,'diviz.out');
   rewrite(f);
   writeln(f,sol);
   close(f);
end;

begin
   readdata;
   solve;
   writedata;
end.