Cod sursa(job #723424)

Utilizator cdascaluDascalu Cristian cdascalu Data 25 martie 2012 14:34:48
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.63 kb
#include<fstream>
#include<values.h>
using namespace std;
unsigned long p;
unsigned long func(unsigned long x);
int main()
{
	unsigned long c,bla=0,a=0,b=MAXLONG,ok=1,x;
	ifstream f("fact.in");
	f>>p;
	f.close();



	while(ok&&!bla)
	{
	   c=(a+b)/2;

	   x=func(c);
	   if(a>b)bla=1;
	   if(x==p)ok=0;
	   else
		if(x<p)a=c+1;
		else b=c-1;
	}
	ofstream g("fact.out");
	if(bla)g<<-1;
	else
	if(p==0)
	  g<<1;
	else g<<(c/5)*5;
	g.close();
	return 0;
}
unsigned long func(unsigned long x)
{
   unsigned long con=0;
   while(x/5)
   {
	  con+=x/5;
	  if(con>p){x=0;continue;}
	  x/=5;
   }
   return con;
}