Cod sursa(job #2365620)

Utilizator MichaelXcXCiuciulete Mihai MichaelXcX Data 4 martie 2019 15:17:12
Problema Factorial Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.69 kb
#include    <bits/stdc++.h>

using namespace std;

typedef long long LL;

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

LL p;

int checkZero(LL nr)
{
    LL nrZero = 0;
    LL c = 5;
    while(c<=nr)
    {
        nrZero += nr/c;
        c*=5;
    }
    return nrZero;
}

LL binSearch(LL lef, LL rig)
{
    while(lef < rig)
    {
        LL m = (lef + rig)/2;
        if(checkZero(m) >= p) rig = m;
        else lef = m + 1;
    }
    return lef;
}

int main()
{
    fin >> p;
    if(p == 0)
    {
        cout << 1;
        return 0;
    }

    LL nr = binSearch(1, 500000000);
    if (p == checkZero(nr)) fout << nr;
	else fout << -1;

    return 0;
}