RT 萌新刚学OI,RE了
求查错
#include <stdio.h>
#include <stdlib.h>
typedef long long ll;
template <typename T>
inline void read(T &x) {
x = 0; int f = 1; char ch = getchar();
while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
x *= f;
}
inline ll mi_n(ll x, ll y) {return x < y ? x : y;}
const int N = 50;
ll a[N], a1[N], a2[N], tot;
main() {
int t; read(t);
while (t --) {
ll p, q; read(p); read(q);
if (p % q) {printf ("%lld\n", p); continue;}
ll pi = p, qi = q;
for (register int i = 2; i * i <= q; i ++) {
if (qi % i == 0) {
a[++ tot] = i, a1[tot] = a2[tot] = 1;
while (pi % i == 0) a1[tot] *= i, pi /= i;
while (qi % i == 0) a2[tot] *= i, qi /= i;
}
}
if (qi > 1) {
a[++ tot] = qi, a1[tot] = a2[tot] = 1;
a2[tot] = a1[tot] = qi;
qi = 1;
}
ll minn = 1e18;
for (register int i = 1; i <= tot; i ++) {
minn = mi_n(minn, a1[i] / a2[i] * a[i]);
}
printf ("%lld\n", p / minn);
}
system("pause");
return 0;
}