# 题解【bzoj2301 [HAOI2011]Problem b】

### Solution

$\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}[\gcd(i,j)=k]$

$k$ 提出来可得

$\sum\limits_{i=1}^{\lfloor\frac{n}{k}\rfloor}\sum\limits_{j=1}^{\lfloor\frac{m}{k}\rfloor}[\gcd(i, j)=1]$

$\sum\limits_{i=1}^{\lfloor\frac{n}{k}\rfloor}\sum\limits_{j=1}^{\lfloor\frac{m}{k}\rfloor}\sum\limits_{d|\gcd(i,j)}\mu(d)$

$d$ 搞到前面来，得到

$\sum\limits_{d=1}^{\lfloor \frac{n}{k} \rfloor} \mu(d)\lfloor \frac{n}{kd} \rfloor\lfloor \frac{m}{kd} \rfloor$

### Code

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 50000;
int k, cnt, p[N + 50], mu[N + 50], flag[N + 50], sum[N + 50];
inline void prework() {
flag[1] = mu[1] = 1;
for(int i = 2; i <= N; i++) {
if(!flag[i]) {
p[++cnt] = i; mu[i] = -1;
} for(int j = 1; j <= cnt && i * p[j] <= N; j++) {
flag[i * p[j]] = 1;
if(i % p[j] == 0) {
mu[i * p[j]] = 0; break;
} mu[i * p[j]] = mu[i] * -1;
}
} for(int i = 1; i <= N; i++) sum[i] = sum[i - 1] + mu[i];
}
inline ll calc(int n, int m) {
if(n > m) swap(n, m); ll ret = 0;
for(int l = 1, r; l <= n / k; l = r + 1) {
r = min(n / (n / l), m / (m / l));
ret += 1ll * (n / (l * k)) * (m / (l * k)) * (sum[r] - sum[l - 1]);
} return ret;
}
int main() {
int T; prework();
scanf("%d", &T);
while(T--) {
int a, b, c, d;
scanf("%d %d %d %d %d", &a, &b, &c, &d, &k);
printf("%lld\n", calc(a - 1, c - 1) - calc(b, c - 1) - calc(d, a - 1) + calc(b, d));
}
return 0;
}

posted @ 2018-12-16 19:07  AcFunction  阅读(265)  评论(0编辑  收藏