Hdu 4407 Sum(容斥原理)

来源:互联网 发布:美国大豆数据 编辑:程序博客网 时间:2024/05/21 13:57

Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2242    Accepted Submission(s): 620


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.
 

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".
 

Output
For each operation 1, output a single integer in one line representing the result.
 

Sample Input
13 32 2 31 1 3 41 2 3 6
 

Sample Output
70
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<string>#include<algorithm>#include<cstdlib>#include<vector>#define N 100010typedef long long ll;using namespace std;struct node {    int idx;    int now;} sets[1010];int n,m,l;vector<int>prime,g;bool is[N*4];int gcd(int a,int b) {    if(b==0)        return a;    return gcd(b,a%b);}void Prime() {    memset(is,0,sizeof is);    for(int i=2; i<=N*4; i++) {        if(!is[i]) {            prime.push_back(i);            for(int j=i+i; j<=N*4; j+=i) is[j]=1;        }    }}void Div(int x) {    g.clear();    for(int i=0; i<prime.size()&&prime[i]<=x; i++)        if(x%prime[i]==0) {            g.push_back(prime[i]);            while(x%prime[i]==0)  x/=prime[i];        }    if(x!=1)        g.push_back(x);}ll Cal(int x,int y,int z) {    ll ans=(ll)y*(y+1)/2-(ll)x*(x-1)/2;    for(int i=1; i<(1<<g.size()); i++) {        ll sum=1;        int cnt=0;        for(int j=0; j<g.size(); j++)            if(i&(1<<j))  sum*=g[j],cnt++;        ll k=(x-1)/sum;        k=k*(k+1)*sum/2;        ll kk=y/sum;        kk=kk*(kk+1)*sum/2;        kk-=k;        if(cnt&1) ans-=kk;        else      ans+=kk;    }    for(int i=0; i<l; i++) {        if(sets[i].idx<x||sets[i].idx>y)           continue;        if(gcd(sets[i].idx,z)==1)   ans-=sets[i].idx;        if(gcd(sets[i].now,z)==1)   ans+=sets[i].now;    }    return ans;}void work(int x,int y,int z) {    int p=z;    Div(p);    printf("%I64d\n",Cal(x,y,z));}int main() {    Prime();    int t;    cin>>t;    while(t--) {        scanf("%d%d",&n,&m);        l=0;        int op,x,y,z;        while(m--) {            scanf("%d%d%d",&op,&x,&y);            if(op==1) {                scanf("%d",&z);                work(x,y,z);            } else {                int flag=0;                for(int i=0; i<l; i++)                    if(sets[i].idx==x) {                        sets[i].now=y, flag=1;                        break;                    }                if(!flag)                    sets[l].idx=x,sets[l++].now=y;            }        }    }    return 0;}


0 0
原创粉丝点击