Cod sursa(job #1249386)

Utilizator killer301Ioan Andrei Nicolae killer301 Data 26 octombrie 2014 22:14:32
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.59 kb
#include <cstdio>

using namespace std;

int exp5(int x)
{
	int ans=0;
	long long p=5;
	while(p<=x)
	{
		ans+=x/p;
		p*=5;
	}
	return ans;
}

int main()
{
    freopen("fact.in", "r", stdin);
    freopen("fact.out", "w", stdout);

    int p;
    scanf("%d", &p);

    int st=1, dr=1000000000;
    int last=-1;
    while(st<=dr)
	{
		int med=(st+dr)/2;
		int val=exp5(med);
		if(val<p)st=med+1;
		else if(val>p)dr=med-1;
		else if(val==p)
		{
			dr=med-1;
			last=med;
		}
	}
	if(exp5(st)==p)last=st;
	printf("%d\n", last); ///-1 = nu e solutie
    return 0;
}