[莫比乌斯反演+莫队] HDU4676 Sum Of Gcd
2017-09-28 13:08
239 查看
反演的过程类似这题 ,得
∑T=1nϕ(T) cnt[T]2
cnt[T] 表示当前区间内是 T 的倍数的数的个数。
因为有 cnt,每次直接求肯定不行,可以想到莫队。移动过程中每次暴力维护 cnt 即可。
复杂度理论很大,但实际上能过。
∑T=1nϕ(T) cnt[T]2
cnt[T] 表示当前区间内是 T 的倍数的数的个数。
因为有 cnt,每次直接求肯定不行,可以想到莫队。移动过程中每次暴力维护 cnt 即可。
复杂度理论很大,但实际上能过。
#include<cstdio> #include<cmath> #include<vector> #include<algorithm> #include<cstring> using namespace std; typedef long long LL; const int maxn=20005, N=20000; int phi[maxn],p[maxn]; bool vis[maxn]; void get_phi(){ phi[1]=1; for(int i=2;i<=N;i++){ if(!vis[i]) p[++p[0]]=i, phi[i]=i-1; for(int j=1;j<=p[0]&&(LL)i*p[j]<=N;j++){ vis[i*p[j]]=true; if(i%p[j]==0){ phi[i*p[j]]=phi[i]*p[j]; break; } phi[i*p[j]]=phi[i]*(p[j]-1); } } } int _test,n,m,a[maxn],cnt[maxn],_B,blk,blg[maxn]; LL ans1,ans2,ans[maxn]; struct query{ int L,R,id; bool operator < (const query &b)const{ if(blg[L]==blg[b.L]) return R<b.R; return blg[L]<blg[b.L]; } } q[maxn]; vector<int> lst[maxn]; void Updata(int x,int k){ ans2+=x*k; for(int i=0;i<lst[x].size();i++){ int now=lst[x][i]; ans1-=(LL)phi[now]*cnt[now]*cnt[now]; cnt[now]+=k; ans1+=(LL)phi[now]*cnt[now]*cnt[now]; } } int main(){ freopen("hdu4676.in","r",stdin); freopen("hdu4676.out","w",stdout); get_phi(); for(int i=1;i<=N;i++) for(int j=1;(LL)i*j<=N;j++) lst[i*j].push_back(i); scanf("%d",&_test); for(int ii=1;ii<=_test;ii++){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); scanf("%d",&m); for(int i=1;i<=m;i++) scanf("%d%d",&q[i].L,&q[i].R), q[i].id=i; blk=sqrt(n); for(int i=1;i<=n;i++) blg[i]=(i-1)/blk+1; sort(q+1,q+1+m); ans1=ans2=0; memset(cnt,0,sizeof(cnt)); Updata(a[1],1); for(int i=1,L=1,R=1;i<=m;i++){ while(L<q[i].L) Updata(a[L++],-1); while(q[i].L<L) Updata(a[--L],1); while(R<q[i].R) Updata(a[++R],1); while(q[i].R<R) Updata(a[R--],-1); ans[q[i].id]=(ans1-ans2)/2; } printf("Case #%d:\n",ii); for(int i=1;i<=m;i++) printf("%lld\n",ans[i]); } return 0; }
相关文章推荐
- [数论 反演 && 莫队] hdu4676 . Sum Of Gcd
- HDU4676 Sum Of Gcd
- hdu4676 Sum Of Gcd
- HDU4676 Sum Of Gcd (数论 + 分块)
- 【HDU4676】Sum Of Gcd-莫队算法+欧拉函数
- HDOJ 5381 The sum of gcd 莫队算法
- HDU 5381 The sum of gcd 询问区间内所有子区间的GCD和 [莫队算法]
- HDU4767_Sum Of Gcd
- Sum Of Gcd HDU - 4676(莫队)
- HDU 5381 The sum of gcd
- hdu5381 The sum of gcd
- hdu 5381 The sum of gcd(线段树)
- HDU 5381 The sum of gcd
- hdu 4676 Sum Of Gcd 莫队+phi反演
- [数论][莫队][莫比乌斯反演] hdu 4676 Sum Of Gcd
- HDU5381-The sum of gcd
- HDU 4676 Sum Of Gcd(欧拉函数求区间gcd之和+分块算法)
- HDU 4676 Sum Of Gcd【数论,数据结构(分块)】
- hdu 5381 The sum of gcd 莫队+预处理
- HDU 5381 The sum of gcd