Cod sursa(job #504126)

Utilizator mihaipopa12Popa Mihai mihaipopa12 Data 26 noiembrie 2010 17:58:54
Problema Pascal Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include<stdio.h>

#include<string>

FILE*f=fopen("pascal.in","r");
FILE*g=fopen("pascal.out","w");

int i,R,D,P[5],X[5],Y[5],x,p,j,pp,Pp[5],XX,NR,ok;

void init () {
	if ( D == 2 ){ X[++p] = 2; Y[p] = 1; }
	if ( D == 3 ){ X[++p] = 3; Y[p] = 1; }
	if ( D == 4 ){ X[++p] = 2; Y[p] = 2; }
	if ( D == 5 ){ X[++p] = 5; Y[p] = 1; }
	if ( D == 6 ){ X[++p] = 2; Y[p] = 1; X[++p] = 3; Y[p] = 1; }	
	
	pp = R;
	for ( j = 1 ; j <= p ; ++j ){
		x = X[j]; 
		while ( x <= pp ){
			Pp[j] += pp / x;
			x *= X[j];
		}
		
	}
}

int main () {
	
	fscanf(f,"%d %d",&R,&D);
	init();	
	
	for ( i = 1 ; i < R  ; ++i ){
		
		memcpy(P,Pp,sizeof(Pp));
		
		XX = pp - i;
		for ( j = 1 ; j <= p ; ++j ){
			x = X[j];
			while ( x <= XX ){
				P[j] -= XX / x;
				x *= X[j];
			}
			
		}
		XX = i;
		for ( j = 1 ; j <= p ; ++j ){
			x = X[j];
			while ( x <= XX ){
				P[j] -= XX / x;
				x *= X[j];
			}
			
		}
		
		for ( j = 1 ; j <= p ; ++j ){
			ok = 0;
			if ( P[j] < Y[j] )
				ok = 1;
			if ( !ok )
				++NR;
		}
		
	}
	
	fprintf(g,"%d\n",NR);
	
	fclose(f);
	fclose(g);
	
	return 0;
}