Cod sursa(job #114360)

Utilizator coderninuHasna Robert coderninu Data 13 decembrie 2007 22:23:26
Problema Factorial Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.61 kb
#include <stdio.h>

long P;

long binSear(long,long);
long nr(long x);
long cinci(long x) { return !(x%5)?x:(x-x%5); }

int main()
{
	freopen("fact.in", "r", stdin);
	scanf("%ld", &P);
	fclose(stdin);
	freopen("fact.out", "w", stdout);
	printf("%ld", !P?-1:binSear(0, P*10));
	fclose(stdout);
	return 0;
}

long nr(long x)
{
	long temp=0;
	for (long i=5; i<=x; temp+=x/i, i*=5);
	return temp;
}

long binSear(long p, long q)
{
	if (q-p<=5) return -1;
	long m=cinci((p+q) / 2), temp = nr(m);
	if (temp==P) return m;
	else if (temp > P) return binSear(p, m);
	else return binSear(m,q);
}