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.

容斥原理

 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long ll; int pnum[]; int main(){
int T;
scanf("%d",&T);
for(int q=;q<=T;++q){
ll a,b;
int n;
scanf("%lld%lld%d",&a,&b,&n);
a--;
ll ans=;
int cnt=;
for(int i=;i*(ll)i<=n;++i){
if(!(n%i)){
pnum[++cnt]=i;
while(!(n%i))n/=i;
}
}
if(n>)pnum[++cnt]=n;
for(int i=;i<(<<cnt);++i){
int bit=;
int mul=;
for(int j=;j<=cnt;++j){
if(i&(<<(j-))){
bit++;
mul*=pnum[j];
}
}
if(bit%)ans+=b/mul-a/mul;
else ans-=b/mul-a/mul;
}
printf("Case #%d: %lld\n",q,b-a-ans);
}
return ;
}

hdu4135 Co-prime 容斥原理的更多相关文章

  1. HDU4135 Co-prime(容斥原理)

    题目求[A,B]区间内与N互质数的个数. 可以通过求出区间内与N互质数的个数的前缀和,即[1,X],来得出[A,B]. 那么现在问题是求出[1,X]区间内与N互质数的个数,考虑这个问题的逆问题:[1, ...

  2. hdu4135 Co-prime【容斥原理】

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

  3. [HDU4135]CO Prime(容斥)

    也许更好的阅读体验 \(\mathcal{Description}\) \(t\)组询问,每次询问\(l,r,k\),问\([l,r]\)内有多少数与\(k\)互质 \(0<l<=r< ...

  4. hdu4135容斥原理 组合遍历

    容斥原理实现的关键在于:组合遍历,即如何遍历2^n种组合. 容斥原理的三种写法: DFS 队列数组 位数组 #include<stdio.h> #include<iostream&g ...

  5. HDU4135(容斥原理)

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

  6. 容斥原理的(二进制思想和质因子分解+模板)hdu4135+ecf81.D

    题:http://acm.hdu.edu.cn/showproblem.php?pid=4135 题意:求[A,B]与N互质的数的个数 #include<iostream> #includ ...

  7. Codeforces1036F Relatively Prime Powers 【容斥原理】

    题目分析: 这种题目标题写莫比乌斯反演会不会显得太恐怖了,那就容斥算了. gcd不为1的肯定可以开根.所以把根式结果算出来就行了. 辣鸡题目卡我精度. 代码: #include<bits/std ...

  8. HDU4135容斥原理

    #include <cstdio> #include <string.h> #include <cmath> using namespace std; #defin ...

  9. hdu4059 The Boss on Mars(差分+容斥原理)

    题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设  则    为一阶差分. 二阶差分: n阶差分:     且可推出    性质: 1. ...

随机推荐

  1. nginx:负载均衡实战(四)nginx+keepalived配置双机热备

    1.下载安装 下载keepalived地址:http://www.keepalived.org/download.html 解压安装: tar -zxvf keepalived-.tar.gz 安装o ...

  2. Java:将Excel数据导入到数据库(一眼就看会)

    所用Jar包 1. sqljdbc4.jar 连接数据库的Jar包(根据数据库的不同进行选择,我用的SqlServer2008) 2.Jxl.jar 访问Excel的Jar包 注意:支持以.xls结尾 ...

  3. 3.10 C++虚基类 虚继承

    参考:http://www.weixueyuan.net/view/6367.html 总结: 本例即为典型的菱形继承结构,类A中的成员变量及成员函数继承到类D中均会产生两份,这样的命名冲突非常的棘手 ...

  4. UFT测试本地应用程序登陆小实例(描述性编程)

    Dim username,password Dim casecount,i Dim currentid DataTable.ImportSheet ,"Action1" casec ...

  5. Iterator 与ListIterator的区别

    Iterator 与ListIterator的区别: 1.Iterator能够迭代Set和List集合的元素,而ListIterator只能迭代List集合的元素 2.Iterator只能前向迭代,L ...

  6. 牛客多校第四场 A Ternary String

    题目描述 A ternary string is a sequence of digits, where each digit is either 0, 1, or 2. Chiaki has a t ...

  7. android-DNS服务找不到

    1.重启eclipse 2.重新建立AVD 3.在建立AVD时sd卡数值不要填

  8. shiro学习(二)身份验证

    身份验证,即在应用中谁能证明他就是他本人.一般提供如他们的身份ID一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明. 在shiro中,用户需要提供principals (身份)和cre ...

  9. mysql的sql语句的性能诊断分析

    1> explain SQL,类似于Oracle中explain语句 例如:explain select * from nad_aditem; 2> select benchmark(co ...

  10. 使用Maven+ssm框架搭建一个web项目

    1,前期准备:Eclipse(Mars.2 Release (4.5.2)).jdk1.7.tomcat7.maven3.2.1 2.使用eclipse中的maven新建一个web项目 点击next: ...