HDU 1695 GCD (容斥 + 莫比乌斯反演)
2015-08-16 00:29
423 查看
GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7450 Accepted Submission(s): 2731
Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be very large, you're
only required to output the total number of different number pairs.
Please notice that, (x=5, y=7) and (x=7, y=5) are considered to be the same.
Yoiu can assume that a = c = 1 in all test cases.
Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 3,000 cases.
Each case contains five integers: a, b, c, d, k, 0 < a <= b <= 100,000, 0 < c <= d <= 100,000, 0 <= k <= 100,000, as described above.
Output
For each test case, print the number of choices. Use the format in the example.
Sample Input
2 1 3 1 5 1 1 11014 1 14409 9
Sample Output
Case 1: 9 Case 2: 736427 HintFor the first sample input, all the 9 pairs of numbers are (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5).
Source
2008 “Sunline Cup” National
Invitational Contest
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695
题目大意:尼玛,还特地补了句a=1,c=1。。。就是求从[1,b]和[1,d]中选出的二元组的最大公约数为k的组数,这里(a,b)和(b,a)算一种
题目分析:HDU的数据太水了,这题用分块求和优化和不做优化跑出来都是15ms,类似BZOJ 2301,比那个简单,不过比那个坑多了,不懂这个k为什么要从0开始。。。特判答案为0两种情况,一个是k=0,另一个是max(b, d) < k,把区间化成(1, b/k),(1, d/k),就是求两个区间各取出一个数使它们gcd为1的个数,剩下来的用莫比乌斯反演求就行了,然后就是这题的(a,b)和(b,a)算一种,因此要去重,方法很简单,比如第一个样例,区间1
5包含了1 3,因此要减去重复的部分,重复的部分就是cal(3, 3) / 2了,最后要用long long,算了下,极限数据答案是3e9左右,就超这么一点点,还真看不出来。。。
#include <cstdio> #include <cstring> #include <algorithm> #define ll long long using namespace std; int const MAX = 1e5 + 5; int mob[MAX], p[MAX], sum[MAX]; bool prime[MAX]; void Mobius() { int pnum = 0; memset(sum, 0, sizeof(sum)); memset(prime, true, sizeof(prime)); mob[1] = 1; sum[1] = 1; for(int i = 2; i < MAX; i++) { if(prime[i]) { p[pnum ++] = i; mob[i] = -1; } for(int j = 0; j < pnum && i * p[j] < MAX; j++) { prime[i * p[j]] = false; if(i % p[j] == 0) { mob[i * p[j]] = 0; break; } mob[i * p[j]] = -mob[i]; } sum[i] = sum[i - 1] + mob[i]; } } ll cal(int l, int r) { if(l > r) swap(l, r); ll ans = 0; for(int i = 1, last = 0; i <= l; i = last + 1) { last = min(l / (l / i), r / (r / i)); ans += (ll) (l / i) * (r / i) * (sum[last] - sum[i - 1]); } return ans; } int main() { Mobius(); int T; scanf("%d", &T); for(int ca = 1; ca <= T; ca++) { int a, b, c, d, k; scanf("%d %d %d %d %d", &a, &b, &c, &d, &k); if(k == 0 || max(b, d) < k) { printf("Case %d: 0\n", ca); continue; } int mi = min(b / k, d / k); printf("Case %d: %I64d\n", ca, cal(b / k, d / k) - cal(mi, mi) / 2); } }
相关文章推荐
- JAVA编程——static与final详解
- iOS开发-assign、retain、copy、strong、weak的区别
- 封装scrollView 循环滚动,tableViewCell(连载) mvc
- 交错正负数
- uva 1424 dp
- win7安装openssl,编译32位&64位
- Java基础之反射
- POJ - 3177 Redundant Paths(强连通分量)
- Gym - 100203I I WIN 网络流
- UVA 11584 dp
- 手动查杀skypee病毒(AutoIt3木马)
- 链队列的实现
- poj1035 spell checker (简单的字符串查找题)
- 费用流 poj2195 Going Home
- uva 1610 Party Games
- 2015华为软件精英挑战赛德州扑克之蒙特卡洛法模拟胜率
- iPhone入门开发系列2备忘录APP学习03--table view
- 基于火狐和谷歌浏览器的html5调用摄像头
- hdu5392 Infoplane in Tina Town(LCM)
- ECLIPSE @OVERRIDE报错解决 必须覆盖超类方法