Cod sursa(job #3039113)

Utilizator pregoliStana Andrei pregoli Data 28 martie 2023 10:39:37
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <iostream>
#include <stdio.h>
using namespace std;

int zerosOfFact(int x) {
    int cnt = 0;
    while (x >= 5) {
        x /= 5;
        cnt += x;
    }
    return cnt;
}

int main() {
    freopen("fact.in", "r", stdin);
    freopen("fact.out", "w", stdout);
    int p;
    cin >> p;
    if (p == 0) {
        cout << "1" << endl;
        return 0;
    }
    int low = 0, high = 1e9;
    while (low <= high) {
        int mid = (low + high) / 2;
        if (zerosOfFact(mid) == p) {
            cout << 5 * (mid / 5) << endl;
            return 0;
        }
        if (zerosOfFact(mid) < p) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    cout << -1 << endl;//*/
    return 0;
}