Cod sursa(job #553678)

Utilizator Cristy94Buleandra Cristian Cristy94 Data 14 martie 2011 11:15:32
Problema Diviz Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include<cstdio>
#include<cstring>
#define Nmax 256
#define Kmax 128
#define anterior l%2
#define curent (l-1)%2
#define mod 30103 
using namespace std;

int K,A,B,N;
char s[Nmax];
int next[10][Nmax],sol;
int a[2][Nmax][Kmax];

void prepr(){
	N=strlen(s);
	for(int i=N-2;i>=0;--i){
	for(int c=0;c<=9;++c)
		if(s[i+1]==c+'0')
			next[c][i+1]=i+2;
		else 
			next[c][i+1]=next[c][i+2];	
		
	}
	for(int i=0;i<=9;++i){
		if(s[0]==i+'0')
			next[i][0]=1;
		else
		next[i][0]=next[i][1];
		if(next[i][0]!=0 && i!=0)
		a[0][next[i][0]][i%K]=1;
	}
}

int main(){
	freopen("diviz.in","r",stdin);
	freopen("diviz.out","w",stdout);
	
	scanf("%d%d%d\n",&K,&A,&B);
	
	fgets(s,Nmax,stdin);
	if(s[strlen(s)-1]=='\n')
		s[strlen(s)-1]='\0';
	prepr();
    
	for(int l=2;l<=B;++l){
		
		memset(a[curent],0,sizeof(a[curent]));
		
		for(int c=0;c<=9;++c)
			for(int rest=0;rest<K;++rest)
				for(int poz=l-1;poz<=N;++poz){
					
				   if(next[c][poz] && a[anterior][poz][rest]){
					   
					   a[curent][next[c][poz]][(rest*10+c)%K] += a[anterior][poz][rest];
					   a[curent][next[c][poz]][(rest*10+c)%K]%=mod;
					}
				}
		if(l>=A)
		for(int i=1;i<=N;++i)
		      sol+=a[curent][i][0];
	}
	
	printf("%d",sol);
return 0;
}