Cod sursa(job #3311866)

Utilizator BuzdiBuzdugan Rares Andrei Buzdi Data 24 septembrie 2025 18:24:23
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define ld long double

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

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

int p;

int get_cnt(ll n) {
    ll p5 = 5;
    int cnt = 0;
    while(p5 <= n) {
        cnt += n / p5;
        p5 *= 5;
    }
    return cnt;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    fin >> p;
    ll left = 1, right = 1e9, answer = -1;
    while(left <= right) {
        ll mid = (left + right) / 2;
        if(get_cnt(mid) >= p) {
            answer = mid;
            right = mid - 1;
        }
        else {
            left = mid + 1;
        }
    }

    if(answer == -1) {
        fout << -1 << '\n';
        return 0;
    }
    if(get_cnt(answer) != p) {
        fout << -1 << '\n';
        return 0;
    }
    fout << answer << '\n';
    return 0;
}