Cod sursa(job #30517)

Utilizator mike4problemsRadu Gabriel mike4problems Data 14 martie 2007 14:27:34
Problema Diviz Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 kb
#include<stdio.h>
#include<string.h>

#define Mod 30103

#define In "diviz.in"
#define Out "diviz.out"

FILE *f;

char n[205];
int l,k,a,b,i,j,r,cif,x,y,z,sol,tx,p;
int first[10][201];
int lap[10];
int m[2][201][100];

int main()
 {
 f=fopen(In,"r");
 fscanf(f,"%d%d%d\n",&k,&a,&b);
 fgets(n+1,204,f);
 l=strlen(n+1);
 if(n[l]=='\n');
 n[l--]='\0';
 for(i=1;i<=l;i++) n[i]-='0';
 for(i=l;i>=0;i--)
  {
  for(j=0;j<10;j++) first[j][i]=lap[j];
  lap[n[i]]=i;
  }
 for(j=1;j<10;j++)
  m[1][first[j][0]][j%k]=1;
 p=k*sizeof(int);
 for(i=1;i<a;i++)
  {
  tx=i%2; x=!tx;
  for(j=1;j<=l;j++)
   memset(m[x][j],0,p);
  for(j=1;j<=l;j++)
   for(cif=0;cif<10;cif++)
    {
    y=first[cif][j];
    for(r=0;r<k;r++)
     {
     z=(r*10+cif)%k;
     m[x][y][z]=(m[tx][j][r]+m[x][y][z])%Mod;
     }
    }
  }
 for(i=a;i<=b;i++)
  {
  tx=i%2; x=!tx;
//  memset(m[tx],0,sizeof(m[tx]));
  for(j=1;j<=l;j++)
   memset(m[x][j],0,p);
  for(j=1;j<=l;j++)
   {
   sol=(sol+m[tx][j][0])%Mod;
   for(cif=0;cif<10;cif++)
    {
    y=first[cif][j];
    for(r=0;r<k;r++)
     {
     z=(r*10+cif)%k;
     m[x][y][z]=(m[tx][j][r]+m[x][y][z])%Mod;
     }
    }
   }
  }
 freopen(Out,"w",f);
 fprintf(f,"%d\n",sol);
 fclose(f);
 return 0;
 }