思路
话不多说,直接推公式吧
$$\sum_{i = 1} ^{n} \sum_{j = 1} ^{m} [gcd(i, j) == d]$$
$$\sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{m}{d}} [gcd(i, j) == 1]$$
$$n = \frac{n}{d}, m = \frac{m}{d}$$
$$\sum_{i = 1} ^{n} \sum_{j = 1} ^{m} \sum_{k \mid gcd(i, j)} \mu(k)$$
$$\sum_{k = 1} ^{n} \mu(k) \sum_{i = 1} ^{\frac{n}{k}} \sum_{j = 1} ^{\frac{m}{k}}$$
到这里就结束了,只要通过$T \sqrt n$的复杂度即可得到答案
代码
/*
Author : lifehappy
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e4 + 10;
int prime[N], mu[N], cnt;
bool st[N];
void init() {
mu[1] = 1;
for(int i = 2; i < N; i++) {
if(!st[i]) {
prime[++cnt] = i;
mu[i] = -1;
}
for(int j = 1; j <= cnt && 1ll * 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];
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T;
init();
scanf("%d", &T);
while(T--) {
int n, m, d;
scanf("%d %d %d", &n, &m, &d);
n /= d, m /= d;
if(n > m) swap(n, m);
ll ans = 0;
for(int l = 1, r; l <= n; l = r + 1) {
r = min(n / (n / l), m / (m / l));
ans += 1ll * (mu[r] - mu[l - 1]) * (n / l) * (m / l);
}
printf("%lld\n", ans);
}
return 0;
}