Cod sursa(job #1327974)

Utilizator Darius15Darius Pop Darius15 Data 27 ianuarie 2015 18:29:03
Problema GFact Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.45 kb
#include <fstream>

using namespace std;
ifstream f("gfact.in");
ofstream g("gfact.out");
long long p,q,st,dr,m,pr,nr,best;
int main()
{
    f>>p>>q;
    if (p==1)
        g<<0;
    else{
    st=1,dr=(1ll<<61)-1;
    while(st<=dr)
    {
        m=(st+dr)/2;
        nr=0;
        for (pr=p;pr<=m;pr*=p)
         if ((m/p)<=pr) nr+=m/pr;
        if (nr>=q) best=m,dr=m-1;
        else st=m+1;
    }
    g<<best<<' ';
    }
    return 0;
}