Cod sursa(job #785028)

Utilizator danalex97Dan H Alexandru danalex97 Data 7 septembrie 2012 17:21:51
Problema Diviz Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <fstream>
using namespace std;

ifstream F("diviz.in");
ofstream G("diviz.out");

const int Cmax = 210;
const int Mod = 30103;

int K,A,B,N[Cmax],M,Sol;
int S[12][Cmax]; 
int D[2][Cmax][Cmax/2];
	
char Str[Cmax];int Act;

int main()
{
	F>>A>>B>>K;
	F.getline(Str,Cmax*2,'\n');
	F.getline(Str,Cmax*2,'\n');
	while ( Str[Act]<='9' && Str[Act]>='0' ) 
		N[++M]=Str[Act++]-'0';
	
	for (int i=M;i;--i)
		for (int j=0;j<=9;++j)
			S[j][i]=(N[i]==j) ? i : S[j][i+1];
	
	for (int cif=1;cif<10;++cif) 
		if ( S[cif][1] != 0 ) 
			D[ 1 ][ S[cif][1] ][ cif%K ] = 1;
	
	for (int i=1;i<M;++i)
		for (int j=1;j<=M-i;++j)
			for (int rest=0;rest<=K;++rest)
				if ( D[i%2][j][rest] )
					for (int cif=0;cif<=9;++cif)
						if ( S[cif][j+1]!=0 )
						{
							D[ (i+1)%2 ][ S[cif][j+1] ][ (rest*10+cif) %K ] += D[i%2][j][rest];
							if ( D[ (i+1)%2 ][ S[cif][j+1] ][ (rest*10+cif) %K ] > Mod )
								D[ (i+1)%2 ][ S[cif][j+1] ][ (rest*10+cif) %K ] -= Mod;
							if ( i+1>=A && i+1<=B )
							{
								Sol+=D[i%2][j][rest];
								if ( Sol > Mod ) Sol-=Mod;
							}
						}
	
	G<<Sol<<'\n';
}