Cod sursa(job #1714448)

Utilizator lucametehauDart Monkey lucametehau Data 8 iunie 2016 11:29:46
Problema GFact Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <fstream>

using namespace std;
ifstream cin("gfact.in");
ofstream cout("gfact.out");
long long f[30],pt[30],k;
long long p,q,j,pas,d,c;
long long legendre(long long f,long long n)
{
    long long pt=0;
    while(n>=c)
    {
        pt+=n/c;
        n/=c;
    }
    return pt;
}
bool isOK(long long n)
{
    for(long long i=1;i<=k;i++)
        if(legendre(f[i],n)<pt[i]*q)
            return 0;
    return 1;
}
int main()
{
    cin>>p>>q;
    pas=4611686018427387904;
    c=p;
    for(d=2;d*d<=c;d++)
    {
        if(c%d==0)
        {
            k++;
            f[k]=d;
        }
        while(c%d==0)
        {
            c/=d;
            pt[k]++;
        }
    }
    if(c>1)
    {
        k++;
        f[k]=c;
        pt[k]=1;
    }
    while(pas)
    {
        if(isOK(j+pas)==0)
            j+=pas;
        pas/=2;
    }
    cout<<j+1;
    return 0;
}