# bzoj2301 HAOI2011 Problem b

#### 题面

2
2 5 1 5 1
1 5 1 5 2

14
3

##### HINT

100%的数据满足：1≤n≤50000，1≤a≤b≤50000，1≤c≤d≤50000，1≤k≤50000

#### 题解

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

$ans = f (b, d) - f (a - 1, d) - f (b, c - 1) + f (a - 1, c - 1)$

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

$x = \lfloor \frac n k \rfloor$$y = \lfloor \frac m k \rfloor$

#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
const int maxn = 5e4 + 10 ;
int mu[maxn], prime[maxn], tot ;
bool vis[maxn] ;
void init() {
mu[1] = 1 ;
for (int i = 2; i < maxn; i ++) {
if (!vis[i]) prime[++ tot] = i, mu[i] = -1 ;
for (int j = 1; j <= tot && i * prime[j] < maxn; j ++) {
vis[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] ;
}
inline int cal (int x, int y) {
if (x > y) swap (x, y) ;
int res = 0 ;
for (int i = 1, nxt = 0; i <= x; i = nxt + 1) {
nxt = min (x / (x / i), y / (y / i)) ;
res += (x / i) * (y / i) * (mu[nxt] - mu[i - 1]) ;
}
return res ;
}
int main() {
init () ;
int T ;
cin >> 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 / k, d / k) - cal (b / k, (c - 1) / k) - cal ((a - 1) / k, d / k) + cal ((a - 1) / k, (c - 1) / k)) ;
}
return 0 ;
}


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客