Pagini recente » Cod sursa (job #408112) | Cod sursa (job #3213192) | Cod sursa (job #1581758) | Cod sursa (job #1143144) | Cod sursa (job #1155397)
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
ifstream fin ("gfact.in");
ofstream fout ("gfact.out");
typedef unsigned long long ull;
ull p, q;
vector <pair <ull, ull> > v;
bool go(ull x) {
for (vector <pair <ull, ull> > :: iterator it = v.begin(); it != v.end(); ++it) {
ull ans = 0, prod = it->first;
while (prod <= x) {
ans += x / prod;
prod *= it->first;
}
if (ans < it->second)
return 0;
}
return 1;
}
int main() {
fin >> p >> q;
ull aux = p;
if (p % 2 == 0) {
v.push_back (make_pair (2, 0));
while (p % 2 == 0) {
v.back().second += q;
p /= 2;
}
}
for (ull i = 3; i * i <= aux && p != 1; i += 2)
if (p % i == 0) {
v.push_back (make_pair(i, 0));
while (p % i == 0) {
v.back().second += q;
p /= i;
}
}
if (p != 1)
v.push_back (make_pair (p, q));
ull sol = 1LL << 62;
for (ull step = 1LL << 62; step; step >>= 1)
if (go (sol - step))
sol -= step;
fout << sol;
}