题目:
http://codeforces.com/contest/1445/problem/C
给定 x , y x,y x,y,找到最大的 z z z使得 z ∣ x , y ∤ z z|x,y\nmid z z∣x,y∤z。 1 ≤ x ≤ 1 0 18 , 2 ≤ y ≤ 1 0 9 1\le x\le 10^{18},2\le y\le 10^9 1≤x≤1018,2≤y≤109
思路:
- y ∤ x y\nmid x y∤x,答案为 x x x
- y ∣ x y|x y∣x,假设 x = p 1 a 1 p 2 a 2 p 3 a 3 p 4 a 4 x=p_1^{a_1}p_2^{a_2}p_3^{a_3}p_4^{a_4} x=p1a1p2a2p3a3p4a4, y = p 2 b 2 p 3 b 3 y=p_2^{b_2}p_3^{b_3} y=p2b2p3b3,则答案为 m a x { p 1 a 1 p 2 b 2 − 1 p 3 a 3 p 4 a 4 , p 1 a 1 p 2 a 2 p 3 b 3 − 1 p 4 a 4 } max\{p_1^{a_1}p_2^{b_2-1}p_3^{a_3}p_4^{a_4},p_1^{a_1}p_2^{a_2}p_3^{b_3-1}p_4^{a_4}\} max{p1a1p2b2−1p3a3p4a4,p1a1p2a2p3b3−1p4a4}
#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct node {
ll val;
int cnt;
node(ll x,int y):val(x),cnt(y) {}
};
ll qpow(ll a,ll b) {
ll res=1;
while(b) {
if(b&1)
res*=a;
a*=a;
b>>=1;
}
return res;
}
int main() {
int T;
cin>>T;
while(T--) {
ll x,y,z,ans=0;
vector<node>w;
cin>>x>>y;
if(x%y) {
cout<<x<<endl;
continue;
}
z=y;
for(ll i=2; i*i<=y; i++) {
if(z%i==0) {
int num=0;
while(z%i==0) {
num++;
z/=i;
}
w.push_back(node(i,num));
}
}
if(z!=1)
w.push_back(node(z,1));
for(int i=0; i<w.size(); i++) {
ll tmp=w[i].val,tmp1=x;
while(tmp1%tmp==0)
tmp1/=tmp;
ans=max(ans,tmp1*qpow(tmp,w[i].cnt-1));
}
cout<<ans<<endl;
}
return 0;
}