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. Win10系列:VC++ Direct3D模板介绍2

    (3)CreateDeviceResources函数 CreateDeviceResources函数默认添加在CubeRenderer.cpp源文件中,此函数用于创建着色器和立体图形顶点.接下来分别介 ...

  2. 【资料搜集】Python学习

    python学习手册 | 演道网 http://dev.go2live.cn/python/python%e5%ad%a6%e4%b9%a0%e6%89%8b%e5%86%8c.html

  3. 【收集资料】OpenGL学习

    1.课本配套网站,有Sample Code和书中的图片等   http://math.ucsd.edu/~sbuss/MathCG/ 2.Visual C++的帮助系统可查阅基本OpenGL函数(不包 ...

  4. java 实现简单循环队列

    package www.queue; import java.util.Arrays; /** * 循环队列: * 循环队列的出现是为了解决顺序队列出队列后,首指针向后移动后前面的存储过程浪费不能使用 ...

  5. centos7中docker操作

    docker部署nginx 1. 下载nginx [root@localhost my.Shells]# docker images REPOSITORY TAG IMAGE ID CREATED S ...

  6. 使用DevExpress Reports和PDF Viewer创建AcroForm Designer

    众所周知,交互式表单(AcroForms)是PDF标准的重要组成部分,AcroForms允许开发者和开发者的用户创建可填写的PDF文档.尽管WinForms和WPF PDF Viewers不支持交互式 ...

  7. day 67 django 之ORM 增删改查基础

    一 操作基础前提准备 1. 新建django 项目 mysite  子项目app01 ,选择好做路径. 2  .2-1在app01 下面models 中引用 模块 from  django.db im ...

  8. Pycharm连接Git及使用

    环境: Git-2.7.2-32-bit_setup.1457942412.exe TortoiseGit-2.4.0.2-64bit.msi 安装配置Git后,打开Pycharm.file--> ...

  9. Day17作业及默写

    正则表达式练习 1.匹配一篇英文文章的标题 类似 The Voice Of China ([A-Z][a-z]*)( [A-Z][a-z]*)* 2.匹配一个网址 https://www.baidu. ...

  10. python day06 作业答案

    1. count=1 while count<11: fen=input('请第{}个评委打分' .format( count)) if int(fen) >5 and int(fen) ...