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
题意:
给T组数据,给一个n,一个m, 起始n个数的排列是1,2,...,n 然后m个操作
操作分为两种,1和2
1 再输入一个x,y,p, 查询在 区间[x,y]中与p互质的数的和
2 再输入一个x,c 把下标为x的数的值换为c
题解:
每次1的操作,我们先不管更改了的值,先把p分解为各种素数的组合, 1到y和1到x-1的和的差值,就是[x,y]这个范围内的总和,这个总和我们拿等差数列来算 n*(n+1)/2, 然后查询 1到n这个这个范围内与p互质的数的总和,因为会出现重复的情况,所以要用容斥去处理,其实先奇加偶减可以的,然后存起来,然后再被n*(n+1)/2减掉就是答案了,但我代码是直接拿这个总和去减去所以代码是奇减偶加的.到后面,在处理2中有没有范围在[x,y]的修改,如果有,原本的x如果跟p互质,就要减去,如果更改后的c跟p互质,就要加上,然后输出结果就行.
每次2的操作,直接用map保存,那我就不会出现可能会出现更改x的情况,做了一个去重的处理
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int N=2e3;
bool mark[N];
int prim[N];
int cnt;
map<int,int>mp;
map<int,int>::iterator it;
vector<int>vc;
void initial()
{
cnt=0;
for (int i=2 ; i<N ; ++i)
{
if (!mark[i])
prim[cnt++]=i;
for (int j=0 ; j<cnt && i*prim[j]<N ; ++j)
{
mark[i*prim[j]]=1;
if (!(i%prim[j]))
break;
}
}
}
void divi(int x)
{
vc.clear();
int be=x;
for (int i=0 ; i<cnt ; ++i)
{
if (prim[i]*prim[i]>be)
break;
if (!(x%prim[i]))
{
while (!(x%prim[i]))
x/=prim[i];
vc.push_back(prim[i]);
if (x==1)
break;
}
}
if (x>1)
vc.push_back(x);
}
void dfs(int now,int s,ll x,ll sum,ll &ans)
{
if (sum>x)
return ;
if (s&1)
ans-=(x/sum)*(x/sum+1)/2*sum;
else
ans+=(x/sum)*(x/sum+1)/2*sum;
for (int i=now+1 ; i<vc.size() ; ++i)
dfs(i,s+1,x,sum*vc[i],ans);
}
ll cal(ll x)
{
ll ans=x*(x+1)/2;
for (int i=0 ; i<vc.size() ; ++i)
dfs(i,1,x,vc[i],ans);
return ans;
}
int main()
{
initial();
int T;
scanf("%d",&T);
while (T--)
{
mp.clear();
int n,m;
scanf("%d%d",&n,&m);
while (m--)
{
int ch;
scanf("%d",&ch);
if (ch==1)
{
int x,y,p;
scanf("%d%d%d",&x,&y,&p);
divi(p);
ll ans=cal(y)-cal(x-1);
for (it=mp.begin() ; it!=mp.end() ; ++it)
{
if ((*it).first<x || (*it).first>y)
continue;
if (__gcd((*it).first,p)==1)
ans-=(*it).first;
if (__gcd((*it).second,p)==1)
ans+=(*it).second;
}
printf("%lld\n",ans);
}
else
{
int x,c;
scanf("%d%d",&x,&c);
mp[x]=c;
}
}
}
return 0;
}