Description
XXX is puzzled with the question below:
1, 2, 3, ..., n (1<=n<=400000) are placed in a line. There are m (1<=m<=1000) operations of two kinds.
Operation 1: among the x-th number to the y-th number (inclusive), get the sum of the numbers which are co-prime with p( 1 <=p <= 400000).
Operation 2: change the x-th number to c( 1 <=c <= 400000).
For each operation, XXX will spend a lot of time to treat it. So he wants to ask you to help him.
1, 2, 3, ..., n (1<=n<=400000) are placed in a line. There are m (1<=m<=1000) operations of two kinds.
Operation 1: among the x-th number to the y-th number (inclusive), get the sum of the numbers which are co-prime with p( 1 <=p <= 400000).
Operation 2: change the x-th number to c( 1 <=c <= 400000).
For each operation, XXX will spend a lot of time to treat it. So he wants to ask you to help him.
Input
There are several test cases.
The first line in the input is an integer indicating the number of test cases.
For each case, the first line begins with two integers --- the above mentioned n and m.
Each the following m lines contains an operation.
Operation 1 is in this format: "1 x y p".
Operation 2 is in this format: "2 x c".
The first line in the input is an integer indicating the number of test cases.
For each case, the first line begins with two integers --- the above mentioned n and m.
Each the following m lines contains an operation.
Operation 1 is in this format: "1 x y p".
Operation 2 is in this format: "2 x c".
Output
For each operation 1, output a single integer in one line representing the result.
Sample Input
1 3 3 2 2 3 1 1 3 4 1 2 3 6
Sample Output
7 0
有一个元素为 1~n 的数列{An},有2种操作(1000次):
1、求某段区间 [a,b] 中与 p 互质的数的和。
2、将数列中某个位置元素的值改变。
分析:由于最开始是从1~n的数,之后再修改的值,所以先求解当没有发生改变的值的和求出来,求法采用容斥原理求1~n中与m互质的数的和。先对m进行素因子分解,然后找小于n中能整除m的素因子的个数,这些数求和就是那些不互质的数的和。然后再把更改的东西加加减减就好了。不过别忘判断是不是在x,y范围内。(数据有可能爆int)
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <map>
using namespace std;
struct sa{
int fen[10];
int k;
}p[400005];
int a[400005];
bool phi[400005];
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
long long sum;
void dfs(int id,int w,int k,long long m,long long sumsum){
if(id==p[k].k+1)return;
for(int i=w;i<p[k].k;i++){
long long temp=p[k].fen[i]*sumsum;
if(id&1){
long long t=m/temp;
sum-=temp*(t+1)*t/2;
}
else {
long long t=m/temp;
sum+=temp*(t+1)*t/2;
}
dfs(id+1,i+1,k,m,temp);
}
}//递归实现容斥原理
long long solve(long long n,int pp){
sum=n*(n+1)/2;
// cout<<sum<<endl;
dfs(1,0,pp,n,1);
return sum;
}
int main()
{
int t;
cin>>t;
for(int i=1;i<400005;i++)
p[i].k=0;
memset(phi,0,sizeof(phi));
phi[1]=true;
for(int i=2;i<400005;i++){
if(!phi[i]){
for(int j=i;j<400005;j+=i){
phi[j]=true;
p[j].fen[p[j].k]=i;
p[j].k++;
}
}
}//先递推求解素因子的和
while(t--){
map<int,int>a;
map<int,int>::iterator it;
int n,m;
cin>>n>>m;
while(m--){
int k;
scanf("%d",&k);
if(k==2){
int x,c;
scanf("%d%d",&x,&c);
a[x]=c;
}
else if(k==1){
int x,y,pp;
long long ans;
scanf("%d%d%d",&x,&y,&pp);
ans=solve(y,pp)-solve(x-1,pp);
for(it=a.begin();it!=a.end();it++){
if(it->first>=x&&it->first<=y){//千万别忘了这个条件
if(gcd(it->first,pp)==1)
ans-=it->first;
if(gcd(it->second,pp)==1)
ans+=it->second;
}
}
cout<<ans<<endl;
}
}
}
return 0;
}