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
分析:注意到m次操作,m非常小,我們將操作過程記錄下來,然後暴力搞
具體就是先求出沒有前面操作的時候的和,然後解決操作帶來的影響
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
typedef long long LL;
const int maxn=1e6+100;
const int mod=1e9+7;
typedef pair<int,int>pil;
pil op[1100];
int vis[maxn];
int v[20];
int t,n,m,k,tot,w;
LL work(int x)
{
LL ans=0;
for(int i=1;i<(1<<k);i++)
{
LL c=0,l=1;
for(int j=0;j<k;j++)
{
if(i&(1<<j))
{
c++;
l*=v[j];
}
}
LL cnt=x/l;
if(c&1)
ans=(ans+l*(1+cnt)*cnt/2);
else
ans=(ans-l*(1+cnt)*cnt/2);
}
ans=1LL*(1+x)*x/2-ans;
return ans;
}
int main()
{
int x,y,p,o;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
tot=0;
while(m--)
{
scanf("%d",&o);
if(o==1)
{
scanf("%d%d%d",&x,&y,&p);
k=0;w=p;
for(int i=2;i*i<=p;i++)
{
if(p%i==0)
{
while(p%i==0)
p/=i;
v[k++]=i;
}
}
if(p>1)
v[k++]=p;
LL sum=work(y)-work(x-1);
for(int i=tot-1;i>=0;i--)
{
int l=op[i].first;
int val=op[i].second;
if(!vis[l])
{
if(l>=x&&l<=y)
{
if(__gcd(l,w)==1&&__gcd(val,w)==1)
sum=sum-l+val;
else if(__gcd(l,w)!=1&&__gcd(val,w)==1)
sum=sum+val;
else if(__gcd(l,w)==1&&__gcd(val,w)!=1)
sum=sum-l;
}
vis[l]=1;
}
}
for(int i=tot-1;i>=0;i--)
{
int l=op[i].first;
int val=op[i].second;
vis[l]=0;
}
printf("%I64d\n",sum);
}
else
{
scanf("%d%d",&x,&p);
op[tot].first=x;
op[tot++].second=p;
}
}
}
return 0;
}