GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17385    Accepted Submission(s): 6699

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

Hint

For 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
 
就是求 [1, b / k]  和 [1, d / k] 互质数的对数
 先用欧拉函数求出 相同区间的互质的对数 多出来的 用容斥去求
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <sstream>
  4. #include <cstring>
  5. #include <map>
  6. #include <cctype>
  7. #include <set>
  8. #include <vector>
  9. #include <stack>
  10. #include <queue>
  11. #include <algorithm>
  12. #include <cmath>
  13. #include <bitset>
  14. #define rap(i, a, n) for(int i=a; i<=n; i++)
  15. #define rep(i, a, n) for(int i=a; i<n; i++)
  16. #define lap(i, a, n) for(int i=n; i>=a; i--)
  17. #define lep(i, a, n) for(int i=n; i>a; i--)
  18. #define rd(a) scanf("%d", &a)
  19. #define rlld(a) scanf("%lld", &a)
  20. #define rc(a) scanf("%c", &a)
  21. #define rs(a) scanf("%s", a)
  22. #define rb(a) scanf("%lf", &a)
  23. #define rf(a) scanf("%f", &a)
  24. #define pd(a) printf("%d\n", a)
  25. #define plld(a) printf("%lld\n", a)
  26. #define pc(a) printf("%c\n", a)
  27. #define ps(a) printf("%s\n", a)
  28. #define MOD 2018
  29. #define LL long long
  30. #define ULL unsigned long long
  31. #define Pair pair<int, int>
  32. #define mem(a, b) memset(a, b, sizeof(a))
  33. #define _ ios_base::sync_with_stdio(0),cin.tie(0)
  34. //freopen("1.txt", "r", stdin);
  35. using namespace std;
  36. const int maxn = , INF = 0x7fffffff;
  37. int ans;
  38. LL tot[maxn + ];
  39. int prime[maxn+], phi[maxn+];
  40. bool vis[maxn+];
  41. void getphi()
  42. {
  43. ans = ;
  44. phi[] = ;
  45. for(int i=; i<=maxn; i++)
  46. {
  47. if(!vis[i])
  48. {
  49. prime[++ans] = i;
  50. phi[i] = i - ;
  51. }
  52. for(int j=; j<=ans; j++)
  53. {
  54. if(i * prime[j] > maxn) break;
  55. vis[i * prime[j]] = ;
  56. if(i % prime[j] == )
  57. {
  58.  
  59. phi[i * prime[j]] = phi[i] * prime[j]; break;
  60. }
  61. else
  62. phi[i * prime[j]] = phi[i] * (prime[j] - );
  63. }
  64. }
  65. }
  66.  
  67. int get_cnt(int n, int m)
  68. {
  69. int ans = ;
  70. for(int i = ; i * i <= n; i++)
  71. {
  72. if(n % i) continue;
  73. while(n % i == ) n /= i;
  74. prime[ans++] = i;
  75. }
  76. if(n != ) prime[ans++] = n;
  77. int res = ;
  78. for(int i = ; i < ( << ans); i++)
  79. {
  80. int tmp = , cnt2 = ;
  81. for(int j = ; j < ans; j++)
  82. {
  83. if(((i >> j) & ) == ) continue;
  84. tmp *= prime[j];
  85. cnt2++;
  86. }
  87. if(cnt2 & ) res += m / tmp;
  88. else res -= m / tmp;
  89. }
  90. return m - res;
  91. }
  92.  
  93. int main()
  94. {
  95. getphi();
  96. int a, b, c, d, k;
  97. for(int i = ; i < maxn; i++)
  98. {
  99. tot[i] = tot[i - ] + phi[i];
  100.  
  101. }
  102. int T, kase = ;
  103. cin >> T;
  104. while(T--)
  105. {
  106. scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
  107. if(k == )
  108. {
  109. printf("Case %d: 0\n", ++kase);
  110. continue;
  111. }
  112. int n = b / k, m = d / k;
  113. LL sum = tot[n > m ? m : n];
  114. // cout << sum << endl;
  115. if(m > n) swap(n, m);
  116. for(int i =m + ; i <= n; i++)
  117. {
  118. sum += get_cnt(i, m);
  119. }
  120. printf("Case %d: %lld\n", ++kase, sum);
  121. }
  122.  
  123. return ;
  124. }

GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17385    Accepted Submission(s): 6699

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

Hint

For 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

GCD HDU - 1695 (欧拉 + 容斥)的更多相关文章

  1. D - GCD HDU - 1695 -模板-莫比乌斯容斥

    D - GCD HDU - 1695 思路: 都 除以 k 后转化为  1-b/k    1-d/k中找互质的对数,但是需要去重一下  (x,y)  (y,x) 这种情况. 这种情况出现 x  ,y ...

  2. HDU 4135 Co-prime 欧拉+容斥定理

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  3. hdu 1695 欧拉函数+容斥原理

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  4. hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

    http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...

  5. GCD nyoj 1007 (欧拉函数+欧几里得)

    GCD  nyoj 1007 (欧拉函数+欧几里得) GCD 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 The greatest common divisor ...

  6. HDU 3970 Harmonious Set 容斥欧拉函数

    pid=3970">链接 题解:www.cygmasot.com/index.php/2015/08/17/hdu_3970 给定n  求连续整数[0,n), 中随意选一些数使得选出的 ...

  7. HDU 1695 GCD (容斥原理+欧拉函数)

    题目链接 题意 : 从[a,b]中找一个x,[c,d]中找一个y,要求GCD(x,y)= k.求满足这样条件的(x,y)的对数.(3,5)和(5,3)视为一组样例 . 思路 :要求满足GCD(x,y) ...

  8. HDU1695:GCD(容斥原理+欧拉函数+质因数分解)好题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题目解析: Given 5 integers: a, b, c, d, k, you're to ...

  9. HDU 4135 Co-prime(容斥:二进制解法)题解

    题意:给出[a,b]区间内与n互质的个数 思路:如果n比较小,我们可以用欧拉函数解决,但是n有1e9.要求区间内互质,我们可以先求前缀内互质个数,即[1,b]内与n互质,求互质,可以转化为求不互质,也 ...

随机推荐

  1. 《React Native 精解与实战》书籍连载「Node.js 简介与 React Native 开发环境配置」

    此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...

  2. Linux命令(一)

    需要用Xshell连接Linux时: 先在终端输入命令:service  sshd  start(开启ssh服务) 1.netstat -tnl:查看端口状态的命令(如 查看22端口) 2.servi ...

  3. js判断一个对象{}是否为空对象,没有任何属性

    // js如何判断一个对象{}是否为空对象,没有任何属性 if (typeof model.rows === "object" && !(model.rows in ...

  4. C#的类型推断发展史

    前言:随着C#的版本升级,C#编译器的类型推断功能也在不断的升级以适应语言进化过程中的变化,并为这个过程做了相应的优化. 隐式类型的数组 在C#1和C#2中,作为变量声明和初始化的一部分,初始化数组的 ...

  5. iphone 分辨率相关

    iPhone 1G 320x480 iPhone 3G 320x480 iPhone 3GS 320x480 iPhone 4 640x960 iPhone 4S 640x960 iPhone 5 6 ...

  6. 缓存session,cookie,sessionStorage,localStorage的区别

    https://www.cnblogs.com/cencenyue/p/7604651.html(copy) 浅谈session,cookie,sessionStorage,localStorage的 ...

  7. Failed to bind properties under 'spring.datasource' to javax.sql.DataSource

    这是我的配置文件 # 国际化配置文件(包名.基础名) spring.messages.basename=i18n.login server.tomcat.uri-encoding=UTF- sprin ...

  8. Linux基础学习笔记3-用户权限

    本章内容 用户user 令牌token,identity Linux用户:Uername/UID 管理员:root,0 普通用户:1-65535 系统用户:1-499,1-999(Centos7) 对 ...

  9. MyCat数据库中间件 - 分库

    MyCat MyCat用于解耦分布式数据库与java,比如分库分表以后,需要查询某条数据时,需要java根据需要查的数据先计算去哪个库查,然而有了Mycat就不用自己计算怎么存储,怎么查询了.MyCa ...

  10. 用mescroll实现无限上拉增加数据,下拉刷新数据 (学习笔记)

    最近自己做一个web app需要用到上拉查询下页数据,网上看了很多很多帖子,发现并不能快速的套用,总是会出现各种问题无法使用,于是无奈自己跑去看了官方api文档,终于做了出来,至此做个笔记,以后用到可 ...