Pagini recente » Cod sursa (job #60108) | Cod sursa (job #2443194) | Borderou de evaluare (job #2789968) | Cod sursa (job #1987160) | Cod sursa (job #1003245)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
long long P, Q;
long long Ans;
vector<pair<long long, long long> > F;
long long GetPow(long long Fact, long long Factorial)
{
long long Sum = 0;
for(long long i = 1LL * Fact; i <= Factorial; i *= Fact)
Sum += Factorial / i;
return Sum;
}
bool OK(long long Fact)
{
for(vector<pair<long long, long long> > :: iterator it = F.begin(); it != F.end(); ++ it)
if(GetPow(it -> first, Fact) < it -> second)
return 0;
return 1;
}
long long BS()
{
long long Left = 1, Right = (1LL << 60), Mid, Now;
while(Left <= Right)
{
Mid = Left + (Right - Left) / 2;
if(OK(Mid)) Now = Mid, Right = Mid - 1;
else Left = Mid + 1;
}
return Now;
}
int main()
{
ifstream fin("gfact.in");
ofstream fout("gfact.out");
fin >> P >> Q;
for(long long i = 2; 1LL * i * i <= P; ++ i)
if(P % i == 0)
{
long long Exp = 0;
while(P % i == 0) Exp ++, P /= i;
F.push_back(make_pair(i, P * Q));
}
if(P > 1)
F.push_back(make_pair(P, Q));
fout << BS() << "\n";
return 0;
}