hdu - Sum-4407 - 容斥原理

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 的数列{An},有2种操作(1≤m≤1000次)
1、求某段区间 [a,b] 中与 p 互质的数的和。
2、将数列中某个位置元素的值改变。

思路
  • 对于操作2,我们可以用map数组记录修改的num和修改后的val。
  • 对于操作1,因为要求所有与p互质的数的和,我们可以转换为求sum(x~y)的和减去所有与p不互质的数的和。
    • 求sum(x~y)的和,我们可以直接用等差公式求和即可。即sum(y)-sum(x-1),等差数列公差为1.
    • 求所有与p不互质的和,即有公约数,因为我们只要知道一个数的质因数,就知道1~n区间内多少与它的约数了,例如12,质因数是2,则有2,4,6,8,10,12共6个,我们可以一次性求掉,因为2+4+6+8+10+12=2*(1+2+3+4+5+6),利用等差求和2*sum(6)即可,然后就是容斥原理,奇加偶减了。
    • 如果有没有讲清楚的地方,请评论说一下具体的哪里,感谢您指出我写题解的不足之处。
代码
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
using namespace std;

typedef long long LL;

int fac[1000];
int cnt;

LL gcd(LL a,LL b)
{//求最大公约数
    return b?gcd(b,a%b):a;
}
void getFactor(LL n)
{//求n的所有素因素
    cnt=0;
    LL m=sqrt(n+0.5);
    for(int i=2;i<=m&&n;i++){
        if(n%i==0){
            fac[cnt++]=i;
            while(n&&n%i==0) n/=i;
        }
    }
    if(n>1) fac[cnt++]=n;
}
LL getSum(LL n)
{//等差数列求和公式
    return (n+1)*n/2;//注意(n+1)/2*n这样不对
}
LL cal(LL n)
{//得到所有与p互质的数之和
    LL res=0;
    for(LL i=1;i<(1LL<<cnt);i++){
        LL mult=1,ones=0;
        for(int j=0;j<cnt;j++){
            if(i&(1<<j)){
                ones++;
                mult*=fac[j];
            }
        }
        //奇加偶减
        if(ones&1) res+=mult*getSum(n/mult);
        else res-=mult*getSum(n/mult);
    }
    //所有与p互质的数之和=整体和-所有与不互质的数之和
    res=getSum(n)-res;
    return res;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        map<LL,LL>mp;
        map<LL,LL>::iterator it;
        LL n,m;
        LL op,x,y,p,c;
        scanf("%lld%lld",&n,&m);
        while(m--){
            scanf("%lld",&op);
            if(op==2){
                scanf("%lld%lld",&x,&c);
                mp[x]=c;
            }else{
                scanf("%lld%lld%lld",&x,&y,&p);
                if(x>y) swap(x,y);
                getFactor(p);
                //printf("%lld-%lld",cal(y),cal(x-1));
                LL ans=cal(y)-cal(x-1);
                for(it=mp.begin();it!=mp.end();++it)
                {
                    LL num=it->first,val=it->second;
                    if(num<x||num>y) continue;
                    if(gcd(num,p)==1){
                        ans-=num;
                        //printf("-%lld",num);
                    }
                    if(gcd(val,p)==1){
                        ans+=val;
                        //printf("+%lld",val);
                    }
                }
                //printf("=");
                printf("%lld\n",ans);
            }
        }
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值