Cod sursa(job #2386665)

Utilizator mihailrazMihail Turcan mihailraz Data 23 martie 2019 12:52:13
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <fstream>

using namespace std;
ifstream fi("fact.in");
ofstream fo("fact.out");
const int nmax=2e9;
int p;

int numara(int x, int div)
{
    int cnt=0, pw=div;
    while(x/pw>0)
    {
        cnt+=x/pw;
        pw*=div;
    }
    return cnt;
}

int bSearch(void)
{
    int lo=1, hi=nmax, mid, res=-1;
    while(lo<=hi)
    {
        mid=lo+(hi-lo)/2;
        int nr0=min(numara(mid, 2), numara(mid, 5));
        if(nr0==p)
        {
            res=mid;
            hi=mid-1;
        }
        else if(nr0<p)
            lo=mid+1;
        else
            hi=mid-1;
    }
    return res;
}

int main()
{
    fi>>p;
    fo<<bSearch();
    fi.close();
    fo.close();
    return 0;
}