# bzoj 2301 [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

a = (a-1)/k;
b = b/k;
c = (c-1)/k;
d = d/k;

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;

const int maxn = 1e5 + 10;
int p[maxn/10];
int flag[maxn];
int mu[maxn];
int cnt = 0;
int sum[maxn];

void init()
{
int i,j;
mu[1] = 1;
for(i=2;i<maxn;i++)
{
if(!flag[i])
{
p[cnt++] = i;
mu[i] = -1;
}
for(j=0;j<cnt&&p[j]*i<maxn;j++)
{
flag[p[j]*i] = 1;
if(i % p[j] == 0)
{
mu[p[j]*i] = 0;
break;
}
mu[p[j]*i] = -mu[i];
}

}
for(i=1;i<maxn;i++)
sum[i] = sum[i-1] + mu[i];
}

LL solve(int b,int d)
{
LL ans = 0;
for(int i=1,last = 0;i<=min(b,d);i=last+1)
{
last = min(b/(b/i),d/(d/i));
ans += (LL)(b/i)*(d/i)*(sum[last]-sum[i-1]);
}
return ans;
}
int main(void)
{
int T,a,b,c,d,k,i,j;
init();
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
a = (a-1)/k;
b = b/k;
c = (c-1)/k;
d = d/k;
LL ans = solve(b,d) - solve(a,d) - solve(c,b) + solve(a,c);
printf("%lld\n",ans);
}
return 0;
}


©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客