Cod sursa(job #139008)

Utilizator marius135Dumitran Adrian Marius marius135 Data 19 februarie 2008 16:58:52
Problema Diviz Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include<stdio.h>
#include<string.h>

#define maxn (1<<8)

long k,a,b;
char sir[maxn];
unsigned short int v[2][maxn][maxn];
long next[maxn][16];

int main()
{
	freopen("diviz.in","r",stdin);
	freopen("diviz.out","w",stdout);
	
	scanf("%ld %ld %ld",&k,&a,&b);
	scanf("%s",sir);
	
	long n = strlen(sir);
	
	for( long cif = 0; cif <= 9; ++cif)
	{
		long max = n+1;
		for(long i = n; i >= 0 ; --i)
		{
			next[i][cif] = max;
			if( sir[i] -'0' == cif)
				max = i;
			
		}
		if( max != n+1 && cif != 0)
		{
			v[1][max][cif%k] = 1;
		}
	}
	long sol =0;
	
	for( long l2 = 1; l2 <= b; ++l2)
	{
		long l = l2%2;
		long l3 = (l+1)%2;
		for( long i = 0;  i < n; ++i)
		{
			for( long j = 0; j < k; ++j)
			{
				if( v[l][i][j] == 0 ) continue;
				if( l2 >= a && l2 <= b && j == 0)
				{
					sol+=v[l][i][j];
					if( sol >= 30103)
						sol-= 30103;
				}
				if( l == b) continue;
			
				for( long cif = 0; cif <= 9; ++cif)
				{
					long aux = next[i][cif];
					if( aux == n+1) continue;
					long aux2 = j*10 + cif;
					while( aux2 >= k)
						aux2-=k;
					v[l3][aux][aux2]+=v[l][i][j];
					if( v[l3][aux][aux2] >= 30103)
						v[l3][aux][aux2] -= 30103;
				}
				v[l][i][j] = 0;
			}
		}
	}
	printf("%ld\n",sol);
	
	return 0;
}