【HDU-4135】Co-prime
来源:互联网 发布:8090端口是干嘛的 编辑:程序博客网 时间:2024/06/05 09:18
点击打开链接
Co-prime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5468 Accepted Submission(s): 2181
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.
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
21 10 23 15 5
Sample Output
Case #1: 5Case #2: 10HintIn 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~b : a11~a-1 : a2 ans = a1 - a2----> [1,k] 与 n 互质的数的个数----> num - 与n不互质的数的个数*/#include<cstdio>#include<cmath>int p[100000];int ant;void ResolvePrime(int n)//分解质因数 {int endd = sqrt(n);for (int i = 2 ; i <= endd ; i++){if (n % i == 0){p[ant++] = i;while (n % i == 0)n /= i;}}if (n > 1)p[ant++] = n;}__int64 solve(__int64 a,int n)//求 1~ a 不与n互质的数的个数 {__int64 ans = 0;for (int i = 1 ; i < ((__int64)1 << ant) ; i++){__int64 mul = 1;int cnt = 0;for (int j = 0 ; j < ant ; j++){if (i & ((__int64)1 << j))//选中 {cnt++;mul *= p[j];}}if (cnt & 1)ans += a / mul;elseans -= a / mul;}return ans;}int main(){int T;int n;__int64 a,b;int Case = 1;scanf ("%d",&T);while (T--){ant = 0;scanf ("%I64d %I64d %d",&a,&b,&n);ResolvePrime(n);printf ("Case #%d: %I64d\n",Case++,b-(a-1)-(solve(b,n)-solve(a-1,n)));}return 0;}
阅读全文
0 0
- hdu 4135 co-prime
- HDU 4135 Co-prime
- HDU - 4135 Co-prime
- HDU 4135 Co-prime
- hdu 4135 Co-prime
- HDU 4135 Co-prime
- 【HDU】 4135 Co-prime
- HDU - 4135 Co-prime
- HDU 4135 Co-prime
- HDU-4135-Co-prime
- HDU 4135 Co-prime
- 【hdu 4135】Co-prime
- HDU 4135 Co-prime
- 【HDU-4135】Co-prime
- HDU 4135——Co-prime
- [容斥原理] hdu 4135 Co-prime
- 【HDU】4135 Co-prime 容斥原理
- hdu 4135 Co-prime(容斥原理)
- hibernate简单应用
- HDU 4417 Super Mario (树状数组 + 离线)
- 问题 D: 地宫取宝
- 12个小球中,有一个小球质量M与其他11个小球不同,只能测试3次,怎么可以找出它?
- ZOJ 3469 Food Delivery (区间DP)
- 【HDU-4135】Co-prime
- CGI详解
- 冒泡排序
- Uva 136 Ugly Numbers(丑数)
- C++ sizeof的使用总结
- 网络协议篇之CWMP协议(一)—— CWMP(TR-069)基础
- 图的割点(简易邻接矩阵)C~
- HDUOJ 1232 畅通工程(并查集)
- REST Client自动化测试RESTful API