Cod sursa(job #834915)

Utilizator drobertDumitru Robert drobert Data 15 decembrie 2012 17:02:45
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("fact.in");
ofstream g("fact.out");
int n,p,st,dr,q,i;
int powy(int a,int b)
{
	int	prod=1,j;
	for (j=1;j<=b;j++)
		prod*=a;
	return prod;
}
int cb(int s,int d)
{
	int nr,suma=0;
	nr=(s+d)/2;
	nr*=5;
	for (i=12;i>=1;i--)
		suma+=nr/powy(5,i);
	if (suma==p)
		return nr;
	if (suma>p)
		return 1;
	if (suma<p)
		return -1;
}
int main ()
{
	f>>p;
	if (p==0)
		g<<"1";
	else
	{
		st=1;
		dr=p;
		while (st<=dr)
		{
			if (cb(st,dr)>1)
			{
				g<<cb(st,dr);
				q=1;
				break;
			}
			else if (cb(st,dr)==-1)
				st=(st+dr)/2+1;
			else if (cb(st,dr)==1)
				dr=(st+dr)/2-1;
		}
		if (!q)
			g<<"-1";
	}
	return 0;
}