Cod sursa(job #1339839)

Utilizator ialexia_ioanaAlexia Ichim ialexia_ioana Data 11 februarie 2015 11:02:28
Problema GFact Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <cstdio>
//http://www.e-olimp.com/problems

int f[15], e[15];

inline long long legendre(long long n, int p)
{
    long long s=0;
    while(n)
    {
        s+=n/p;
        n/=p;
    }
    return s;
}
inline int bs(int poz)
{
    long long st, dr, med, nr, last=-1;
    st=1;
    dr=(1LL<<60)-1;
    while(st<=dr)
    {
        med=st+((dr-st)>>1);
        nr=legendre(med, f[poz]);
        if (nr>=e[poz])
            dr=med-1, last=med;
        else
            st=med+1;
    }
    return last;
}

using namespace std;

int main()
{
    freopen("gfact.in", "r", stdin);
    freopen("gfact.out", "w", stdout);
    int p, q, d, ex, nf=0, i;
    long long b, max;
    scanf("%d%d", &p, &q);
    d=2;
    while(1LL*d*d<=p && p>1)
    {
        ex=0;
        while(p%d==0)
            ex++, p/=d;
        if (ex)
            f[++nf]=d, e[nf]=ex*q;
        d++;
    }
    if (p>1)
        f[++nf]=p, e[nf]=q;
    b=1;
    max=0;
    for(i=1; i<=nf; i++)
    {
        b=bs(i);
        if (b>max)
            max=b;
    }
    printf("%lld\n", max);
    return 0;
}