# bzoj 2301: [HAOI2011]Problem b 【莫比乌斯反演】

### 代码：

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<cmath>
#include<stack>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define Mn 50005
#define Mm 2000005
#define mod 1000000007
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define CPY(a,b) memcpy ((a), (b), sizeof((a)))
#define ul u<<1
#define ur (u<<1)|1
using namespace std;
typedef long long ll;
char c;
int ans=0,f=1;c=getchar();
while(c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9') {ans=ans*10+c-'0';c=getchar();}
return  ans*f;
}
int prime[Mn],tot=0,no[Mn];
int mo[Mn];int sum[Mn];
void mobius() {
mo[1]=1;
for(int i=2;i<=Mn;i++) {
if(!no[i]) {
prime[++tot]=i;
mo[i]=-1;
}
for(int j=1;prime[j]*i<=Mn;j++) {
no[prime[j]*i]=1;
if(i%prime[j]==0) {
mo[prime[j]*i]=0;
break;
}
mo[prime[j]*i]=-mo[i];
}
}
}

ll solve(int a,int b) {
ll re=0;
if(a>b) swap(a,b);
for(int i=1,last;i<=a;i=last+1) {
last=min(a/(a/i),b/(b/i));
re+=(ll)(sum[last]-sum[i-1])*(a/i)*(b/i);
}
return re;
}
int main() {
mobius();sum[0]=0;
for(int i=1;i<=Mn;i++)
sum[i]=sum[i-1]+mo[i];
while(t--) {
ll ans=solve(b/k,d/k)-solve((a-1)/k,d/k)-solve(b/k,(c-1)/k)+solve((a-1)/k,(c-1)/k);
printf("%lld\n",ans);
}
return 0;
}


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