Cod sursa(job #21759)

Utilizator krisCristina kris Data 24 februarie 2007 11:41:15
Problema Factorial Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.61 kb
#include<fstream.h>
#include<math.h>


unsigned long fo(unsigned long k) {
	if(k/5==0) return 0;
	 else return k/5+fo(k/5);
}

long gg(unsigned long p) {
	
	if(p==0) return 1;
	if(p==5) return -1;
	if(p%25==0) return -1;
	if(p==5) return -1;
	if(p%5!=0 && (p%30)%5==(p%30)/5-1) return -1;  
	
	unsigned long c,n=0,m=5*100000000;
	
	while(n<=m) {
		
		c=(m+n)/2-((m+n)/2)%5;
		
		if(fo(c)<p) n=c+5;
		if(fo(c)>p) m=c-5;
	
		if(fo(c)==p) n=m+5;
	}
	return c;
	
}

	
main() {
	
	unsigned long p;
	
	ifstream f("fact.in");
	ofstream g("fact.out");
	f>>p;

	g<<gg(p);

return 0;
}