# 【BZOJ2301】【HAOI2011】Problem b

【题目链接】

【思路要点】

• 补档博客，无题解。

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN	100005
int tot, prime[MAXN], f[MAXN], miu[MAXN], sum[MAXN];
void init() {
for (int i = 2; i < MAXN; i++) {
if (f[i] == 0) {
prime[++tot] = i;
f[i] = i;
miu[i] = -1;
}
for (int j = 1; j <= tot && prime[j] <= f[i]; j++) {
int tmp = i * prime[j];
if (tmp >= MAXN) break;
f[tmp] = prime[j];
if (prime[j] == f[i]) miu[tmp] = 0;
else miu[tmp] = -miu[i];
}
}
miu[1] = 1;
for (int i = 1; i < MAXN; i++)
sum[i] = sum[i - 1] + miu[i];
}
long long solve(long long x, long long y) {
int i = 1;
long long ans = 0;
while (i <= x && i <= y) {
int next = x / (x / i);
int mext = y / (y / i);
if (next <= mext) {
ans += (sum[next] - sum[i - 1]) * (x / i) * (y / i);
i = next + 1;
} else {
ans += (sum[mext] - sum[i - 1]) * (x / i) * (y / i);
i = mext + 1;
}
}
return ans;
}
int main() {
init();
int T;
scanf("%d", &T);
while (T--) {
long long x, y, a, b, z;
scanf("%lld%lld%lld%lld%lld", &x, &a, &y, &b, &z);
x = (x - 1) / z; y = (y - 1) / z; a /= z; b /= z;
printf("%lld\n", solve(a, b) - solve(a, y) - solve(x, b) + solve(x, y));
}
return 0;
}

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客