HDU 4407 Sum (容斥原理,素数因子)

 

B - Sum

Time Limit: 1000 MS Memory Limit: 32768 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status]

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

 

题意:
对1~n的数列进行两个操作:1、查询区间[x,y]内与p互质的数之和

                                            2、把第x个数修改为c


思路:

刚开始看题往线段树上思考,但发现给定数列是固定的1~n。题目让找与p互质的数,考虑容斥原理和素因子分解,可以将操作二存在map容器中,每次查询时遍历map中的元素,即看之前的操作是否对查询有影响
注:int会爆


相关题目:

https://blog.csdn.net/A_Thinking_Reed_/article/details/81913290


 

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <map>
#define LL long long
using namespace std;
LL a[10000], num, t, rc[10000];
map<LL, LL>Map;///用来存行为二
map<LL, LL>::iterator it;
void init()
{
    memset(a, 0, sizeof(a));
    memset(rc, 0, sizeof(rc));
    Map.clear();
    num = t = 0;
}
LL gcd(LL a, LL b)
{
    return b==0 ? a : gcd(b, a%b);
}
void fenjie(LL p)                  ///分解素数因子
{
    num = 0;
    for(LL i=2; i*i<=p; i++){
        if(p%i==0){
            a[num++] = i;
            while(p%i == 0) p/=i;
        }
    }
    if(p>1) a[num++] = p;
                                    ///容斥原理,奇数次为正,偶数次为负
    t = 0;
    rc[t++] = -1;
    for(LL i=0; i<num; i++){
        LL k=t;
        for(LL j=0; j<k; j++){
            rc[t++] = a[i]*rc[j]*(-1);
        }
    }
}
LL sumdengbi(LL x, LL y, LL a)   ///x~y之间a的倍数和
{
    LL l = (x/a)*a, r = (y/a)*a;
    if(l < x) l += a;
    LL n = (r-l)/a+1;
    LL sum = 0;
    sum = (LL)n*l+n*(n-1)*a/2;
    return sum;
}
LL rongchi(LL x, LL y, LL p)     ///求区间和
{
    if(x>y) return rongchi(y, x, p);
    LL sum = 0;
    for(LL i=1; i<t; i++){         ///小于零减去,大于零加上
        if(rc[i]<0) sum -= sumdengbi(x, y, (-1)*rc[i]);
        else sum += sumdengbi(x, y, rc[i]);
    }
    sum = y*(y+1)/2 - x*(x-1)/2 - sum;///x~y之间与p互质的数的和
    for(it=Map.begin(); it!=Map.end(); it++){///查询改变的值是否影响结果
        if(it->first >= x && it->first <= y){
            if(gcd(it->first, p)==1) sum-=it->first;///若删掉的数与p互质,则减去
            if(gcd(it->second, p)==1) sum+=it->second;///若添加的数与p互质,则加上
        }
    }
    return sum;
}
int main()
{
    LL t, n, m, cnt;
    cin >> t;
    while(t --){
        init();
        scanf("%lld%lld", &n, &m);
        for(LL i=0; i<m; i++){
            scanf("%lld", &cnt);
            if(cnt == 1){
                LL x, y, p;
                scanf("%lld%lld%lld", &x, &y, &p);
                fenjie(p);
                cout << rongchi(x, y, p) << endl;
            }else if(cnt == 2){
                LL x, c;
                scanf("%lld%lld", &x, &c);
                ///Map.insert(pair<int, int>(x, c) );  可能对一个点修改多次,所以不能insert
                Map[x]=c;
            }
        }
    }
}
/**
2
400000 5
2 12354 3
1 1 399999 30
2 41562 45682
2 48512 482879
1 3 100000 15
3 4
2 2 3
1 1 3 4
2 2 2
1 1 3 4

21333066668
2667113378
7
4
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值