Sum(容斥原理)

E - Sum
Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

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,2操作.一开始给出(1-n)n个数.1操作就是从第X个到第Y个中找出与P互质的数,并求出它们的和;2操作是把第X个数改为C;
题解:对于题目的1操作可以直接容斥加等差数列求和搞,而对于2操作可以先用一个X数组和num数组记录改变那些数和改变后的数,然后在求1操作是直接
枚举就行. 具体的看以下的代码吧!

AC代码:

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;
const int M = 4e5 + 100;
int x[M],num[M];
vector<int>tmp;

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

int main()
{
    //freopen("in","r",stdin);
    int T,n,m,to;
    scanf("%d",&T);
    while(T--)
    {
        memset(x,0,sizeof(x));
        memset(num,0,sizeof(num));
        scanf("%d %d",&n,&m);
        to = 0;
        while(m--)
        {
            int nx,ny,p,c,id;
            scanf("%d",&id);
            if(id == 2)
            {
                scanf("%d %d",&nx,&c);
                if(nx == c && !x[nx]) continue;
                if(!x[nx]) num[to++] = nx;
                x[nx] = c;
            }
            else
            {
                scanf("%d %d %d",&nx,&ny,&p);
                ll isGO = 0;
                int t = p;
                tmp.clear();
                if(nx > ny) swap(nx,ny);
                for(int i = 2; i * i <= t && p != 1; i++)
                {
                    if(p % i == 0)
                    {
                        tmp.push_back(i);
                        while(!(p % i))
                            p /= i;
                    }
                }
                if(p != 1) tmp.push_back(p);
                int L = tmp.size();
                ll ans = 0,sum = 1LL * (nx + ny) * (ny - nx + 1) / 2;
                for(int i = 1; i < (1LL << L); i++)
                {
                    ll mul = 1,tp = 0;
                    for(int j = 0; j < L; j++)
                    {
                        if((i >> j) & 1)
                        {
                            mul *= tmp[j];
                            tp++;
                        }
                    }
                    ll N = ny / mul;
                    ll M = (nx - 1) / mul;
                    ll res = mul * (1 + N) * N / 2;
                    res = res - mul * (1 + M) * M / 2;
                    if(tp & 1) ans += res;
                    else ans -= res;
                }
                sum -= ans;
                sort(num,num + to);
                if(to == 0 || num[0] > ny || num[to - 1] < nx);
                else
                {
                    int u1 = lower_bound(num,num + to,nx) - num;
                    for(int i = u1; i < to && num[i] <= ny; i++)
                    {
                        int Gcd_x_t = gcd(x[num[i]],t);
                        int Gcd_x_i = gcd(num[i],t);
                        if(Gcd_x_i == 1) sum -= num[i];
                        if(Gcd_x_t == 1)
                            sum += x[num[i]];
                    }
                }
                printf("%I64d\n",sum);
            }
        }
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值