Problem 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
思路:先用容斥定理求解x-y内与p互素的和s,然后在判断前面修改过的第k数字和修改后的k1是否都与p不互素):如果都不互素,则不需要做改变,如果k,k1都与p互素则s=s-k+k1;如果k与p互素,k1不与p互素,则s=s-d;如果k与p不互素;k1与p互素,则s=s+k1;
具体代码如下:
#include<stdio.h>
#include<string.h>
int q[400001],cnt,p[100],J[1000];
int main()
{
long long n,m,t,x,y,k,a,snt,s;
scanf("%lld",&t);
for(int i=1;i<=t;i++)
{
scanf("%lld %lld",&n,&m);
snt=0;
memset(q,0,sizeof(q));
for(int o=1;o<=m;o++)
{
scanf("%lld",&a);
if(a==2)
{
scanf("%lld %lld",&x,&y);
if(!q[x])
J[snt++]=x;
q[x]=y;
}
else
{
scanf("%lld %lld %lld",&x,&y,&k);
cnt=0;
s=(y-x+1)*(x+y)/2;;
for(int j=2;j*j<=k;j++)
{
if(k%j==0)
{
p[cnt++]=j;
while(k%j==0)
k=k/j;
}
}
if(k>1)
p[cnt++]=k;
for(int j=1;j<(1<<cnt);j++)
{
long long sum=1,l=0,h1,h2,sen;
for(int k=0;k<cnt;k++)
{
if(j&(1<<k))
{
l++;
sum=sum*p[k];
}
}
h1=y/sum;
h2=(x-1)/sum;
sen=(h1+h2+1)*(h1-h2)/2*sum;
if(l%2)
s=s-sen;
else
s=s+sen;
}
for(int j=0;j<snt;j++)
{
if(J[j]<x||J[j]>y)
continue;
int d=J[j];
int flag1=1,flag2=1;
for(int k=0;k<cnt;k++)
{
if(d%p[k]==0)
flag1=0;
if(q[d]%p[k]==0)
flag2=0;
}
if(flag1&&flag2)
s=s-d+q[d];
if(flag1&&!flag2)
s=s-d;
if(!flag1&&flag2)
s=s+q[d];
}
printf("%lld\n",s);
}
}
}
return 0;
}