首页 > 代码库 > hdu4407 Sum 容斥+暴力

hdu4407 Sum 容斥+暴力

http://acm.hdu.edu.cn/showproblem.php?pid=4407

Sum

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


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
 

Source
2012 ACM/ICPC Asia Regional Jinhua Online

题意:对于1到n的n个数,两种操作,一种是第[x,y]区间有多少数和p互质,另一种是改变第x个数为c。

分析:如果没有第二个操作,那么求[x,y]区间可以直接用容斥求1到x-1和1到y的分别有多少个与p互质的,结果一减就好。对于操作2,由于总操作数只有m<=1000次,那么我们可以用一个map记录修改的数,然后暴力扫一遍map每一个数,map[a]=b,若a在区间[x,y]内,a与p互质,则答案减1,b与p互质则加1.。。

/**
 * @author neko01
 */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf("%d\n",a)
typedef pair<int,int> pp;
const double eps=1e-9;
const double pi=acos(-1.0);
const int INF=0x7fffffff;
const LL inf=(((LL)1)<<61)+5;
int n,m;
const int M=1000;
bool isprime[M];
int prime[M];
int fac[25];
int tot=0,num;
map<int,int>mp;
map<int,int>::iterator it;
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
void getprime()
{
    for(int i=2;i<=M;i++)
    {
        if(isprime[i]) continue;
        prime[tot++]=i;
        for(int j=i*i;j<=M;j+=i)
            isprime[j]=true;
    }
}
LL gao(int x)
{
    LL sum=0;
    for(int i=1;i<(1<<num);i++)
    {
        int tmp=1,op=0;
        for(int j=0;j<num;j++)
        {
            if((1<<j)&i)
            {
                op++;
                tmp*=fac[j];
            }
        }
        if(op&1)
        {
            LL k=x/tmp;
            sum+=(tmp+k*tmp)*k/2;
        }
        else
        {
            LL k=x/tmp;
            sum-=(tmp+k*tmp)*k/2;
        }
    }
    return 1LL*x*(1+x)/2-sum;
}
void getfac(int x)
{
    num=0;
    for(int i=0;i<tot&&prime[i]*prime[i]<=x;i++)
    {
        if(x%prime[i]==0)
        {
            fac[num++]=prime[i];
            while(x%prime[i]==0)
                x/=prime[i];
        }
    }
    if(x>1) fac[num++]=x;
}
LL getans(int a,int b,int p)
{
    LL ans=gao(b)-gao(a-1);
    for(it=mp.begin();it!=mp.end();it++)
    {
        if(it->first<a||it->first>b||it->first==it->second)
            continue;
        if(gcd(it->first,p)==1) ans-=it->first;
        if(gcd(it->second,p)==1) ans+=it->second;
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    getprime();
    while(t--)
    {
        scanf("%d%d",&n,&m);
        mp.clear();
        for(int i=1;i<=m;i++)
        {
            int xx,x,y,p;
            scanf("%d",&xx);
            if(xx==1)
            {
                scanf("%d%d%d",&x,&y,&p);
                getfac(p);
                printf("%I64d\n",getans(x,y,p));
            }
            else
            {
                scanf("%d%d",&x,&y);
                mp[x]=y;
            }
        }
    }
    return 0;
}



hdu4407 Sum 容斥+暴力