题解
- 由题意可知只有
是减小总重量的,而
是
交换因子,所以
- 利用唯一分解定理、快速幂加速程序
AC-Code
#include<bits/stdc++.h>
#define
using namespace std;
vector<pair<ll, int>> prime_factors(ll n) {
vector<pair<ll, int>> f;
int k = sqrt(n) + 1;
ll p=0; int q;
for (ll i = 2; i <= k; ++i) {
q = 0; if (n % i == 0) p = i;
while (n % i == 0) ++q, n /= i;
if (p) f.push_back(make_pair(p, q));
}
if (n > 1) f.push_back(make_pair(n, 1));
return f;
}
ll q_pow(ll a, ll n) {
ll res = 1;
while (n > 0) {
if (n & 1) res = res * a;
a *= a;
n >>= 1;
}
return res;
}
int main() {
int T; cin >> T;
while (T--) {
ll a, b; cin >> a >> b;
ll ans = a * b;
vector<pair<ll, int>> f = prime_factors(ans);
for (auto i : f) {
if (i.second % 2 == 0) ans /= q_pow(i.first, i.second);
}
cout << ans << endl;
}
}