# HDU4947GCD Array（莫比乌斯反演+树状数组）

## 题解

orz ljz

$v\times [\gcd(i,n)=d]=v\times [\gcd(i/d,n/d)=1]=v\times \sum_{p|{i\over d},p|{n\over d}}\mu(p)$

//minamoto
#include<bits/stdc++.h>
#define R register
#define pb push_back
#define inline __inline__ __attribute__((always_inline))
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
R int res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
const int N=2e5+5;
typedef long long ll;
vector<int>vec[N];int mu[N],p[N],vis[N],n,q,m,T;ll c[N],f[N];
inline void add(R int x,R int y){for(;x<=n;x+=x&-x)c[x]+=y;}
inline ll query(R int x){R ll res=0;for(;x;x-=x&-x)res+=c[x];return res;}
void init(int n=N-5){
mu[1]=1;
fp(i,2,n){
if(!vis[i])p[++m]=i,mu[i]=-1;
for(R int j=1;j<=m&&1ll*i*p[j]<=n;++j){
vis[i*p[j]]=1;
if(i%p[j]==0)break;
mu[i*p[j]]=-mu[i];
}
}
fp(i,1,n)if(mu[i])for(R int j=i;j<=n;j+=i)vec[j].pb(i);
}
int main(){
//	freopen("testdata.in","r",stdin);
int lim=400;init();
fp(i,1,n)c[i]=f[i]=0;
printf("Case #%d:\n",++T);
for(int op,x,d,v,tmp;m;--m){
if(op&1){
fp(i,0,vec[x/d].size()-1){
tmp=vec[x/d][i]*d;
}
}else{
ll las=0,now=0,res=0;
fp(i,1,min(lim,x))res+=1ll*x/i*f[i],las+=f[i];
for(R int l=lim+1,r;l<=x;l=r+1)r=x/(x/l),now=query(r),res+=x/l*(now-las),las=now;
printf("%lld\n",res);
}
}
}
return 0;
}

posted @ 2019-05-21 17:59  bztMinamoto  阅读(464)  评论(0编辑  收藏  举报
Live2D