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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值