Cod sursa(job #286336)

Utilizator undogSavu Victor Gabriel undog Data 23 martie 2009 18:12:28
Problema Factorial Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.44 kb
#include <stdio.h>
#include <fstream>

using namespace std;

int main(){
	freopen("fact.in","rt",stdin);
	fstream f("fact.out",ios::out);
	unsigned long p;
	scanf("%lu",&p);
	unsigned long long l,r,n,m,i,t;
	l=1;r=0xfffff5;
	while(l<r){
		m=(l+r)/2;
		n=0;
		
		for(i=5;i<=m;i+=5){
			t=i;
			while(t%5==0){
				n++;
				t/=5;
			}
		}
		
		if(n<p)
			l=m+1;
		if(n>p)
			r=m-1;
		if(n==p){
			f<<m-m%5;
			return 0;
		}
	}
	
	f<<"-1";
	return 0;
}