# bzoj2301（莫比乌斯反演）

## bzoj2301

### 分析

$F(d) = \sum_{k\mid d}f(k) => f(k) = \sum_{k\mid d}\mu(\frac d k)F(d)$
f(1) 即为答案。

### code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 10;
int not_prime[MAXN];
int prime[MAXN];
int mu[MAXN];
void getMu() {
mu[1] = 1;
int cnt = 0;
for(int i = 2; i < MAXN; i++) {
if(!not_prime[i]) {
prime[cnt++] = i;
mu[i] = -1;
}
for(int j = 0; i * prime[j] < MAXN; j++) {
not_prime[i * prime[j]] = 1;
if(i % prime[j] == 0) {
mu[i * prime[j]] = 0;
break;
}
mu[i * prime[j]] = -mu[i];
}
}
for(int i = 1; i < MAXN; i++) mu[i] += mu[i - 1]; // 前缀和
}
ll cal(int m, int n, int k) {
int last;
m /= k; n /= k;
ll s = 0;
for(int i = 1; i <= min(n, m); i = last + 1) {
last = min(n / (n / i), m / (m / i));
s += (ll)(mu[last] - mu[i - 1]) * (m / i) * (n / i);
}
return s;
}
int main() {
getMu();
int T;
scanf("%d", &T);
while(T--) {
int a, b, c, d, k;
scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
printf("%d\n", cal(b, d, k) - cal(a - 1, d, k) - cal(b, c - 1, k) + cal(a - 1, c - 1, k));
}
return 0;
}
posted @ 2017-06-12 22:49  ftae  阅读(...)  评论(...编辑  收藏