You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.6 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
2 years ago
#define int long long
#define endl "\n"
2 years ago
const int N = 50010;
2 years ago
// 线性筛法求莫比乌斯函数(枚举约数)
2 years ago
int mu[N], sum[N]; // 莫比乌斯函数的前缀和
int primes[N], cnt;
bool st[N];
2 years ago
void get_mobius(int n) {
2 years ago
mu[1] = 1;
for (int i = 2; i <= n; i++) {
if (!st[i]) {
primes[cnt++] = i;
2 years ago
mu[i] = -1;
2 years ago
}
for (int j = 0; primes[j] * i <= n; j++) {
int t = primes[j] * i;
2 years ago
st[t] = true;
2 years ago
if (i % primes[j] == 0) {
mu[t] = 0;
break;
}
mu[t] = -mu[i];
}
}
// 维护莫比乌斯函数前缀和
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + mu[i];
}
2 years ago
signed main() {
// 筛法求莫比乌斯函数
2 years ago
get_mobius(N - 1);
int T;
cin >> T;
while (T--) {
int a, b, d;
cin >> a >> b >> d;
2 years ago
// 套路啊满满的套路直接先用最大公约数a/gcd(a,b)=a',b/gcd(a,b)=b',映射到a',b'
2 years ago
a /= d, b /= d;
// n为 min(a', b')
int n = min(a, b);
2 years ago
int res = 0;
2 years ago
// l r, 是每一段的左右边界
// 每次只能取较小的那个上界作为这一段的右端点r
// 然后下次迭代时下一段的左端点就是r + 1
2 years ago
for (int l = 1, r; l <= n; l = r + 1) { // 分块大法
2 years ago
r = min(n, min(a / (a / l), b / (b / l)));
2 years ago
res += (sum[r] - sum[l - 1]) * (a / l) * (b / l);
2 years ago
}
2 years ago
cout << res << endl;
2 years ago
}
}