BZOJ 3853 GCD Array

来源:互联网 发布:日本动漫制作软件 编辑:程序博客网 时间:2024/05/16 19:20

Description

Teacher Mai finds that many problems about arithmetic function can be reduced to the following problem:
Maintain an array a with index from 1 to l. There are two kinds of operations:
  1. Add v to ax for every x that gcd(x,n)=d.
  2. Query Sigma(Xi) (i<=1<=X)

Input

There are multiple test cases, terminated by a line "0 0".
For each test case, the first line contains two integers l,Q(1<=l,Q<=5*10^4), indicating the length of the array and the number of the operations.
In following Q lines, each line indicates an operation, and the format is "1 n d v" or "2 x" (1<=n,d,v<=2*10^5,1<=x<=l).

Output

For each case, output "Case #k:" first, where k is the case number counting from 1.
Then output the answer to each query.

Sample Input

6 4
1 4 1 2
2 5
1 3 3 3
2 3
0 0

Sample Output

Case #1:
6
7

HINT

Source

By 镇海中学

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

莫比乌斯反演+树状数组+思路~

思路见游神的博客:http://www.cnblogs.com/SilverNebula/p/6991328.html,顺便orz一下~

把修改看成是求和的形式来进行莫比乌斯反演,相当神奇的做法啊。


#include<cstdio>#include<cstring>#include<vector>using namespace std;#define ll long longint n,m,pos,x,d,val,pri[100001],mu[200001],totcas;ll c[50001],ans;bool b[200001];vector<int> ve[200001];int read(){int x=0,f=1;char ch=getchar();while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}void add(int u,ll v){for(;u<=n;u+=u&(-u)) c[u]+=v;}ll cal(int u){ll now=0;for(;u;u-=u&(-u)) now+=c[u];return now;}int main(){mu[1]=1;for(int i=2;i<=200000;i++){if(!b[i]) pri[++pri[0]]=i,mu[i]=-1;for(int j=1;j<=pri[0] && pri[j]*i<=200000;j++){b[pri[j]*i]=1;if(!(i%pri[j])){mu[i*pri[j]]=0;break;}mu[i*pri[j]]=-mu[i];}}for(int i=1;i<=200000;i++)  for(int j=i;j<=200000;j+=i) ve[j].push_back(i);while(n=read()){printf("Case #%d:\n",++totcas);memset(c,0,sizeof(c));m=read();while(m--){pos=read();x=read();if(pos==1){d=read();val=read();if(!(x%d)){x/=d;for(int i=ve[x].size()-1;~i;i--) add(d*ve[x][i],mu[ve[x][i]]*val);}}else{ans=0;for(int i=1,now;i<=x;i=x/now+1){now=x/i;ans+=(cal(x/now)-cal(i-1))*(ll)now;}printf("%lld\n",ans);}}}return 0;}


原创粉丝点击