Pagini recente » Cod sursa (job #117083) | Cod sursa (job #43413) | Cod sursa (job #3199640) | Cod sursa (job #70745) | Cod sursa (job #3204446)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("gfact.in");
ofstream fout("gfact.out");
vector<pair<int, int>> fact;
void initFact(int x)
{
//fact.clear();
int exp=0;
while(x%2==0)
{
exp++;
x/=2;
}
if(exp>0)
fact.push_back(make_pair(2, exp));
for(int d=3; d*d<=x; d+=2)
{
exp=0;
while(x%d==0)
{
exp++;
x/=d;
}
if(exp>0)
fact.push_back(make_pair(d, exp));
}
if(x>1)
fact.push_back(make_pair(x, 1));
}
bool verifDiv(int n)
{
for(unsigned int i=0; i<fact.size(); i++)
{
int p=fact[i].first,cnt=0;
while(p<=n)
{
cnt+=n/p;
p*=fact[i].first;
if(cnt>=fact[i].second)
break;
}
if(cnt<fact[i].second)
return 0;
}
return 1;
}
int cb()
{
int st=1,dr=1078125000,mij,rez=-1;
while(st<=dr)
{
mij=(st+dr)/2;
if(verifDiv(mij))
{
rez=mij;
dr=mij-1;
}
else st=mij+1;
}
return rez;
}
int main()
{
int p,q;
fin>>p>>q;
initFact(p);
for(unsigned int i=0; i<fact.size(); i++)
{
fact[i].second*=q;
//cout<<fact[i].first<<' '<<fact[i].second<<'\n';
}
fout<<cb();
return 0;
}