Cod sursa(job #2644744)

Utilizator Ykm911Ichim Stefan Ykm911 Data 25 august 2020 20:07:59
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <fstream>

using namespace std;

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

int P;

int Zero(int x)
{
    int fac = 5, cnt = 0;
    while(x / fac != 0)
    {
        cnt += x / fac;
        fac *= 5;
    }
    return cnt;
}

int main()
{
    fin >> P;
    int st = 1, dr = 500000000, mij, nr, ans = 0;
    if(P == 0)
    {
        fout << "1\n";
        return 0;
    }
    while(st <= dr)
    {
        mij = (st + dr) / 2;
        nr = Zero(mij);
        if(nr == P)
        {
            ans = mij;
            break;
        }
        else if(nr < P)
            st = mij + 1;
        else
            dr = mij - 1;
    }
    if(ans == 0)
        fout << "-1\n";
    else
        fout << ans - (ans % 5) << "\n";
    return 0;
}