Cod sursa(job #3294223)

Utilizator mihai.25Calin Mihai mihai.25 Data 19 aprilie 2025 21:23:27
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <fstream>

using namespace std;

ifstream fin ("fact.in");

ofstream fout ("fact.out");

int caut_bin (int p, bool & gasit) {

	int val = -1, st = 1, dr = 1e9;

	while (st <= dr) {

		int mij = st + (dr - st) / 2;

		int put = 5, cnt = 0;

		while (put <= mij) {

			cnt += mij / put;

			put *= 5;
		}

		if (cnt == p) {

			gasit = true;

			val = mij;

			dr = mij - 1;
		}
		else {

			if (cnt > p)
				dr = mij - 1;
			else
				st = mij + 1;
		}
	}

	return val;
}

int main () {

	int p;

	fin >> p;

	bool gasit = false;

	int n = caut_bin (p, gasit);

	if (!gasit)
		fout << -1;
	else
		fout << n;

	return 0;
}