Cod sursa(job #3252145)

Utilizator andrei257Andrei Enache andrei257 Data 28 octombrie 2024 18:29:04
Problema Factorial Scor 25
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.73 kb
// https://www.infoarena.ro/problema/fact

#include <bits/stdc++.h>
using namespace std;

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

int fact5 (const int x)
{
	int n = 0;
	for (int i = 5; i <= x; i *= 5)
		n += x / i;
	return n;
}

int searchInput(const int st, const int dr, const int target)
{
	const int mid = (st + dr) / 2, val = fact5(mid);
	if (st > dr)
		return -1;
	if (val == target)
		return mid;
	if (val > target)
		return searchInput(st, mid - 1, target);
	return searchInput(mid + 1, dr, target);
}

int main()
{
	int p, sol;
	fin >> p;
	sol = searchInput(0, 600000, p);
	if (sol != -1 && sol >= 5)
		sol = sol - sol % 5;
	else if (sol != -1)
		sol = 1;
	fout << sol;
	return 0;
}