GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7357    Accepted Submission(s): 2698

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
  1. 2
  2. 1 3 1 5 1
  3. 1 11014 1 14409 9
 
Sample Output
  1. Case 1: 9
  2. Case 2: 736427
  3. Hint
  4. 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).
  5.  
 
Source


题目大意:求出[a,b]和[c,d]区间里面gcd(x,y)=k的数的对数。

思路:既然是求gcd为k的数的对数,最好还是先将b和d都除以k,这样问题就转化为[1,n]和[1,m]区间里面gcd(x,y)为1 的数的对数。由于题目里已经说明a和c 能够觉得是1,这样就更简单了。

对于一个[1,n]的区间。我们能够用欧拉函数算出总对数。

那么问题就能够分解成2个:
1、在[1,n]上用欧拉函数算出总对数。

2、在[n+1,m]上。计算在[1,n]里面的总对数,能够用容斥原理。
  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<string.h>
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<vector>
  7. #define min(a,b) a<b?a:b
  8. #define max(a,b) a>b?
  9.  
  10. a:b
  11. #define Max 100005
  12. #define LL __int64
  13. using namespace std;
  14. LL sum[Max],tot;
  15. int p[Max][20];
  16. int num[Max];
  17. void init()
  18. {
  19. sum[1]=1;
  20. for(int i=2;i<Max;i++)
  21. sum[i]=i;
  22. for(int i=2;i<Max;i++)
  23. if(sum[i]==i)
  24. for(int j=i;j<Max;j+=i)
  25. sum[j]=sum[j]/i*(i-1);
  26.  
  27. }
  28. void init2()
  29. {
  30. LL x,k,i,j;
  31. for( i=1;i<=Max;i++)
  32. {
  33. x=i;k=0;
  34. for(j=2;j<=sqrt(i);j++)
  35. {
  36. if(x%j==0){
  37. while(x%j==0)x=x/j;
  38. // p[i].push_back(j);
  39. p[i][num[i]++]=j;
  40. }
  41. }
  42. if(x>1)p[i][num[i]++]=x;
  43. }
  44. }
  45. LL dfs(int n,int b,int x,int k)
  46. {
  47. LL ans=0;
  48. for(int i=x;i<k;i++)
  49. {
  50. ans+=b/p[n][i]-dfs(n,b/p[n][i],i+1,k);
  51. }
  52. return ans;
  53. }
  54. int main()
  55. {
  56. LL T,a,b,c,d,k;
  57. int i,j,t;
  58. init();
  59. init2();
  60. // printf("%I64d %I64d\n",sum[2],sum[3]);
  61. scanf("%I64d",&T);
  62. t=0;
  63. while(T--)
  64. {
  65. tot=0;
  66. t++;
  67. scanf("%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&k);
  68. printf("Case %d: ",t);
  69. if(k==0){printf("0\n");continue;}
  70. b=b/k;
  71. d=d/k;
  72. int m;
  73. m=min(b,d);
  74. d=max(b,d);
  75. b=m;
  76. for(i=1;i<=b;i++)
  77. tot=tot+sum[i];
  78. for(i=b+1;i<=d;i++)
  79. {
  80. // printf("%d\n",p[i].size());
  81. tot+=b-dfs(i,b,0,num[i]);
  82. }
  83. printf("%I64d\n",tot);
  84. }
  85. return 0;
  86. }


hdu 1695 GCD (欧拉函数、容斥原理)的更多相关文章

  1. hdu 1695 GCD (欧拉函数+容斥原理)

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

  2. HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a ...

  3. HDU 1695 GCD (欧拉函数,容斥原理)

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

  4. HDU 1695 GCD 欧拉函数+容斥定理

    输入a b c d k求有多少对x y 使得x在a-b区间 y在c-d区间 gcd(x, y) = k 此外a和c一定是1 由于gcd(x, y) == k 将b和d都除以k 题目转化为1到b/k 和 ...

  5. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

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

  6. hdu 1695 GCD 欧拉函数 + 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K]  和 [L ...

  7. HDU 2588 GCD (欧拉函数)

    GCD Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status De ...

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

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

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...

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

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

随机推荐

  1. 正则效验url

    上篇文章讲到多主题的解决方案:简单暴力的TP5多主题方案 为了简化配置,所以将域名前的协议 http/https 截取了. 后台配置时就需要效验配置的格式是否正确,需要用到的正则代码如下: /*** ...

  2. 题解 P3369 【【模板】普通平衡树】

    在网上某篇神奇的教程和@codesonic 大佬的标程帮助下,我又肝完了Leafy Tree,跑过来写篇题解(好像以前写过一篇?) 什么是Leafy Tree? Leafy Tree由两种节点组成:辅 ...

  3. jquery.validate动态更改校验规则 【转】

    有时候表单中有多个字段是相互关联的,以下遇到的就是证件类型和证件号码的关联,在下拉框中选择不同的证件类型,证件号码的值的格式都是不同的,这就需要动态的改变校验规则. <!DOCTYPE html ...

  4. Android Camera子系统之Linux C应用开发人员View

    Android Camera HAL通过V4L2接口与内核Camera Driver交互.本文从Linux应用开发人员的角度审视Android Camera子系统. V4L2应用开发一般流程: 1. ...

  5. C语言:constkeyword、结构体

    前几节内容的解说,主要是内存地址及指针的分析.这一节解说一下easy混淆的keywordconstant及结构体的知识. 一.constkeyword 1. 字符常量的指针 char const *p ...

  6. UVA 1415 - Gauss Prime(数论,高斯素数拓展)

    UVA 1415 - Gauss Prime 题目链接 题意:给定a + bi,推断是否是高斯素数,i = sqrt(-2). 思路:普通的高斯素数i = sqrt(-1),推断方法为: 1.假设a或 ...

  7. java学习记录笔记--继承,super,Object类

    继承: Java中的继承是单继承的. 1.子类拥有父类的全部属性和方法. 可是属性和方法的修饰符不能使private. 2.能够复用父类的代码. 方法的重写须要满足的条件: a.返回值类型 b.方法名 ...

  8. zzulioj--1778-- 和尚特烦恼4——有多少战斗力(gcd)

    1778: 和尚特烦恼4--有多少战斗力 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 259  Solved: 123 SubmitStatusWe ...

  9. 14.mocha+should.js

    转自http://www.ruanyifeng.com/blog/2015/12/a-mocha-tutorial-of-examples.html 众所周知对于任何一个项目来说,做好单元测试都是必不 ...

  10. Django关于图片验证码显示笔记

    .访问页面 /login/ - 内部需要创建一张图片,并且给用户返回 - 创建一个白板 Session存放验证码 .POST - 根据用户提交的数据与session里面比较 .登录界面 和 验证码 分 ...