HDU 4135

http://acm.hdu.edu.cn/showproblem.php?pid=4135

求[A,B]内与N互素的数字个数

首先对N分解质因数,对于一个质因数,1-n与它不互素的数字个数是n/(这个质因数),这样可以得到m个集合(m是N分解出的质因数的个数),对这m个集合用容斥原理解出来它们的并集,再用总数去减

这里学习了用位运算求解容斥原理,非常简单,和状压dp中的位运算差不多感觉,注意容斥中奇加偶减

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

typedef __int64 ll;

ll A,B,N;

vector <ll> v;

ll gao(ll n){
    int m=v.size();
    ll res=0;
    for(int i=1;i<(1<<m);i++){
        int cnt=0;
        ll temp=1;
        for(int j=0;j<m;j++){
            if(i&(1<<j)){
                cnt++;
                temp*=v[j];
            }
        }
        if(cnt&1)res+=n/temp;
        else res-=n/temp;
    }
    return n-res;
}

int main(){
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++){
        scanf("%I64d%I64d%I64d",&A,&B,&N);
        v.clear();
        for(ll i=2;i*i<=N;i++){
            if(N%i==0){
                v.push_back(i);
                while(N%i==0)N/=i;
            }
        }
        if(N>1)v.push_back(N);
        printf("Case #%d: %I64d\n",cas,gao(B)-gao(A-1));
    }
    return 0;
}

时间: 2024-10-01 10:19:29

HDU 4135的相关文章

[容斥原理] hdu 4135 Co-prime

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4135 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1176    Accepted Submission(s): 427 Problem Description Given a number N, you are a

HDU 4135 Co-prime

Co-prime Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 413564-bit integer IO format: %I64d      Java class name: Main Given a number N, you are asked to count the number of integers between A and B inclusiv

容斥 - HDU 4135 Co-prime

Co-prime Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4135 推荐: 容斥原理 Mean: 给你一个区间[l,r]和一个数n,求[l,r]中有多少个数与n互素. analyse: 经典的容斥原理题. 如果题目是说求1~n中有多少个数与n互质,我们一定反应应该是欧拉函数. 但是如果n特别大或者说是求某个给定区间与n互素的个数,这时用欧拉函数就行不通. 容斥做法:首先我们可以在O(sqrt(n))内求出n的所有质因数p

HDU 4135 Co-prime (分解质因数 + 容斥)

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2314    Accepted Submission(s): 865 Problem Description Given a number N, you are asked to count the number of integers between A and B

容斥原理学习(Hdu 4135,Hdu 1796)

题目链接Hdu4135 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1412    Accepted Submission(s): 531 Problem Description Given a number N, you are asked to count the number of integers betwe

HDU 4135 Co-prime(容斥+数论)

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5526    Accepted Submission(s): 2209 Problem Description Given a number N, you are asked to count the number of integers between A and B

HDU - 4135 Co-prime(容斥原理)

Question 参考 题意找出[a,b]中与n互质的数的个数分析通常我们求1-n中与n互质的数的个数都是用欧拉函数.但如果n比较大或者是求1-m中与n互质的数的个数等等问题,要想时间效率高的话还是用容斥原理.先对n分解质因数,分别记录每个质因数, 那么所求区间内与某个质因数不互质的个数就是 m/r(i),假设r(i)是r的某个质因子 假设只有三个质因子, 总的不互质的个数应该为p1+p2+p3-p1*p2-p1*p3-p2*p3+p1*p2*p3. pi代表m/r(i),即与某个质因子不互质的

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 tha

hdu 4135 容斥原理

又搞了一道容斥原理. 题目:求[1,n]区间对m互质的数有多少个? #include<iostream> #include<vector> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define LL __int64 const int maxn = 1e5+8; LL a[maxn],cn,numpri[maxn],vis[maxn]