版权声明:本文为hzy原创文章,未经博主允许不可随意转载。 https://blog.csdn.net/Binary_Heap/article/details/82184929
题意
T组询问求,
题解
不妨设 .
然后整除分块做一下就行.
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int MAXN = 50010;
bool tag[MAXN];
int pr[MAXN], tot, mu[MAXN];
LL mus[MAXN];
void init(int n) {
tag[1] = true;
mus[1] = mu[1] = 1;
for(int i = 2; i <= n; i ++) {
if(!tag[i]) {
pr[++ tot] = i;
mu[i] = -1;
}
for(int j = 1; j <= tot && i * pr[j] <= n; j ++) {
tag[i * pr[j]] = true;
if(i % pr[j] == 0) {
mu[i * pr[j]] = 0;
break ;
}
mu[i * pr[j]] = - mu[i];
}
mus[i] = mus[i - 1] + mu[i];
}
}
LL calc(int a, int b, int d) {
if(a > b) a ^= b ^= a ^= b;
a /= d; b /= d;
LL ans = 0;
for(int i = 1, j; i <= a; i = j + 1) {
j = min(a / (a / i), b / (b / i));
ans += (mus[j] - mus[i - 1]) * (a / i) * (b / i);
}
return ans;
}
int main() {
init(50000);
int n, a, b, d;
for(scanf("%d", &n); n --; ) {
scanf("%d%d%d", &a, &b, &d);
printf("%lld\n", calc(a, b, d));
}
return 0;
}