Cod sursa(job #673496)

Utilizator JBaccountCatalin JBaccount Data 4 februarie 2012 15:53:38
Problema Factorial Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

#define LL long long

LL P;

LL HM(LL X)
{
	LL ans2 = 0, ans5 = 0;
	LL p = 2;
	while (X/p)
	{
		ans2 += X/p;
		p *= 2;
	}	
	p = 5;
	while (X/p)
	{
		ans5 += X/p;
		p *= 5;
	}	
	return min(ans2, ans5);
}

int main()
{
	freopen ("factorial.in", "r", stdin);
	freopen ("factorial.out", "w", stdout);
	
	scanf ("%lld", &P);
	
	LL st = 1, dr = 1000000000000000000LL;
	
	while (st < dr)
	{
		LL mij = (st + dr)/2;
		if (HM(mij) >= P) dr = mij;
		else st = mij + 1;
	}	
	
	if (HM(st) == P) printf ("%lld", st);
	else printf ("-1");
	
	return 0;
}