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

1
3 3
2 2 3
1 1 3 4
1 2 3 6

Sample Output
7
0
#include <iostream>
#include <cstdio>
#include <map>
#define ll long long
#include <cstring>
using namespace std;

const int N = 400010;
bool isprime[N];
map<int,int> mp;
int prime[34000],cnt;
int factor[30];//素数因子

void Prime() //素数打表
{
    cnt=0;
    memset(isprime,true,sizeof(isprime));
    for(int i=2; i<N; i++)
    {
        if(isprime[i])
        {
            for(int j=i+i; j<N; j+=i)
                isprime[j]=false;
            prime[cnt++]=i;
        }
    }
}
int gcd(int a,int b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}

ll finds(int x,int n,int p) //容斥原理
{
    int t,i,j,num,d,m=1<<n;
    ll ans=(ll)x*(x+1)/2;
    for(i=1; i<m; i++)
    {
        t=i;
        j=num=0;
        d=1;
        while(t)
        {
            if(t&1)
            {
                d*=factor[j];
                num++;
            }
            j++;
            t>>=1;
        }
        n=x/d;
        if(num&1) ans-=(ll)d*(1+n)*n/2;
        else ans+=(ll)d*(1+n)*n/2;
    }
    map<int,int>::iterator it;
    for(it=mp.begin(); it!=mp.end(); it++) //处理被改变了的数
    {
        if(it->first>x) continue;
        if(gcd(it->first,p)==1) ans-=it->first;
        if(gcd(it->second,p)==1) ans+=it->second;
    }
    return ans;
}

int pri(int a) //求出a的素数因子
{
    if(isprime[a])
    {
        factor[0]=a;
        return 1;
    }
    int k=0,i;
    for(i=0; i<cnt; i++)
    {
        if(a%prime[i]==0) factor[k++]=prime[i];
        while(a%prime[i]==0) a/=prime[i];
        if(a!=1&&isprime[a])
        {
            factor[k++]=a;
            return k;
        }
    }
    return k;
}

int main()
{
    Prime();
    int n,m,x,y,op,p,cs;
    scanf("%d",&cs);
    while(cs--)
    {
        scanf("%d%d",&n,&m);
        mp.clear();
        for(int i=0; i<m; i++)
        {
            scanf("%d",&op);
            if(op==1)
            {
                scanf("%d%d%d",&x,&y,&p);
                int num=pri(p);
                printf("%I64d\n",finds(y,num,p)-finds(x-1,num,p));
            }
            else
            {
                scanf("%d%d",&x,&p);
                mp[x]=p;
            }
        }
    }

    return 0;
}
View Code

 

posted @ 2013-09-06 14:08  1002liu  阅读(160)  评论(0编辑  收藏  举报