Cod sursa(job #2801251)

Utilizator chiriacandrei25Chiriac Andrei chiriacandrei25 Data 15 noiembrie 2021 17:54:40
Problema Factorial Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.75 kb
#include<iostream>
#include<fstream>
#include<climits>
using namespace std;
long long i, p, mij, sol=LLONG_MAX;

long long calc(long long x)
{
    long long j = 5, sum = 0;
    while(j <= x)
    {
        sum += x / j;
        j = j * 5;
    }
    return sum;
}

int main()
{
    freopen("fact.in", "r", stdin);
    freopen("fact.out", "w", stdout);
    cin >> p;
    long long st = 1, dr = LLONG_MAX-1000;
    while(st <= dr)
    {
        mij = (st + dr) / 2;
        if(calc(mij) >= p)
        {
            dr = mij - 1;
            if(calc(mij)==p)
            sol = min(sol, mij);
        }
        else
        {
            st = mij + 1;
        }
    }

    if(calc(sol)==p)
    cout << sol;
    else cout<<-1;
}