codeforces-839D(莫比乌斯反演)
codeforces-839D Winter is here
设,则答案可以表示为
设,设
的倍数个数为
,则
。
所以有,反演得
。
#include<bits/stdc++.h> using namespace std; const int N=1e6+6; const int p=1e9+7; typedef long long ll; ll ksm(ll a,ll b){ll ans=1;for(;b>0;b>>=1,a=a*a%p)if(b&1)ans=ans*a%p;return ans;} short int mu[N]; int flag[N],F[N],f[N]; int n; void pre(){ mu[1]=1; for(int i=1;i<N;i++) for(int j=i+i;j<N;j+=i)mu[j]-=mu[i]; } int main(){ pre(); cin>>n; for(int i=1,x;i<=n;i++)scanf("%d",&x),flag[x]++; ll ans=0; for(int d=1;d<N;d++){ int cnt=0; for(int j=d;j<N;j+=d){ if(flag[j])cnt+=flag[j]; } F[d]=1LL*cnt*ksm(2,cnt-1)%p; } for(int i=1;i<N;i++){ for(int j=i;j<N;j+=i){ f[i]+=F[j]*mu[j/i]; if(f[i]<0)f[i]+=p; if(f[i]>=p)f[i]-=p; } } for(int d=2;d<N;d++){ ans+=1LL*d*f[d]%p; ans%=p; } printf("%lld\n",ans); }