1. GCD
  2. Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
  3. Total Submission(s): Accepted Submission(s):
  4.  
  5. Problem Description
  6. Given 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.
  7. Please notice that, (x=, y=) and (x=, y=) are considered to be the same.
  8.  
  9. Yoiu can assume that a = c = in all test cases.
  10.  
  11. Input
  12. The input consists of several test cases. The first line of the input is the number of the cases. There are no more than , cases.
  13. Each case contains five integers: a, b, c, d, k, < a <= b <= ,, < c <= d <= ,, <= k <= ,, as described above.
  14.  
  15. Output
  16. For each test case, print the number of choices. Use the format in the example.
  17.  
  18. Sample Input
  19.  
  20. Sample Output
  21.  
  22. Case :
  23. Case :
  24.  
  25. Hint
  26. For the first sample input, all the pairs of numbers are (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ).
  27.  
  28. /**
  29. 题目:hdu1695 GCD2
  30. 链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695
  31. 题意:求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。
  32. 思路:
  33. gcd(x,y)=k => gcd(x/k,y/k) = 1;
  34.  
  35. 则求x/k与y/k互质对数。
  36.  
  37. 即求:[1,b/k]与[1,d/k]之间互质的对数
  38.  
  39. 设x属于[1,b/k], y属于[1,d/k];
  40. 枚举x,求x与y互质的对数。所以要预处理所有x的质因子。然后容斥处理。由于(x,y)=>(5,7),(7,5)是同一组。
  41. 所以:答案为ans += (d/k) - x在d/k中不互质的数 - (x-1);
  42. */
  43. #include<iostream>
  44. #include<cstring>
  45. #include<algorithm>
  46. #include<cstdio>
  47. #include<vector>
  48. #include<map>
  49. #include<set>
  50. #include<cmath>
  51. #include<queue>
  52. #define LL long long
  53. using namespace std;
  54. typedef long long ll;
  55. typedef unsigned long long ull;
  56. const int maxn = 1e5+;
  57. vector<int> prime[maxn];
  58. int flag[maxn];
  59. void init()
  60. {
  61. memset(flag, , sizeof flag);
  62. for(ll i = ; i < maxn; i++){
  63. if(flag[i]==){
  64. prime[i].push_back(i);
  65. for(ll j = *i; j < maxn; j+=i){
  66. prime[j].push_back(i);
  67. flag[j] = ;
  68. }
  69. }
  70. }
  71. }
  72. ll rc(int pos,int n)
  73. {
  74. ll sum = ;
  75. ll mult, ones;
  76. ll len = prime[pos].size();
  77. ll m = <<len;
  78. for(int i = ; i < m; i++){
  79. ones = ;
  80. mult = ;
  81. for(int j = ; j < len; j++){
  82. if(i&(<<j)){
  83. ones++;
  84. mult = mult*prime[pos][j];
  85. if(mult>n) break;
  86. }
  87. }
  88. if(ones%==){
  89. sum -= n/mult-(pos-)/mult;
  90. }else
  91. {
  92. sum += n/mult-(pos-)/mult;
  93. }
  94. }
  95. return n-(pos-)-sum;
  96. }
  97. int main()
  98. {
  99. init();
  100. int T;
  101. int cas = ;
  102. int a, b, c, d, k;
  103. cin>>T;
  104. while(T--)
  105. {
  106. scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
  107. if(k==){
  108. printf("Case %d: 0\n",cas++);continue;
  109. }
  110. if(b>d) swap(b,d);///for b<=d;
  111. b = b/k;
  112. d = d/k;
  113. ll ans = ;
  114. if(b>=){
  115. ans += d;
  116. }
  117. for(int i = ; i <= b; i++){
  118. ans += rc(i,d);
  119. }
  120. printf("Case %d: %lld\n", cas++,ans);
  121. }
  122. return ;
  123. }

hdu1695 GCD2 容斥原理 求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。的更多相关文章

  1. GCD 莫比乌斯反演 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对.

    /** 题目:GCD 链接:https://vjudge.net/contest/178455#problem/E 题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对( ...

  2. hdu2588 GCD 给定n,m。求x属于[1,n]。有多少个x满足gcd(x,n)>=m; 容斥或者欧拉函数

    GCD Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted Sub ...

  3. 容斥原理 求M以内有多少个跟N是互质的

    开始系统的学习容斥原理!通常我们求1-n中与n互质的数的个数都是用欧拉函数! 但如果n比较大或者是求1-m中与n互质的数的个数等等问题,要想时间效率高的话还是用容斥原理!   本题是求[a,b]中与n ...

  4. Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数

    题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include < ...

  5. [hdu1695] GCD ——欧拉函数+容斥原理

    题目 给定两个区间[1, b], [1, d],统计数对的个数(x, y)满足: \(x \in [1, b]\), \(y \in [1, d]\) ; \(gcd(x, y) = k\) HDU1 ...

  6. ACM学习历程—HDU1695 GCD(容斥原理 || 莫比乌斯)

    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) = ...

  7. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  8. hdu1695 GCD 莫比乌斯反演做法+枚举除法的取值 (5,7),(7,5)看做同一对

    /** 题目:hdu1695 GCD 链接:http://acm.hdu.edu.cn/status.php 题意:对于给出的 n 个询问,每次求有多少个数对 (x,y) , 满足 a ≤ x ≤ b ...

  9. BZOJ 2005: [Noi2010]能量采集( 数论 + 容斥原理 )

    一个点(x, y)的能量损失为 (gcd(x, y) - 1) * 2 + 1 = gcd(x, y) *  2 - 1. 设g(i)为 gcd(x, y) = i ( 1 <= x <= ...

随机推荐

  1. 动态NAT地址转换

    1.配置路由器的端口ip地址(注意外网和内网ip地址的设置) Router(config)#inter f0/0 Router(config-if)#ip add 192.168.1.1 255.25 ...

  2. zip压缩与解压文件夹或文件

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  3. RMAN备份恢复 控制文件和归档日志丢失情况

    RMAN> backup current controlfile tag='bak_ctlfile' format='/home/oracle/backup/bak_ctl_%U_%T'; al ...

  4. centos7 安装nginx与配置

    第一步安装 使用Yum安装是推荐的方式,整体的流程非常的简单,也不容易出错,如果不需要什么特殊配置,建议使用Yum尽进行安装. 第一种安装方式,通过添加epel源 yum install epel-r ...

  5. hibernate4配置文件hibernate.cfg.xml配置详解

    <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configurat ...

  6. Can we say objects have attributes, states and behaviors?

    15down votefavorite 3 I was reading through Oracle's introduction to OOP concepts and I came across ...

  7. vue2计算属性computed

    详见vue2.0 API<计算属性> 需求: 模板内的表达式是非常便利的,但是它们实际上只用于简单的运算.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div id=&qu ...

  8. webmagic的多线程及线程池的应用

  9. 普通用户 crontab 任务不运行

    今天发如今linux下,普通用户的crontab任务不运行.网上搜了好多.好多说要在运行的脚本前面加上例如以下内容 if [ -f ~/.bash_profile ]; then   . ~/.bas ...

  10. python——异常except语句用法与引发异常

    except: #捕获所有异常 except: <异常名>: #捕获指定异常 except:<异常名1,异常名2):捕获异常1或者异常2 except:<异常名>,< ...