Cod sursa(job #785025)

Utilizator danalex97Dan H Alexandru danalex97 Data 7 septembrie 2012 17:13:29
Problema Diviz Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 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[Cmax][Cmax]; 
int D[Cmax][Cmax][Cmax];
	
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][j][rest] )
					for (int cif=0;cif<=9;++cif)
						if ( S[cif][j+1]!=0 )
						{
							D[ i+1 ][ S[cif][j+1] ][ (rest*10+cif) %K ] += D[i][j][rest];
							if ( D[ i+1 ][ S[cif][j+1] ][ (rest*10+cif) %K ] > Mod )
								D[ i+1 ][ S[cif][j+1] ][ (rest*10+cif) %K ] -= Mod;
						}
	
	for (int i=A;i<=B;++i)
		for (int j=i;j<=M;++j)
		{
			Sol+=D[ i ][ j ][ 0 ];
			if ( Sol > Mod ) Sol-=Mod;
		}
	
	G<<Sol<<'\n';
}