Cod sursa(job #2977605)

Utilizator Eraserurares campean Eraseru Data 11 februarie 2023 22:51:27
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.54 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");
ll n, p;
ll sum(ll x){
    ll crt = x / 5, sol = 0;
    while(crt){
        sol += crt;
        crt /= 5;
    }
    return sol;
}
int main()
{
    fin >> p;
    ll l = 1, r = 1e11;
    while(l <= r){
        ll mid = (l + r) / 2;
        if(sum(mid) == p) n = mid, r = mid - 1;
        else
        if(sum(mid) < p) l = mid + 1;
        else r = mid - 1;
    }
    n = (n == 0 ? -1 : n);
    fout << n;

}