Cod sursa(job #1876047)

Utilizator loghin.alexandruLoghin Alexandru loghin.alexandru Data 11 februarie 2017 21:53:02
Problema Factorial Scor 35
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 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()
{
	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)) { fout << middle; return 0; }
		if (getZeros(middle) > n)
		{
			end = middle - 1;
		}
		else
		{
			start = middle + 1;
		}
	}

	fout << -1;
	return 0;
}