HDU4135(容斥原理)
2016-08-27 14:57
183 查看
Co-prime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4090 Accepted Submission(s): 1619
[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
思路:求1~m中与n互素的数的个数。
#include <cstdio> #include <vector> using namespace std; typedef long long LL; LL sieve(LL m,LL n) { vector<LL> divisor; for(LL i=2;i*i<=n;i++) { if(n%i==0) { divisor.push_back(i); while(n%i==0) n/=i; } } if(n>1) divisor.push_back(n); LL ans=0; for(LL mark=1;mark<(1<<divisor.size());mark++) { LL odd=0; LL mul=1; for(LL i=0;i<divisor.size();i++) { if(mark&(1<<i)) { mul*=divisor[i]; odd++; } } LL cnt=m/mul; if(odd&1) ans+=cnt; else ans-=cnt; } return m-ans; } LL a,b,n; int main() { int T; scanf("%d",&T); for(int cas=1;cas<=T;cas++) { scanf("%lld%lld%lld",&a,&b,&n); LL res=sieve(b,n)-sieve(a-1,n); printf("Case #%d: ",cas); printf("%lld\n",res); } return 0; }
相关文章推荐
- 使用poi向excel中插入内容遇到的问题总结
- #79 Longest Common Substring
- Apache StratOS 部分功能介绍
- Android Binder线程
- 生活 ▎在“宅”的道路上越走越远!
- CSS3系列之3D制作 再研究
- 使用 zssh 进行 Zmodem 文件传输
- 【 D3.js 入门系列 --- 2 】 如何使用数据和选择元素
- C#用NPOI读取excel
- 同胚
- 公司内部对ID转义
- MVP模式的学习
- Yii2 配置yii2-redis扩展
- android深度理解ListView notifyDataSetChanged()不刷新
- EntityFramework Code-First 简易教程(十一)-------从已存在的数据库中映射出表
- 自己封装的一个LoadRes组件
- AndroidStudio在断点时打印日志
- leetcode-java-219. Contains Duplicate II
- linux安装svn服务端
- 九度OJ题目1201:二叉排序树