Cod sursa(job #727283)

Utilizator enedumitruene dumitru enedumitru Data 27 martie 2012 20:32:31
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.54 kb
#include<fstream>
#define i64 unsigned long long 
using namespace std;
ifstream f("fact.in"); ofstream g("fact.out");
i64 d61=1LL<<61, c[30];
int i, p, nr, w, m, st, dr;
inline int fct(int n)
{int s=0, i=1;
 while(c[i]<=n) s+=n/c[i++];
 return s;
}
int main()
{   c[0]=1;  
    while(4*c[i]<d61-c[i]){i++; c[i]=c[i-1]*5;}
	f>>p; st=1; dr=p*5; 
	if(p==0) {g<<"1\n"; w=0;} else w=1;
	while((st<=dr) && w)
	   {m=(st+dr)/2;
	    nr=fct(m);
		if(nr==p) {g<<m-m%5<<"\n"; w=0;}
			else if(nr<p) st=m+1; else dr=m-1;
	   }
	if(w) g<<"-1\n";
	return 0;
}