Cod sursa(job #235897)

Utilizator marinMari n marin Data 26 decembrie 2008 11:59:46
Problema Diviz Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include<stdio.h>
#include<string.h>
#define DIM 202
#define MOD 30103

int a[DIM][DIM], b[DIM][DIM];
int fc[10][DIM];

char s[DIM];

int n,k,A,B,i,j,tot,cif,t;

int main(){
	FILE *f = fopen("diviz.in","r");
	fscanf(f,"%d %d %d",&k,&A,&B);
	fscanf(f,"%s",s);
	fclose(f);
	n=strlen(s);
	for (j=0;j<=9;j++)
		fc[j][n-1] = fc[j][n] = n;
	fc[s[n-1]-48][n-1] = n-1;
	for (i=n-2;i>=0;i--){
		for (j=0;j<=9;j++)
			if (s[i]-48==j) {
				fc[j][i] = i;
			} else {
				fc[j][i] = fc[j][i+1];
			}
	}

	for (i=1;i<=9;i++)
		if (fc[i][0]<n)
			a[fc[i][0]][i % k] = 1;


	for (t=2;t<=n;t++){
		for (i=0;i<=n;i++)
			for (j=0;j<=k;j++)
				b[i][j] = 0;

		for (i=t-2;i<n;i++)
			for (j=0;j<k;j++)
				if (a[i][j]!=0)
					for (cif = 0; cif <= 9; cif++)
					if (fc[cif][i+1]!=n){
						b[fc[cif][i+1]][(j*10+cif)%k]+=a[i][j];
						b[fc[cif][i+1]][(j*10+cif)%k]%=MOD;
					}

		for (i=0;i<n;i++)
			for (j=0;j<k;j++) {
				a[i][j] = b[i][j];
				if (j==0&&t>=A&&t<=B) {
					tot = (tot + b[i][j]) % MOD;
				}
			}
	}

	FILE *g = fopen("diviz.out","w");
	fprintf(g,"%d",tot);
	fclose(g);
}