-
Notifications
You must be signed in to change notification settings - Fork 0
/
A.cpp
40 lines (38 loc) · 888 Bytes
/
A.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
#include <bits/stdc++.h>
using namespace std;
void solve() {
int q;
int64_t p, ans = 0;
cin >> p >> q;
if (p % q)
ans = p;
else {
vector<int> div;
int64_t tmp = q;
if (tmp % 2 == 0) {
while (!(tmp % 2)) tmp /= 2;
div.emplace_back(2);
}
for (int i = 3; i*i <= tmp; i += 2)
if (!(tmp % i)) {
while (!(tmp % i)) tmp /= i;
div.emplace_back(i);
}
if (tmp > 1) div.emplace_back(tmp);
for (auto& i: div) {
tmp = p;
while (!(tmp % q)) tmp /= i;
ans = max(ans, tmp);
}
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
while (t--) {
solve();
}
}