Cod sursa(job #1876051)

Utilizator loghin.alexandruLoghin Alexandru loghin.alexandru Data 11 februarie 2017 21:58:19
Problema Factorial Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb

#include <fstream>
#define MAX_FACT 500000000

using namespace std;


ifstream fin("fact.in");
ofstream fout("fact.out");


unsigned long long getZeros(unsigned long long n)
{
	unsigned long long power = 5;
	unsigned long long zeros=0;
	while (n / power != 0)
	{
		zeros += n / power;
		power *= 5;
	}
	return zeros;
}

int main()
{
	bool found = false;
	unsigned long long middle = 0;
	unsigned long long firstN = MAX_FACT, input;
	unsigned long long n = 0, start = 0, end = firstN;
	fin >> n;
	input = n * 5;
	if (n == 0){fout << -1;return 0;}
	if (n == 1) { fout << 5; return 0; }
	while (start <= end)
	{
		middle = (start + end) / 2;
		if (n == getZeros(middle)) { found = true; break; }
		if (getZeros(middle) > n)
		{
			end = middle - 1;
		}
		else
		{
			start = middle + 1;
		}
	}
	if (found)
	{
		if (middle % 10 >= 5)
		{
			fout << middle / 10 << 5;
		}
		else
		{
			fout << middle / 10 << 0;
		}

	}
	else fout << -1;

	return 0;
}