HDU 4407 Sum (容斥原理)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/deepseazbw/article/details/76660935

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

题意给n个数字,m个操作,操作1,计算从第x到第y个数字与p互素的和;操作2:改变第x个数字的值为c,默认未改变的时候a[i]=i;
思路:又涉及到了互素的问题,肯定是容斥原理,但是如果每次将2操作改变之后的数字容斥的话,会花费大量时间TLE,于是我们想到了可以先求出从x--y未改变的时候数字互素p的和,然后存操作2改变的数字,map的红黑树一一对应比较适合,操作1:对之前map存贮的数字,进行逐个判断,操作2进行存贮
(最近这几天心情比较浮躁,可能是因为晚上睡的晚的缘故,一直静不下心来好好搞ACM,仔细回想这几天看到的容斥原理的题目,不是做不出来,而是刚开始心情太浮躁,不愿意去仔细理解。还有关于项目的方面,目前仍是一无所知,得花时间去了解下)
#include <map>
#include <stdio.h>
typedef long long ll;
using namespace std;

#define N 650
bool np[N];
int prime[120],fac[9],F_top,p;
ll ans;

void get_prime()
{
    int top =0;
    for(int i=2;i<N;i++)
        if(!np[i])
        {
            prime[top++] = i;
            for(int j=i+i;j<N;j+=i)
                np[j] = true;
        }
}

void Div(int n)
{
    F_top = 0;
    for(int i=0;prime[i]*prime[i]<=n;i++)
    {
        if(n % prime[i])
            continue;
        while(n % prime[i] == 0)
            n /= prime[i];
        fac[F_top++] = prime[i];
    }
    if(n != 1)
        fac[F_top++] = n;
}

ll PreSum(int n)
{
    return (ll)n*(n+1)/2;
}

void dfs(int i,int cnt,int m,int n,int num,int x)       // C(n,m).
{
    if(cnt == m)
    {
        ll sum = num * PreSum(x/num);
        m&1 ? ans -= sum : ans += sum;
        return ;
    }
    if(num*fac[i] > x || n-i < m-cnt)
        return ;
    dfs(i+1,cnt+1,m,n,num*fac[i],x);
    dfs(i+1,cnt,m,n,num,x);
}

ll sovle(int n)
{
    ans = PreSum(n);
    for(int m=1;m<=F_top;m++)
        dfs(0,0,m,F_top,1,n);
    return ans;
}

int gcd(int a,int b)
{
    return b ? gcd(b,a%b) : a;
}

int main()
{
    int t,n,ch,cmd,a,b,num;
    get_prime();
    scanf("%d",&t);
    map<int,int> M;
    while(t--)
    {
        M.clear();
        scanf("%d%d",&n,&num);
        while(num--)
        {
            int ch;
            scanf("%d",&ch);
            if(ch == 1){
                scanf("%d%d%d",&a,&b,&p);
                Div(p);
                ans = sovle(b) - sovle(a-1);//容斥原理就算互素的和
                for(map<int,int>::iterator it=M.begin();it!=M.end();it++)//之前存贮的2操作改变的数值
                    if((*it).first >= a && (*it).first <= b){//如果该值在范围内
                        if(gcd((*it).first,p) == 1) //如果与之前的互素,则减去
                            ans -= (*it).first;     //如果与之前的不互素则不影响
                        if(gcd((*it).second,p) == 1) //如果改变后的互素,则加上
                            ans += (*it).second;      //如果与改变后的不互素,则不影响
                    }
                printf("%lld\n",ans);
            }
            else{
                scanf("%d%d",&a,&b);
                M[a] = b;
            }
        }
    }
    return 0;
}


阅读更多
换一批

没有更多推荐了,返回首页