# BZOJ 2301 Problem b（莫比乌斯反演+分块优化）

code：

 1 #include <cstdio>
2 #include <cstring>
3 #include <algorithm>
4 using namespace std;
5 typedef long long LL;
6 const int MAXN = 50005;
7
8 bool check[MAXN];
9 int primes[MAXN];
10 int mu[MAXN];
11 int sum[MAXN];
12 LL a, b, c, d, k;
13
14 void moblus()
15 {
16     memset(check, false, sizeof(check));
17     mu[1] = 1;
18     int cnt = 0;
19     for (int i = 2; i < MAXN; ++i) {
20         if (!check[i]) {
21             primes[cnt++] = i;
22             mu[i] = -1;
23         }
24         for (int j = 0; j < cnt; ++j) {
25             if (i * primes[j] > MAXN) break;
26             check[i * primes[j]] = true;
27             if (i % primes[j] == 0) {
28                 mu[i * primes[j]] = 0;
29                 break;
30             } else {
31                 mu[i * primes[j]] = -mu[i];
32             }
33         }
34     }
35     sum[0] = 0;
36     for (int i = 1; i < MAXN; ++i) {
37         sum[i] = sum[i - 1] + mu[i];
38     }
39 }
40
41 LL cal(LL n, LL m)
42 {
43     if (n > m) swap(n, m);
44     n /= k;
45     m /= k;
46     LL ret = 0;
47     for (int i = 1, la = 0; i <= n; i = la + 1) {
48         la = min(n/(n/i), m/(m/i));
49         ret += (n / i) * (m / i) * (sum[la] - sum[i - 1]);
50     }
51     return ret;
52 }
53
54 int main()
55 {
56     moblus();
57     int nCase;
58     scanf("%d", &nCase);
59     while (nCase--) {
60         scanf("%lld %lld %lld %lld %lld", &a, &b, &c, &d, &k);
61         LL ans = cal(b, d) - cal(a - 1, d) - cal(b, c - 1) + cal(a - 1, c - 1);
62         printf("%lld\n", ans);
63     }
64     return 0;
65 }
posted @ 2015-09-09 09:37  jasaiq  阅读(...)  评论(...编辑  收藏