Cod sursa(job #391927)

Utilizator mihai_voiVoicescu Mihai mihai_voi Data 6 februarie 2010 15:20:49
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.44 kb
#include<fstream.h>
int n,p,st,dr,m,nr,s;
int main()
{
	ifstream f("fact.in");
	ofstream g("fact.out");
	f>>p;
	if(p==0)
	{
		g<<1;
		return 0;
	}
	st=1;
	dr=2000000000;
	while (st<=dr)
	{
		m=(st+dr)/2;
		nr=0;
		s=5;
		while (s<=m)
		{
			nr=nr+m/s;
			s=s*5;
		}
		if(nr==p)
		{
			m=m-m%5;
			g<<m;
			return 0;
		}
		if(nr<p)
		{
			st=m+1;
		}
		else
		{
			dr=m-1;
		}
	}
	if(st>dr) g<<"-1";
	return 0;
}