Pagini recente » Cod sursa (job #686390) | Cod sursa (job #622653) | Cod sursa (job #7106) | Cod sursa (job #2297429) | Cod sursa (job #2086563)
#include <fstream>
using namespace std;
const int ND=10, L=45;
int d[ND], e[ND], nd, p, q;
ifstream in("gfact.in");
ofstream out("gfact.out");
void desc(int n)
{
int i=2;
while(i*i<=n)
{
if(n%i==0)
{
d[nd]=i;
while(n%i==0)
{
e[nd]++;
n/=i;
}
nd++;
}
i++;
}
if(n!=1)
{
d[nd]=n;
e[nd]=1;
nd++;
}
}
long long putere(long long x, int y)
{
long long nr=0;
while(x>=y)
{
nr+=x/y;
x/=y;
}
return nr;
}
bool divizibil(long long n)
{
for(int i=0; i<nd; i++)
{
if(putere(n, d[i])< e[i]*q) return false;
}
return true;
}
int main()
{
long long r;
long long pas;
in>>p>>q;
desc(p);
pas=1LL<<L;
r=0;
while(pas!=0)
{
if(!divizibil(r+pas)) r+=pas;
pas/=2;
}
r++;
out<<r;
return 0;
}