https://blog.csdn.net/qq_42555009/article/details/87253503
#include <bits/stdc++.h> using namespace std; const int N=50005; int mu[N],pr[N],tot,vis[N],sum[N]; typedef long long ll; void init() { mu[1] = 1; for (int i = 2; i < N; i++) { if (!vis[i]) { pr[++tot] = i; mu[i] = -1; } for (int j = 1; j <= tot && i * pr[j] < N; j++) { vis[i * pr[j]] = 1; if (i % pr[j] == 0) break; mu[i * pr[j]] = -mu[i]; } } for (int i = 1; i < N; i++) sum[i] = sum[i - 1] + mu[i]; } int main() { int _, b, d, k; scanf("%d", &_); init(); while (_--) { scanf("%d%d%d", &b, &d, &k); if (k == 0) { printf("0 "); continue; } ll ans = 0; b /= k; d /= k; for (int i = 1, nxt; i <= min(b, d); i = nxt + 1) { nxt = min(b / (b / i), d / (d / i)); ans += 1ll * (sum[nxt] - sum[i - 1]) * (b / i) * (d / i); } printf("%lld ", ans); } return 0; }