Cod sursa(job #600950)

Utilizator luckyme91wiz kid luckyme91 Data 4 iulie 2011 13:50:32
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.68 kb
#include <iostream>
#include <fstream>

using namespace std;
int zeros (int x)
{
	int count = x, den = 5;
	while (x >= den)
	{
		count += x/den;
		den *= 5;
	}
	return count;
}

int bsearch (int begin, int end, int x)
{
	if (begin <= end)
	{
		int half = (begin + end)/2;
		int zero = zeros(half);
		if (x > zero)
			return bsearch(half + 1, end, x);
		if (x < zero)
			return bsearch(begin, half - 1, x);
		if (x == zero)
			return half;
	}
	else
		return -1;
}

int main () {

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

int n, i, count, den;

in >> n;
if (n == 0)
{
	out << 1;
	return 0;
}

int sol =  bsearch (1, n, n);
if (sol > 0)
	out << sol * 5;
else
	out << -1;

return 0;
}