容斥原理详解 以及代码的剖析 结合实例hdu4135
2012-08-21 20:31
453 查看
原理:首先考虑一个问题,1000以内6,7,8,9的倍数有多少个?答案是
1000div6+1000div7+1000div8+1000div9
-1000div(6*7)-1000div(6*8)-1000div(6*9)-1000div(7*8)-1000div(7*9)-1000div(8*9)
+1000div(6*7*8)+1000div(6*8*9)+1000div(7*8*9)
-1000div(6*7*8*9)
这是容斥原理的一个最简单的应用,类比这道题,Step3到4其实将每个数a的不重复约数记录了下来,有公共约数的四个数的方案要从ans中减去,多减的要加上
奇加偶减
hdu 4135
Total Submission(s): 87 Accepted Submission(s): 39
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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).
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
1 10 2
3 15 5
[align=left]Sample Output[/align]
Case #1: 5
Case #2: 10
HintIn 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互质的数的个数
代码是参考人家的
参考地址
http://blog.csdn.net/duanxian0621/article/details/7303767
1000div6+1000div7+1000div8+1000div9
-1000div(6*7)-1000div(6*8)-1000div(6*9)-1000div(7*8)-1000div(7*9)-1000div(8*9)
+1000div(6*7*8)+1000div(6*8*9)+1000div(7*8*9)
-1000div(6*7*8*9)
这是容斥原理的一个最简单的应用,类比这道题,Step3到4其实将每个数a的不重复约数记录了下来,有公共约数的四个数的方案要从ans中减去,多减的要加上
奇加偶减
hdu 4135
Co-prime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 87 Accepted Submission(s): 39
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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).
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
1 10 2
3 15 5
[align=left]Sample Output[/align]
Case #1: 5
Case #2: 10
HintIn 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互质的数的个数
代码是参考人家的
参考地址
http://blog.csdn.net/duanxian0621/article/details/7303767
#include<iostream> #include<cstdio> #include<cstring> using namespace std; #define LL long long #define maxn 70 LL prime[maxn]; LL make_ans(LL num,int m)//1到num中的所有数与m个质因子不互质的个数 注意是不互质哦 { LL ans=0,tmp,i,j,flag; for(i=1;i<(LL)(1<<m);i++) { //用二进制来1,0来表示第几个素因子是否被用到,如m=3,三个因子是2,3,5,则i=3时二进制是011,表示第2、3个因子被用到 tmp=1,flag=0; for(j=0;j<m;j++) if(i&((LL)(1<<j)))//判断第几个因子目前被用到 flag++,tmp*=prime[j]; if(flag&1)//容斥原理,奇加偶减 ans+=num/tmp; else ans-=num/tmp; } return ans; } int main() { int T,t=0,m; LL n,a,b,i; scanf("%d",&T); while(T--) { scanf("%I64d%I64d%I64d",&a,&b,&n); m=0; for(i=2;i*i<=n;i++) //对n进行素因子分解 if(n&&n%i==0) { prime[m++]=i; while(n&&n%i==0) n/=i; } if(n>1) prime[m++]=n; printf("Case #%d: %I64d\n",++t,(b-make_ans(b,m))-(a-1-make_ans(a-1,m))); } return 0; }
相关文章推荐
- Spring结合java Quartz配置实例代码详解
- POST上传文件的原理剖析以及实例验证
- 结合实例详解clone()函数,Cloneable接口以及深拷贝与浅拷贝的问题
- LZ77压缩算法编码原理详解(结合图片和简单代码)
- Rsync服务器配置,安装、配置、实例以及原理详解(一)
- Rsync服务器配置,安装、配置、实例以及原理详解(一)
- Rsync服务器配置,安装、配置、实例以及原理详解(二)
- 堆排序算法原理以及实例代码
- Rsync服务器配置,安装、配置、实例以及原理详解(三)
- .net 邮件发送原理及实例以及smtp详解
- php mysql insert into 结合详解及实例代码
- Spring MVC代码实例系列-05:Spring MVC配置Log4j以及 log4j.properties 属性详解介绍
- yii2 添加 自定义 组件 custom component,以及模块 module 原理的详解剖析
- php mysql insert into 结合详解及实例代码
- Linux网络编程--epoll 模型原理详解以及实例
- 飞鸽传书原理以及相关代码详解
- Linux网络编程--epoll 模型原理详解以及实例
- LZ77压缩算法编码原理详解(结合图片和简单代码)
- 结合实例详解clone()函数,Cloneable接口以及深拷贝与浅拷贝的问题