Cod sursa(job #639548)

Utilizator andrei1291Balasanu Andrei Nicolae andrei1291 Data 23 noiembrie 2011 15:31:28
Problema Factorial Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include<fstream>
#include<iostream>
#include<math.h>
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");
long long f(long long c)
{
	long long i,k=1,x=1,pt5=5;
	long long z=25,m=0;
	while(c%5)
		c--;
	m=c/5;
	for(i=5;i<=c;i+=5)
	{
		k=i/5;
		while(k%5==0)
		{
			m++;
			k=k/5;
		}
	}
	return m;
}
int main()
{
	long long p;
	long long st,sp,m;
	fin>>p;
	if(p==0)
	{
		cout<<1;
		return 0;
	}
	st=0;
	sp=(p+1)*5;
	while(st<=sp)
	{
		m=(st+sp)/2;
		if(f(m)==p)
		{
			while(m%5)
				m--;
			fout<<m;
			return 0;
		}
		else
		{
			if(f(m)<p)
			{
				st=m+1;
			}
			else
			{
				sp=m-1;
			}
		}
	}
	fout<<-1;
	return 0;
}