Cod sursa(job #2356439)

Utilizator MoldovanAndrei1Moldovan Andrei MoldovanAndrei1 Data 26 februarie 2019 18:04:02
Problema GFact Scor 75
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <bits/stdc++.h>
using namespace std;
int main()
{
    freopen("gfact.in","r",stdin);
    freopen("gfact.out","w",stdout);
    int st=1,dr=15000000,p,q;
    scanf("%d%d",&p,&q);
    int d=2,e=0;
    int m1=0;
    while(d*d<=p)
    {
        st=1,dr=150000000;
        e=0;
        while(p%d==0)
        {
            p/=d;
            e++;
        }
        if(e==0)
        {
            d++;
            continue;
        }
        int q1=q*e;
    while(st<=dr)
    {
        int med=(st+dr)/2;
        int x=0,p1=1,pl=med;
        while(pl)
        {
            x+=pl/d;
            pl/=d;
        }
        if(x>q1)dr=med-1;
        if(x==q1)
        {
            m1=max(m1,med/d*d);
            break;
        }
        if(x<q1)st=med+1;
    }
    d++;

    }
    printf("%d\n",max(m1,p));
    return 0;
}