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 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.

Input

The first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 1015) and (1 <=N <= 109).

Output

For each test case, print the number of integers between A and B inclusive which are relatively prime to N. Follow the output format below.

Sample Input

2
1 10 2
3 15 5

Sample Output

Case #1: 5
Case #2: 10

Hint

In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.

解题思路:求区间[A,B]与N互质的数的个数,我们可以从其对立面来考虑:分别求区间[1,A-1]、区间[1,B]中与N不互质的数的个数为num1、num2,那么区间[A,B]与N互质的数的个数就有(B-num2)-(A-1-num1)。怎么求出区间[1,X]与N不互质的数的个数呢?先分解出N的所有素因子,然后用这些素因子来筛选计算出区间[1,X]中与N不互质的数的个数即X/p_i(p_i为素因子,X为区间右端点),因为任何一个不小于2的数都能表示成若干个素数的乘积,这样就得到区间[1,X]中是素因子的倍数的个数,但为了不重复和不遗漏计数,应采用容斥定理,公式:,其中选择某几个素因子可以看成是二进制对应bit上的1,如果当前所选个数为奇数,符号为正,否则为负。注意:容斥计数x/p_i中p_i是几个素数的最小公倍数,由于素数之间是互质的,所以可以直接相乘起来作为其最小公倍数。
AC代码:
 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int t,cnt,prime[];LL a,b,n;
LL solve(LL x){//求与n不互质的总个数
int num;LL ans=,tp;
for(int i=;i<(<<cnt);++i){//用二进制来表示每个质因子是否被使用,即有2^cnt-1种可能,此时cnt较小,题目中1e9最多也就8个素因子,二进制优化
tp=,num=;
for(int j=;j<cnt;++j)
if(i&(<<j))num++,tp*=prime[j];//表示选择哪几个素因子
if(num&)ans+=x/tp;//奇加
else ans-=x/tp;//偶减
}
return x-ans;
}
int main(){
while(~scanf("%d",&t)){
for(int i=;i<=t;++i){
scanf("%lld%lld%lld",&a,&b,&n);cnt=;
for(LL j=;j*j<=n;++j){//求出n内的所有质因子
if(n%j==){
prime[cnt++]=j;
while(n%j==)n/=j;
}
}
if(n>)prime[cnt++]=n;
printf("Case #%d: %lld\n",i,solve(b)-solve(a-));//区间差
}
}
return ;
}

题解报告:hdu 4135 Co-prime(容斥定理入门)的更多相关文章

  1. HDU 1695 GCD(容斥定理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  2. HDU 4135 Co-prime(容斥:二进制解法)题解

    题意:给出[a,b]区间内与n互质的个数 思路:如果n比较小,我们可以用欧拉函数解决,但是n有1e9.要求区间内互质,我们可以先求前缀内互质个数,即[1,b]内与n互质,求互质,可以转化为求不互质,也 ...

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

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. BZOJ2839 : 集合计数 (广义容斥定理)

    题目 一个有 \(N\) 个 元素的集合有 \(2^N\) 个不同子集(包含空集), 现在要在这 \(2^N\) 个集合中取出若干集合(至少一个), 使得它们的交集的元素个数为 \(K\) ,求取法的 ...

  5. HDU 4135 Co-prime 欧拉+容斥定理

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. HDU - 4135 Co-prime 容斥定理

    题意:给定区间和n,求区间中与n互素的数的个数, . 思路:利用容斥定理求得先求得区间与n互素的数的个数,设表示区间中与n互素的数的个数, 那么区间中与n互素的数的个数等于.详细分析见求指定区间内与n ...

  7. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  8. hdu 6053 trick gcd 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意:给定一个数组,我们定义一个新的数组b满足bi<ai 求满足gcd(b1,b2....bn)&g ...

  9. HDU 2841 Visible Trees(容斥定理)

    Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

随机推荐

  1. 【bzoj2761】【JLOI2011】【不反复数字】【平衡树】

    Description 给出N个数,要求把当中反复的去掉.仅仅保留第一次出现的数. 比如,给出的数为1 2 18 3 3 19 2 3 6 5 4.当中2和3有反复.去除后的结果为1 2 18 3 1 ...

  2. iframe 框架中 父子界面的JS调用

    子界面调用父界面 window.parent.hello(); 父界面调用子界面 window.frmaes[i].hello();

  3. HTML——使用表格进行页面布局

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 关于在PHP中当一个请求未完成时,再发起另一个请求被阻塞的问题

    最近做项目的时候遇到个问题,就是做阿里云oss大文件上传进度条显示,因为要实时查询上传分片进度,所以在上传的同时必须要再发起查询的请求,但是一直都是所有分片上传完成后查询的请求才执行,刚开始以为是阿里 ...

  5. react native与原生的交互

    一.交互依赖的重要组件 react native 中如果想要调用ios  中相关的方法,必须依赖一个重要的组件nativemodules import { NativeModules } from ' ...

  6. 各种DP总结

    一.数位DP 1.含有或不含某个数“xx”: HDU3555 Bomb HDU2089 不要62 2.满足某些条件,如能整除某个数,或者数位上保持某种特性: HDU3652 B-number Code ...

  7. YTU 2481: 01字串

    2481: 01字串 时间限制: 1 Sec  内存限制: 128 MB 提交: 103  解决: 72 题目描述 对于长度为7位的一个01串,每一位都可能是0或1,一共有128种可能.它们的前几个是 ...

  8. vi编辑器设置行号可见

    vi 设置行号 需要切换到命令模式下,输入set number :set number 按下回车即可

  9. ewasm项目初探

    为了改进EVM1.0,以太坊的新一代虚拟机项目ewasm (github.com/ewasm)将支持WebAssembly(wasm),wasm在性能,扩展性,开发工具,社区都更有优势.除以太坊外,一 ...

  10. Elasticsearch 安装配置 外网访问 及 后台启动

    本文转自http://www.jianshu.com/p/658961f707d8 作者:咪博士 感谢咪博士分享 Elasticsearch的安装总体来说还是相当简单的,当然中间也会有些小坑.不过大家 ...