hdu 4407 SUM
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 的质因子互质的数,加奇减偶
在将改变的数进行遍历
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <map> #define ll long long using namespace std; ll change[400005]; ll findthenum[1005]; ll gcd(ll a,ll b) { if(b==0) return a; return gcd(b,a%b); } int main() { int t; ll n,m,x,y,p,c,r; cin>>t; while(t--) { cin>>n>>m; r=0; memset(change,0,sizeof(change)); memset(findthenum,0,sizeof(findthenum)); while(m--) { char ch; cin>>ch; if(ch=='1') { ll pp; cin>>x>>y>>p; pp=p; ll sum=(x+y)*(y-x+1)/2; vector <ll> t; for(int i=2; i*i<=p; i++) { if(p%i==0) { t.push_back(i); while(p%i==0) p=p/i; } } if(p>1) t.push_back(p); ll mm=0; for(int i=1; i<(1<<t.size()); i++) { int cnt=0; ll mul=1; for(int j=0; j<t.size(); j++) { if(i&(1<<j)) { cnt++; mul=mul*t[j]; } } ll numy=y/mul; ll numx=(x-1)/mul; ll tmp; tmp=(mul+numy*mul)*numy/2-(mul+numx*mul)*numx/2; if(cnt&1) mm=mm+tmp; else mm=mm-tmp; } sum=sum-mm; // cout<<sum<<endl; for(int i=0; i<r; i++) { if(findthenum[i]>=x&&findthenum[i]<=y) { if(gcd(findthenum[i],pp)==1) { if(gcd(change[findthenum[i]],pp)==1) { sum=sum-findthenum[i]+change[findthenum[i]]; } else sum=sum-findthenum[i]; } else { if(gcd(change[findthenum[i]],pp)==1) { sum=sum+change[findthenum[i]]; } } } } cout<<sum<<endl; } else { cin>>x>>c; if(change[x]==0) { change[x]=c; findthenum[r++]=x; } else change[x]=c; } } } return 0; }
你若是天才,我便是疯子