BZOJ3853 : GCD Array
1 n d v相当于给$a[x]+=v[\gcd(x,n)=d]$
\[\begin{eqnarray*}&&v[\gcd(x,n)=d]\\&=&v[\gcd(\frac{x}{d},\frac{n}{d})=1]\\&=&v\sum_{k|\gcd(\frac{x}{d},\frac{n}{d})}\mu(k)\\&=&\sum_{k|\frac{n}{d},dk|x}v\mu(k)\end{eqnarray*}\]
设$a[i]=\sum_{j|i}f[j]$
则每次修改相当于枚举$k|\frac{n}{d}$,然后给$f[dk]+=v\mu(k)$
查询$x=\sum_{i=1}^x a[i]=\sum_{i=1}^x\sum_{d|i}f[d]=\sum_{d=1}^x f[d]\frac{x}{d}$
可以分块统计,用树状数组维护f[]的前缀和
#include<cstdio> typedef long long ll; const int N=200001,M=2480000; int T,n,m,i,j,x,y,z,p[N],tot,mu[N],g[N],nxt[M],v[M],ed,op;ll b[N],t;bool vis[N]; inline void addedge(int x,int y){v[++ed]=y;nxt[ed]=g[x];g[x]=ed;} inline void add(int x,int y){for(;x<=n;x+=x&-x)b[x]+=y;} inline ll sum(int x){ll t=0;for(;x;x-=x&-x)t+=b[x];return t;} int main(){ for(mu[1]=1,i=2;i<N;i++){ if(!vis[i])p[++tot]=i,mu[i]=-1; for(j=1;j<=tot;j++){ if(i*p[j]>=N)break; vis[i*p[j]]=1; if(i%p[j])mu[i*p[j]]=-mu[i];else{mu[i*p[j]]=0;break;}; } } for(i=1;i<N;i++)for(j=i;j<N;j+=i)addedge(j,i); while(1){ scanf("%d%d",&n,&m); if(!n)return 0; for(i=1;i<=n;i++)b[i]=0; printf("Case #%d:\n",++T); while(m--){ scanf("%d%d",&op,&x); if(op==1){ scanf("%d%d",&y,&z); if(x%y==0)for(i=g[x/y];i;i=nxt[i])add(v[i]*y,z*mu[v[i]]); }else{ for(t=0,i=1;i<=x;i=j+1)j=x/(x/i),t+=(sum(j)-sum(i-1))*(x/i); printf("%lld\n",t); } } } }