Cod sursa(job #87895)

Utilizator toni2007Pripoae Teodor Anton toni2007 Data 29 septembrie 2007 16:15:47
Problema Factorial Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include<stdio.h>
int v[15],t[15];
void genereaza(){
	//v[0]=1;
	t[0]=1;
	v[0]=5;
	t[1]=6;
	v[1]=25;
	t[2]=31;
	v[2]=125;
	t[3]=156;
	v[3]=625;
	t[4]=781;
	v[4]=3125;
	t[5]=3906;
	v[5]=15625;
	t[6]=19531;
	v[6]=78125;
	t[7]=97656;
	v[7]=390625;
	t[8]=488281;
	v[8]=1953125;
	t[9]=2441406;
	v[9]=9765625;
	t[10]=12207031;
	v[10]=48828125;
	t[11]=61035156;
	v[11]=244140625;
	t[12]=305175781;
}
int main(){
	int s,i,p,f;
	genereaza();
	freopen("fact.in","r",stdin);
	freopen("fact.out","w",stdout);
	scanf("%d",&p);
	s=0;
	f=p+1;
	if (p==0){
		printf("1\n");
		return 0;
	}
	while (p>1){
		for (i=1;i<13&&t[i]<=p;++i);
		--i;
		s+=p/t[i]*v[i];
		p%=t[i];
	}
	p=f;f=s;s=0;
	while (p>1){
		for (i=1;i<13&&t[i]<=p;++i);
		--i;
		s+=p/t[i]*v[i];
		p%=t[i];
	}
	if (s==f) printf("-1\n");
	else printf("%d\n",s);
	return 0;
}