Cod sursa(job #551509)

Utilizator RampageSergiu Caraian Rampage Data 10 martie 2011 20:33:42
Problema Factorial Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.53 kb
#include <fstream>
using namespace std;

ifstream f ("fact.in");
ofstream g ("fact.out");

int main ()
	{
	long p,s,a=5,b=1000000,x,j;
	bool flag=false;
	f>>p;

	while (flag==false && a<=b)
			{
			s=0;
			x=(a+b)/2;
			j=5;
			while (j<=x)
				{
				s+=x/j;
				j*=5;
				}
			if (s==p)
				flag=true;
			else if (s>p)
				b=x-1;
			else 
				a=x+1;
			}
		
		if (p==0)
			g<<1;
		else if (a>b)
			g<<-1;
		else if (s==p)
			{
			while (x%5!=0)
				x-=1;
			g<<x;
			}
		
	g.close();
	return 0;
	}