Cod sursa(job #3195010)

Utilizator Cezar2009Cezar Mihai Titihazan Cezar2009 Data 19 ianuarie 2024 22:00:25
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.75 kb
//https://infoarena.ro/problema/fact
#include <bits/stdc++.h>
using namespace std;

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

int main()
{
    long long p;
    fin>>p;
    if(p==0)
    {
        fout<<"1";
        return 0;
    }
    long long l=1,r=5e8;
    while (l<=r)
    {
        long long mm=(l+r)/2;
        long long c5=5,sum=0;
        while(c5<=mm)
        {
            sum+=mm/c5;
            c5*=5;
        }
        if(sum>=p)
        {
            r=mm-1;
        }
        else
        {
            l=mm+1;
        }
    }
    long long c5=5,sum=0;
    while(c5<=l)
    {
        sum+=l/c5;
        c5*=5;
    }
    if(sum==p)
        fout<<l;
    else
        fout<<"-1";
    return 0;
}