# 【BZOJ 2301】problem b

#### 【分析】

$f(a, b)=\sum_{i=1}^a\sum_{j=1}^b[gcd(i,j)=k]$

$\sum_{i=1}^{\lfloor\frac{a}{k}\rfloor}\sum_{j=1}^{\lfloor\frac{b}{k}\rfloor}[gcd(i,j)=1]$
$\sum_{d|n}\mu(d)=[n=1]$
$\sum_{i=1}^{\lfloor\frac{a}{k}\rfloor}\sum_{j=1}^{\lfloor\frac{b}{k}\rfloor}\sum_{d|gcd(i,j)}\mu(d)$

$\sum_{d=1}^{\lfloor \frac{max(a,b)}{k}\rfloor}\lfloor\frac{a}{kd}\rfloor\lfloor\frac{b}{kd}\rfloor$

#### 【代码】

#include<bits/stdc++.h>
#pragma GCC optimize (3)
#define ll long long
using namespace std;
const int mn = 50005;
int mu[mn];
bool vis[mn];
int pri[mn], cnt, k;
inline void init(int n)
{
mu[1] = 1;
for(int i = 2; i <= n; i++)
{
if(!vis[i]) pri[++cnt] = i, mu[i] = -1;
for(int j = 1; j <= cnt && pri[j] * i <= n; j++)
{
vis[i * pri[j]] = 1;
if(i % pri[j] == 0) {mu[pri[j] * i] = 0; break;}
else mu[pri[j] * i] = -mu[i];
}
}
for(int i = 2; i <= n; i++)
mu[i] += mu[i - 1];
}
inline ll calc(int x, int y)
{
x /= k, y /= k;
if(x > y) swap(x, y);
ll ret = 0;
for(int i = 1, las; i <= x; i = las + 1)
{
las = min(x / (x / i), y / (y / i));
ret += 1ll * (mu[las] - mu[i - 1]) * (x / i) * (y / i);
}
return ret;
}
int main()
{
int a, b, c, d, T;
scanf("%d", &T), init(mn - 5);
while(T--)
{
scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
printf("%lld\n", calc(b, d) - calc(a-1, d) - calc(b, c - 1) + calc(a-1, c-1));
}
}


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