HDU4407Sum 容斥定理

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4407

题意:在1E5个数中求某段区间中和p互素的数和。有1000次操作,每次操作可能是询问,可能是修改单点值。

注意!!初始时,序列是1,2.....n,很关键呀。操作数很少,可以用map存下修改操作,每次询问求cal(r,p)-cal(l-1,p),在加上修改的影响。

cal(r,p)可用容斥求得。

代码:

/********************************************

Problem : 4407 ( Sum )
Judge Status : Accepted
Language : G++
Author : alpc_wt

********************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;

const int N = 4*100000+10;
const int maxn = 4*100000;

vector<int> pri[N];
map<int,int> mm;
map<int,int>::iterator it;
int cnt[N];

void init(){
    for(int i=0;i<=maxn;i++)
        pri[i].clear();
    for(int i=2;i<=maxn;i++) if(pri[i].size()==0)
        for(int j=i;j<=maxn;j+=i)
            pri[j].push_back(i);
}

int sav[N][2],num;

ll cal(int r,int p){
    if(r==0) return 0;
    ll ret = 0;

    int size = pri[p].size();
    for(int i=0;i<(1<<size);i++){
        int z=1 , fl =1;
        for(int j=0;j<size;j++)
            if(i&(1<<j)){
                z *= pri[p][j];
                fl *= -1;
            }
        ll co = r / z;
        ll tmp = co * (co+1) * z / 2;

        if(fl==1) ret += tmp;
        else ret -= tmp;
    }
    return ret;
}

ll solve(int l,int r,int p){
    ll ans = cal(r,p) - cal(l-1,p);
    for(it=mm.begin();it!=mm.end();it++){
        int x = it->first;
        int y = it->second;
        if(x<l || x>r) continue;
        if(__gcd(x,p)==1) ans -= x;
        if(__gcd(y,p)==1) ans += y;
    }
    return ans;
}

int main(){
    int T,n,m,p,c,x,l,r;
    init();
    cin >> T;
    while(T--){

        scanf("%d%d",&n,&m);
        num=0;
        mm.clear();

        for(int i=1;i<=m;i++){
            scanf("%d",&x);
            if(x==1){
                scanf("%d%d%d",&l,&r,&p);
                ll ans = solve(l,r,p);
                printf("%lld\n",ans);
            }
            else{
                scanf("%d%d",&x,&c);
                mm[x] = c;
            }
        }
    }
    return 0;
}
时间: 2024-07-30 01:33:56

HDU4407Sum 容斥定理的相关文章

容斥定理 hdu2204 Eddy&#39;s爱好

传送门:点击打开链接 很明显会有大量重复的被计算,所以很容易就想到容斥定理. 我们设dp[i]表示能表示成M^i(i>1)且i是这个数字能表示出来的最大的情况时的总类数 比如,27拆成M^K时的K最大能表示成3,所以27这个数字分在dp[3]这一类 1我们暂时不考虑,不把它放在任何一类 因为K>1,所以K至少是2,最大是2^K=N的时候,所以K最大等于log2(N),所以K非常的小 首先,求出K最大大概的位置 然后开始求dp[i].求法如下: 首先,1~N中有哪些是能拆分成M^i的,利用pow

cf451E Devu and Flowers 卢卡斯定理+容斥定理

题目:http://codeforces.com/problemset/problem/451/E 题意:有n个盒子(n<=20),每个盒子中有10^12个小球,现从每个盒子中取出若干球(可为0),求共取出s个小球(s<=10^14)的方案数. 组合数学问题,求C(n,m).但n,m过大时,可用卢卡斯定理. 卢卡斯定理:C(n,m) %p = C(n/p,m/p) * C(n%p,m%p) 从n个盒子中取出s个球的方案数,相当于插板,即 C(s+n-1,n-1).注意这是没有限制条件的情况.

HDU1796 How many integers can you find【容斥定理】

题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1796 题目大意: 给你一个整数N.和M个整数的集合{A1.A2.-.Am}.集合内元素为非负数(包括零),求小于N的 正整数(1~N-1)中,能被M个整数的集合中随意一个元素整除的正整数个数. 比如N = 12.M = {2,3},在1~N-1中,能被2整除的数为{2,4,6.8.10},能被3整除的数为 {3.6,9}.则所求集合为{2,3,4.6,8,9,10},共7个,则答案为7. 思路:

HDU 1796 How many integers can you find (容斥定理 + 二进制)

How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5493    Accepted Submission(s): 1567 Problem Description Now you get a number N, and a M-integers set, you should

HDU 1695 GCD(容斥定理)

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7529    Accepted Submission(s): 2773 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y

GCD SUM 强大的数论,容斥定理

GCD SUM Time Limit: 8000/4000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description 给出N,M执行如下程序:long long  ans = 0,ansx = 0,ansy = 0;for(int i = 1; i <= N; i ++)   for(int j = 1; j <= M; j ++)       if(gcd(i,j)

HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)

When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortunately fall into the sea. While it was dying, seven dolphins arched its body and sent it back to the shore. It is said that ?? used to surrounded by 7

容斥定理,皮克公式

(来源:哈工大算法培训) 容斥定理:在计算集合的并集的时候,我们经常需要减去重复的部分.但是怎么减比较麻烦.容斥定理就是解决这类问题. 内容: 比如: 皮克公式: 1.如何求多边形面积: 例: 思路:按顺序两点求叉积. S=abs(1/2*((x1*y2-x2*y1)+.......+(xk*yk+1-xk+1*yk)+.........+(xn*y1-x1*yn))). 由于计算的时候可能因为顺逆时针的不同而产生正负.所以加上一个abs. 2.求多边形边上的整数点. 3. 适用于格点图. 原文

题解报告:hdu 4135 Co-prime(容斥定理入门)

Problem Description Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than