HDU 4135 Co-prime (容斥原理, 数学)
2016-04-19 12:29
399 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4135
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.
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
2
1 10 2
3 15 5
Sample Output
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}.
Source
The Third Lebanese Collegiate Programming
Contest
题意:
给出a、b、n,求a 到 b 的区间内与 n 互质的数。
PS:
http://www.cnblogs.com/jiangjing/archive/2013/06/03/3115470.html
http://www.cnblogs.com/jackge/archive/2013/04/02/2995238.html
分析:我们可以先转化下:用(1,b)区间与n互质的数的个数减去(1,a-1)区间与n互质的数的个数,那么现在就转化成求(1,m)区间于n互质的数的个数,如果要求的是(1,n)区间与n互质的数的个数的话,我们直接求出n的欧拉函数值即可,可是这里是行不通的!我们不妨换一种思路:就是求出(1,m)区间与n不互质的数的个数,假设为num,那么我们的答案就是:m-num!现在的关键就是:怎样用一种最快的方法求出(1,m)区间与n不互质的数的个数?方法实现:我们先求出n的质因子(因为任何一个数都可以分解成若干个质数相乘的),如何尽快地求出n的质因子呢?我们这里又涉及两个好的算法了!第一个:用于每次只能求出一个数的质因子,适用于题目中给的n的个数不是很多,但是n又特别大的;(http://www.cnblogs.com/jiangjing/archive/2013/06/03/3115399.html)第二个:一次求出1~n的所有数的质因子,适用于题目中给的n个数比较多的,但是n不是很大的。(http://www.cnblogs.com/jiangjing/archive/2013/06/01/3112035.html)本题适用第一个算法!举一组实例吧:假设m=12,n=30.
第一步:求出n的质因子:2,3,5;
第二步:(1,m)中是n的因子的倍数当然就不互质了(2,4,6,8,10)->n/2 6个,(3,6,9,12)->n/3 4个,(5,10)->n/5 2个。
如果是粗心的同学就把它们全部加起来就是:6+4+2=12个了,那你就大错特错了,里面明显出现了重复的,我们现在要处理的就是如何去掉那些重复的了!
第三步:这里就需要用到容斥原理了,公式就是:n/2+n/3+n/5-n/(2*3)-n/(2*5)-n/(3*5)+n/(2*3*5).
第四步:我们该如何实现呢?我在网上看到有几种实现方法:dfs(深搜),队列数组,位运算三种方法都可以!上述公式有一个特点:n除以奇数个数相乘的时候是加,n除以偶数个数相乘的时候是减。我这里就写下用队列数组如何实现吧:我们可以把第一个元素设为-1然后具体看代码如何实现吧!
代码如下:
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.
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
2
1 10 2
3 15 5
Sample Output
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}.
Source
The Third Lebanese Collegiate Programming
Contest
题意:
给出a、b、n,求a 到 b 的区间内与 n 互质的数。
PS:
http://www.cnblogs.com/jiangjing/archive/2013/06/03/3115470.html
http://www.cnblogs.com/jackge/archive/2013/04/02/2995238.html
分析:我们可以先转化下:用(1,b)区间与n互质的数的个数减去(1,a-1)区间与n互质的数的个数,那么现在就转化成求(1,m)区间于n互质的数的个数,如果要求的是(1,n)区间与n互质的数的个数的话,我们直接求出n的欧拉函数值即可,可是这里是行不通的!我们不妨换一种思路:就是求出(1,m)区间与n不互质的数的个数,假设为num,那么我们的答案就是:m-num!现在的关键就是:怎样用一种最快的方法求出(1,m)区间与n不互质的数的个数?方法实现:我们先求出n的质因子(因为任何一个数都可以分解成若干个质数相乘的),如何尽快地求出n的质因子呢?我们这里又涉及两个好的算法了!第一个:用于每次只能求出一个数的质因子,适用于题目中给的n的个数不是很多,但是n又特别大的;(http://www.cnblogs.com/jiangjing/archive/2013/06/03/3115399.html)第二个:一次求出1~n的所有数的质因子,适用于题目中给的n个数比较多的,但是n不是很大的。(http://www.cnblogs.com/jiangjing/archive/2013/06/01/3112035.html)本题适用第一个算法!举一组实例吧:假设m=12,n=30.
第一步:求出n的质因子:2,3,5;
第二步:(1,m)中是n的因子的倍数当然就不互质了(2,4,6,8,10)->n/2 6个,(3,6,9,12)->n/3 4个,(5,10)->n/5 2个。
如果是粗心的同学就把它们全部加起来就是:6+4+2=12个了,那你就大错特错了,里面明显出现了重复的,我们现在要处理的就是如何去掉那些重复的了!
第三步:这里就需要用到容斥原理了,公式就是:n/2+n/3+n/5-n/(2*3)-n/(2*5)-n/(3*5)+n/(2*3*5).
第四步:我们该如何实现呢?我在网上看到有几种实现方法:dfs(深搜),队列数组,位运算三种方法都可以!上述公式有一个特点:n除以奇数个数相乘的时候是加,n除以偶数个数相乘的时候是减。我这里就写下用队列数组如何实现吧:我们可以把第一个元素设为-1然后具体看代码如何实现吧!
代码如下:
#include <cstdio> #include <cstring> #include <vector> #include <iostream> #include <algorithm> using namespace std; #define LL long long vector<LL> v; LL a, b, n; LL solve(LL x, LL n)//[1, x]区间与n互质的个数 { v.clear(); for(LL i = 2; i*i <= n; i++) //求n的质因子(素数因子) { if(n%i==0) { v.push_back(i); while(n%i==0) { n/=i; } } } if(n > 1) v.push_back(n); LL tsum = 0; LL tval, cnt; //用二进制来1,0来表示第几个素因子是否被用到,如n = 30,三个因子是2,3,5, //则i=3时二进制是011,表示第2、3个因子被用到 for(LL i = 1; i < (1<<v.size()); i++) { tval = 1; cnt = 0; for(LL j = 0; j < v.size(); j++) { if(i & (1<<j)) //判断目前第几个因子被用到 { tval*=v[j]; cnt++; } } if(cnt&1) //容斥,奇加偶减 tsum+=x/tval; else tsum-=x/tval; } return x-tsum; } int main() { int t; int cas = 0; scanf("%d",&t); while(t--) { scanf("%lld%lld%lld",&a,&b,&n); LL ans = solve(b,n) - solve(a-1,n); printf("Case #%d: %lld\n",++cas,ans); } return 0; }
相关文章推荐
- [数学] BZOJ 4544 椭圆上的整点
- C++ WINDOWS API 如何使用NMAKE和CL编译
- 136. Single Number
- <Pro WPF 4.5 in C#> - Note-02
- EAS性能优化
- android edittext 去边框 去下划线
- web页面记住密码存在安全问题 - 处理方式
- 26Sudoku Killer
- java开发学习工具,必须收藏
- oracle基础实战笔记
- php命名空间的设计思想和缺点
- [线段树]分数修改
- Linux共享Windows文件夹
- 将retrofit2和rxjava的初始化配置到application
- 【Leetcode】:238. Product of Array Except Self 问题 in JAVA
- 1017
- ES配置文件
- Qt 使用statusBar来显示程序打印信息
- RecyclerView解决加载和刷新
- (LeetCode 299) Bulls and Cows (HashTable)