Pagini recente » Cod sursa (job #3352046) | Cod sursa (job #2836833) | Cod sursa (job #97714) | Cod sursa (job #3320032) | Cod sursa (job #3330405)
#include <fstream>
#include <vector>
#define fi first
#define se second
using namespace std;
ifstream fin("gfact.in");
ofstream fout("gfact.out");
typedef long long ll;
vector <pair<ll,ll>> divs;
bool exceedsexp(ll val,ll prim,ll maxi)
{
int res=0;
ll prod=prim;
while(prod<=val)
{
res+=val/prod;
if(maxi<=res)
return false;
prod*=prim;
}
return true;
}
bool check(ll val)
{
//val! divizibil p^q, deci prim^(exp*q) deci exp(val!,prim)>=exp*q
for(auto p:divs)
if(exceedsexp(val,p.fi,p.se))
return false;
return true;
}
int main()
{
ll d=2;
ll p,q;
fin>>p>>q;
while(p>1)
{
int exp=0;
while(p%d==0)
exp++,p/=d;
if(exp)
divs.push_back({d,exp*q});
d++;
if(d*d>=p)
d=p;
}
ll st=0,dr=1e10,mid,ans=0;
while(st<=dr)
{
mid=(st+dr)/2;
if(check(mid))
{
ans=mid;
dr=mid-1;
}
else
st=mid+1;
}
fout<<ans;
return 0;
}