BZOJ 3853 GCD Array

216 篇文章 0 订阅
28 篇文章 0 订阅

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

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

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

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

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


#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define ll long long

int 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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值