Cod sursa(job #3316518)

Utilizator gabi70Bila Chirsanov Gabriel gabi70 Data 19 octombrie 2025 01:07:47
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <bits/stdc++.h>
using namespace std;

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

int zero(int n) {
    int cnt = 0;
    for (long long p = 5; p <= n; p *= 5)
        cnt += n / p;
    return cnt;
}

int main(){
    int n;
    fin >> n;

    int binL = 1, binH = 5 * n + 1000, ans = -1;

    while (binL <= binH) {
        int mid = (binL + binH) / 2;
        int z = zero(mid);

        if (z < n){
            binL = mid + 1;
        } else {
            binH = mid - 1;
            if (z == n) ans = mid;
        }
    }

    if (ans != -1) {
        while (ans > 1 && zero(ans - 1) == n) ans--;
        gout << ans;
    } else {
        gout << -1;
    }
    return 0;

}