-
Notifications
You must be signed in to change notification settings - Fork 0
/
C.cpp
58 lines (56 loc) · 1.3 KB
/
C.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
long long power(long long x,long long y)
{
long long u=1;
for(long long i=0;i<y;i++)
u*=x;
return u;
}
int main()
{
fastio;
long long t,p,q,i,cnt,tmp,mn;
cin>>t;
while(t--)
{
mn=1000000000000000000;
cin>>p>>q;
if(p%q!=0)
cout<<p<<"\n";
else
{
vector<pair<long long,long long> > vect;
for(i=2;i*i<=q;i++)
{
if(q==1)
break;
cnt=0;
while(q%i==0)
{
cnt++;
q/=i;
}
if(cnt>0)
vect.push_back(make_pair(i,cnt));
}
if(q!=1)
vect.push_back(make_pair(q,1LL));
for(i=0;i<vect.size();i++)
{
tmp=p;
cnt=0;
while(tmp%vect[i].first==0)
{
cnt++;
tmp/=vect[i].first;
}
mn=min(mn,power(vect[i].first,cnt-vect[i].second+1));
//cout<<mn<<"\n";
}
cout<<p/mn<<"\n";
}
}
return 0;
}