求 ∑ i = 1 n ∑ j = 1 m d ( i j ) \sum_{i = 1} ^{n} \sum_{j = 1} ^{m} d(ij) ∑i=1n∑j=1md(ij)
= ∑ i = 1 n ∑ j = 1 m ∑ x ∣ i ∑ y ∣ j g c d ( x , y ) = = 1 = \sum_{i = 1} ^{n} \sum_{j = 1} ^{m} \sum_{x \mid i} \sum_{y \mid j} gcd(x, y) == 1 =i=1∑nj=1∑mx∣i∑y∣j∑gcd(x,y)==1
改成枚举 x , y x, y x,y,
= ∑ i = 1 n ∑ j = 1 m ∑ i ∣ x ∑ j ∣ y g c d ( i , j ) = = 1 = \sum_{i = 1} ^{n} \sum_{j = 1} ^{m} \sum _{i \mid x} \sum_{j \mid y} gcd(i, j) == 1 =i=1∑nj=1∑mi∣x∑j∣y∑gcd(i,j)==1
= ∑ i = 1 n ∑ j = 1 m ⌊ n i ⌋ ⌊ m j ⌋ ( g c d ( i , j ) = = 1 ) = \sum_{i = 1} ^{n} \sum_{j = 1} ^{m} \lfloor\frac{n}{i}\rfloor \lfloor\frac{m}{j}\rfloor (gcd(i, j) == 1) =i=1∑nj=1∑m⌊in⌋⌊jm⌋(gcd(i,j)==1)
= ∑ i = 1 n ∑ j = 1 m ⌊ n i ⌋ ⌊ m j ⌋ ∑ d ∣ ( g c d ( i , j ) ) μ ( d ) = \sum_{i = 1} ^{n} \sum_{j = 1} ^{m} \lfloor\frac{n}{i}\rfloor \lfloor\frac{m}{j}\rfloor \sum_{d \mid (gcd(i, j))} \mu(d) =i=1∑nj=1∑m⌊in⌋⌊jm⌋d∣(gcd(i,j))∑μ(d)
∑ d = 1 n μ ( d ) ∑ i = 1 n d ⌊ n d i ⌋ ∑ j = 1 m d ⌊ m d j ⌋ \sum_{d = 1} ^{n} \mu(d)\sum_{i = 1} ^{\frac{n}{d}} \lfloor\frac{n}{di}\rfloor \sum_{j = 1} ^{\frac{m}{d}} \lfloor \frac{m}{dj}\rfloor d=1∑nμ(d)i=1∑dn⌊din⌋j=1∑dm⌊djm⌋
最后我们只要预处理一下所有 n d m d \frac{n}{d} \frac{m}{d} dndm的整除分块即可,整体复杂度 n n n \sqrt n nn
/*
Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include
#define mp make_pair
#define pb push_back
#define endl '\n'
#define mid (l + r >> 1)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define ls rt << 1
#define rs rt << 1 | 1
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;
inline ll read() {
ll f = 1, x = 0;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f * x;
}
const int N = 5e4 + 10;
bool st[N];
vector<int> prime;
int n, m, mu[N];
ll sum[N];
void mobius() {
st[0] = st[1] = mu[1] = 1;
for(int i = 2; i < N; i++) {
if(!st[i]) {
prime.pb(i);
mu[i] = -1;
}
for(int j = 0; j < prime.size() && i * prime[j] < N; j++) {
st[i * prime[j]] = 1;
if(i % prime[j] == 0) break;
mu[i * prime[j]] = -mu[i];
}
}
for(int i = 1; i < N; i++) {
mu[i] += mu[i - 1];
for(ll l = 1, r; l <= i; l = r + 1) {
r = i / (i / l);
sum[i] += (i / l) * (r - l + 1);
}
}
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
mobius();
int T = read();
while(T--) {
ll n = read(), m = read(), ans = 0;
if(n > m) swap(n, m);
for(ll l = 1, r; l <= n; l = r + 1) {
r = min(n / (n / l), m / (m / l));
ans += 1ll * (mu[r] - mu[l - 1]) * sum[n / l] * sum[m / l];
}
printf("%lld\n", ans);
}
return 0;
}