Cod sursa(job #579814)

Utilizator BlackElfSpulber Iosif BlackElf Data 12 aprilie 2011 14:54:10
Problema Factorial Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <iostream>
#include <fstream>

using namespace std;

int pws[]={5,25,125,625,3125,15625,78125,390625,1953125,9765625,48828125,244140625,1220703125};

int noOfFives (int n)
{
	int c = 0;
	int i = 0;
	int a = 1;

	while (a)
	{
		a = n / pws[i];
		c += a;
		i ++;
	}

	return c;
}

int main ()
{
	ifstream in ("fact.in");
	ofstream out ("fact.out");

	int p;
	int n = 5;
	int nfv;
	int left, right;

	in >> p;

	if (p == 0)
	{
		out << 1 << endl;
		return 0;
	}

	if (p < 0)
	{
		out << -1 << endl;
		return 0;
	}
	

	left = 0;
	right = 400000016;
	int pw = 1;

	while (1)
	{
		n = (left + right) / 2;
		nfv = noOfFives(n);

		if (nfv < p)
			left = n;
		else
			if (nfv > p)
				right = n;
			else
			{
				n = n - n % 5;
				out << n << endl;
				return 0;
			}
	}

	return 0;
}