Cod sursa(job #3354185)

Utilizator wizardragonWizard Dragon wizardragon Data 16 mai 2026 04:03:59
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <iostream>
#include <fstream>

using namespace std;

typedef long long ll;

ll count_zeros(ll n) {
    ll count = 0;
    while (n > 0) {
        n /= 5;
        count += n;
    }
    return count;
}

int main() {
    ifstream fin("fact.in");
    ofstream fout("fact.out");
    ll p;
    if (!(fin >> p)) return 0;

    if (p == 0) {
        fout << 1 << endl;
        return 0;
    }

    ll low = 1, high = 5000000000LL;
    ll best_n = -1;

    while (low <= high) {
        ll mid = low + (high - low) / 2;
        if (count_zeros(mid) >= p) {
            best_n = mid;
            high = mid - 1;
        } else {
            low = mid + 1;
        }
    }

    if (best_n != -1 && count_zeros(best_n) == p) {
        fout << best_n << endl;
    } else {
        fout << -1 << endl;
    }

    return 0;
}