Cod sursa(job #3377271)

Utilizator elena_ionIon Elena Daniela elena_ion Data 22 noiembrie 2026 18:41:33
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <fstream>
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");
int zerouri(long long int n)
{
    long long int s = 0;
    while(n > 0)
    {
        s += n / 5;
        n /= 5;
    }
    return s;
}
int main()
{
    long long int p, st, dr, mij, ans = 1, z;
    fin >> p;
    st = 1;
    dr = 5 * p;
    while(st <= dr)
    {
        mij = (st + dr) / 2;
        z = zerouri(mij);
        if(z < p)
        {
            st = mij + 1;
        }
        else
        {
            ans = mij;
            dr = mij - 1;
        }
    }
    if(zerouri(ans) == p)
    {
        fout << ans;
    }
    else
    {
        fout << -1;
    }
    return 0;
}