# bzoj2301 [HAOI2011]Problem b【莫比乌斯反演 分块】

#include <cstdio>
#include <algorithm>

const int maxn = 50005;

int n, a, b, c, d, k, mu[maxn] = {0, 1}, prime[maxn], tot, s[maxn];
char book[maxn];

inline long long slove(int lmtx, int lmty) {
lmtx /= k;
lmty /= k;
int lmt = std::min(lmtx, lmty), last;
long long rt = 0;
for (int i = 1; i <= lmt; i = last + 1) {
last = std::min(lmtx / (lmtx / i), lmty / (lmty / i));
rt += (long long)(lmtx / i) * (lmty / i) * (s[last] - s[i - 1]);
}
return rt;
}

int main(void) {
scanf("%d", &n);
for (int i = 2; i < maxn; ++i) {
if (!book[i]) {
prime[tot++] = i;
mu[i] = -1;
}
for (int j = 0; j < tot; ++j) {
if (i * prime[j] > maxn) {
break;
}
book[i * prime[j]] = 1;
if (i % prime[j] == 0) {
break;
}
else {
mu[i * prime[j]] = -mu[i];
}
}
}
for (int i = 1; i < maxn; ++i) {
s[i] = s[i - 1] + mu[i];
}
while (n--) {
scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
printf("%lld\n", slove(b, d) - slove(a - 1, d) - slove(b, c - 1) + slove(a - 1, c - 1));
}
return 0;
}

posted @ 2017-02-06 22:17  ciao_sora  阅读(...)  评论(...编辑  收藏