Cod sursa(job #3354745)

Utilizator Alex_at_gameIustin-Alexandru Frateanu Alex_at_game Data 20 mai 2026 09:55:08
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(v) begin(v), end(v)
#define al(v, l, r) begin(v) + l, begin(v) + r + 1
#define sz(v) static_cast<int>(v.size())
#define pb push_back
#define pob pop_back
#define fs first
#define sd second

constexpr int inf = 2e9;
constexpr ll infll = 4e18;

inline int nr(int x) {
    int k = 0;
    for (int i = 5; i <= x; i *= 5) {
        k += x / i;
    }

    return k;
}

int p;
signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    freopen("fact.in", "r", stdin);
    freopen("fact.out", "w", stdout);

    cin >> p;

    if (!p) {
        cout << "1\n";
        exit(0);
    }

    int l = 0, r = 5 * p, ans = -1;

    while (l <= r) {
        int mij = l + ((r - l) >> 1);

        if (nr(mij) >= p) {
            ans = mij;
            r = mij - 1;
        } else {
            l = mij + 1;
        }
    }

    if (nr(ans) != p) {
        cout << "-1\n";
    } else {
        cout << ans << "\n";
    }
}