Pagini recente » Cod sursa (job #1537689) | Cod sursa (job #2702180) | Cod sursa (job #2027782) | Cod sursa (job #2367420) | Cod sursa (job #1734260)
#include <fstream>
#include <math.h>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define ll long long
#define llu long long unsigned
#define pb push_back
#define mp make_pair
string problemName = "gfact";
string inFile = problemName+".in";
string outFile = problemName+".out";
ifstream fin(inFile.c_str());
ofstream fout(outFile.c_str());
const long long MAX = 1e17;
unordered_map <int, int> need;
bool check(ll x, int y, int target){
ll i;
ll cnt = 0;
for(i = y;i <= x;i *= y){
cnt += x/i;
if(cnt >= target){
return true;
}
}
return false;
}
ll binarySearch(ll st, ll dr, int x, int target){
ll mid = (st+dr)/2;
ll ret = 0;
while(st <= dr){
if(check(mid*x, x, target)){
ret = mid;
dr = mid-1;
}else{
st = mid+1;
}
mid = (st+dr)/2;
}
return ret*x;
}
void desc(int p, int q){
int i,aux,c;
c = 0;
while(p%2 == 0){
c++;
p /= 2;
}
if(c){
need[2] = c*q;
}
for(i = 3;i*i <= p;i += 2){
if(p%i == 0){
c = 0;
while(p%i == 0){
c++;
p /= i;
}
need[i] = c*q;
}
}
if(p > 1){
need[p] = q;
}
}
int main(){
int p,q,i,aux,c,target,x;
fin>>p>>q;
desc(p, q);
ll mn = 0;
for(auto it : need){
mn = max(mn, binarySearch(1LL, MAX/it.first, it.first, it.second));
}
fout<<mn;
return 0;
}