Cod sursa(job #629267)

Utilizator blue_phoenixPosea Elena blue_phoenix Data 3 noiembrie 2011 00:16:11
Problema Factorial Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <stdio.h>

long v[]={5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625, 48828125};
int cate_zerouri(long n){
   //cate zerouri are n!
   int k=0,suma=0;
   int x=n;
   while(x!=0 && k<11) {
      x=n/v[k];
      suma+=x;  
      k++;
   }
   return suma;
}

int cauta_n(int p){
   int li,ls,k,s,pn;
	li=1;
	ls=1000000000;
	while(li<ls){
           k=li+(ls-li)/2;
	   s=cate_zerouri(k);
	   if(s==p) return k;
	   if(s<p) li=k+1;
	      else ls=k-1;
	}
	return 0;
}

int main()
{
	int p,k;
	FILE *fin=fopen("fact.in","r");
	fscanf(fin,"%d",&p);
	fclose(fin);
	k=cauta_n(p);
	k=k-k%5;
	FILE *fout=fopen("fact.out","w");
	if (p == 0) fprintf(fout,"1\n");
	else if(k)  fprintf(fout,"%d\n",k);
	else  fprintf(fout,"-1\n");
	fclose(fout);
	return 0;
}