Cod sursa(job #3238890)

Utilizator Andrei1209Andrei Mircea Andrei1209 Data 31 iulie 2024 12:45:52
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <fstream>

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

int main()
{
    long long p, st = 1, dr, mij, nr_zero, x, sol = -1;
    fin >> p;
    if ( p  == 0 )
    {
        fout << 1 << '\n';
        return  0;
    }
    dr  = 400000015;
    while ( st <= dr )
    {
        mij = ( st + dr) / 2;
        nr_zero = 0;
        x = 5;
        while ( mij >= x )
        {
            nr_zero += mij / x;
            x =  x * 5;
        }
        if ( nr_zero < p )
            st  = mij + 1;
        if ( nr_zero > p )
            dr = mij - 1;
        if ( nr_zero == p )
        {
            sol = mij;
            break;
        }
    }
    if ( sol == -1)
        fout << sol << '\n';
    else
    {
        sol = sol / 5 * 5;
        fout << sol << '\n';

    }
    return 0;
}