Cod sursa(job #1604119)

Utilizator Catalin121Catalin Sumanaru Catalin121 Data 17 februarie 2016 23:15:44
Problema Factorial Scor 55
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 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)/5) / 2)*5;
		int temp = parse(mid, 5, 0);
		if (temp < pos)
			l = mid + 5;
		else
				r = mid - 5;
	}
	return l;
}

int main()
{
	long  pos;
	ifstream fin("fact.in");
	fin >> pos;
	fin.close();

	int temp = bin(1, pos * 5, pos);

	ofstream fout("fact.out");
	if (pos == 0) fout << 1;
	else

	if (parse(temp, 5, 0) == pos)
			fout << temp << endl;
	else
		fout << -1;
	fout.close();
	cin >> pos;

	return 0;

}