Cod sursa(job #1604121)

Utilizator Catalin121Catalin Sumanaru Catalin121 Data 17 februarie 2016 23:20:03
Problema Factorial Scor 55
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <iostream>
#include <fstream>

using namespace std;



int parse(int total, int divide, int ext)
{
	int temp = total / divide;

	if (temp != 0)
			return parse(total, divide * 5, ext + temp);
	else	return ext;
}

int bin(int l, int r, int pos)
{
	int mid;
	while (l < r)
	{

		mid = (l + r) / 2;
		int temp = parse(mid, 5, 0);
		if (temp < pos)
			l = mid + 1;
		else
				r = mid - 1;
	}
	return l;
}

int main()
{
	long  pos;
	ifstream fin("fact.in");
	ofstream fout("fact.out");
	fin >> pos;
	fin.close();
	if (pos == 0) {
		fout << 1;	

	}
	else {
		int temp = bin(1, pos * 5, pos);
		if (parse(temp, 5, 0) == pos)
			fout << temp << endl;
		else
			fout << -1;
	}
	fout.close();
	fin >> pos;

	return 0;

}