题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019

Problem Description
In mathematics, the greatest common divisor (gcd), also known as the greatest common factor (gcf), highest common factor (hcf), or greatest common measure (gcm), of two or more integers (when at least one of them is not zero), is the largest positive integer
that divides the numbers without a remainder.

---Wikipedia



Today, GCD takes revenge on you. You have to figure out the k-th GCD of X and Y.
 
Input
The first line contains a single integer T, indicating the number of test cases. 



Each test case only contains three integers X, Y and K.



[Technical Specification]

1. 1 <= T <= 100

2. 1 <= X, Y, K <= 1 000 000 000 000
 
Output
For each test case, output the k-th GCD of X and Y. If no such integer exists, output -1.
 
Sample Input
  1. 3
  2. 2 3 1
  3. 2 3 2
  4. 8 16 3
 
Sample Output
  1. 1
  2. -1
  3. 2
 
Source

官方题解:

代码例如以下:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <vector>
  7. using namespace std;
  8. typedef __int64 LL;
  9. vector<LL>v;
  10. LL GCD(LL a, LL b)
  11. {
  12. if(b == 0)
  13. return a;
  14. return GCD(b,a%b);
  15. }
  16.  
  17. int main()
  18. {
  19. int t;
  20. LL x, y, k;
  21. scanf("%d",&t);
  22. while(t--)
  23. {
  24. v.clear();
  25. scanf("%I64d%I64d%I64d",&x,&y,&k);
  26. LL tt = GCD(x,y);
  27. // printf("%I64d\n",tt);
  28. for(LL i = 1; i*i <= tt; i++)
  29. {
  30. if(tt%i == 0)
  31. {
  32. v.push_back(i);
  33. if(i*i != tt)//防止放入两个i
  34. v.push_back(tt/i);
  35. }
  36. }
  37. sort(v.begin(), v.end());
  38. if(k > v.size())
  39. printf("-1\n");
  40. else
  41. printf("%I64d\n",v[v.size()-k]);
  42. }
  43. return 0;
  44. }

HDU 5019 Revenge of GCD(数学)的更多相关文章

  1. 数学--数论--HDU 5019 revenge of GCD

    Revenge of GCD Problem Description In mathematics, the greatest common divisor (gcd), also known as ...

  2. HDU 5019 Revenge of GCD

    题解:筛出约数,然后计算即可. #include <cstdio> #include <algorithm> typedef long long LL; LL a1[10000 ...

  3. HDOJ 5019 Revenge of GCD

    Revenge of GCD In mathematics, the greatest common divisor (gcd), also known as the greatest common ...

  4. hdu 5018 Revenge of GCD

    题意: 给你两个数:X和Y  .输出它们的第K大公约数.若不存在输出 -1 数据范围: 1 <= X, Y, K <= 1 000 000 000 000 思路: 它俩的公约数一定是gcd ...

  5. hdu 5869 区间不同GCD个数(树状数组)

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  6. hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)

    CA Loves GCD  Accepts: 64  Submissions: 535  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: 2 ...

  7. hdu 4983 Goffi and GCD(数论)

    题目链接:hdu 4983 Goffi and GCD 题目大意:求有多少对元组满足题目中的公式. 解题思路: n = 1或者k=2时:答案为1 k > 2时:答案为0(n≠1) k = 1时: ...

  8. hdu 5019(第K大公约数)

    Revenge of GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. HDU 4983 Goffi and GCD(数论)

    HDU 4983 Goffi and GCD 思路:数论题.假设k为2和n为1.那么仅仅可能1种.其它的k > 2就是0种,那么事实上仅仅要考虑k = 1的情况了.k = 1的时候,枚举n的因子 ...

随机推荐

  1. 解决android.os.NetworkOnMainThreadException

    好久不写Android代码手都生了,找出自己之前写的程序发现跑不了了,也没啥特别的错误提示,就看到一句有用的错误Caused by: android.os.NetworkOnMainThreadExc ...

  2. Java 面向对象概念

    Interface 接口 An interface defines a protocol of communication between two objects. An interface decl ...

  3. Linux下静态库生成和使用

    Linux下静态库生成和使用 一.静态库概念 1.库是预编译的目标文件(object  files)的集合,它们可以被链接进程序.静态库以后缀为”.a”的特殊的存档(archive file)存储. ...

  4. eCryptfs文件系统测试

    650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic.php?refimg=" ...

  5. linux 从命令行自动识别文件并将其打开的命令

    若是shell是 zsh,则可: 使用 alias -s 定义后缀别名 (zsh) % alias -s pl=perl % script.pl perl script.pl % alias -s p ...

  6. UNITY3D MAC版本破解

    百度网盘下载地址: http://pan.baidu.com/s/1eQmvLqa#path=%252F 包含本体和破解文件 首先说明一下,如果是公司做开发建议去购买正版. 之前网上也有很多人贴出了破 ...

  7. HD2025查找最大元素

    查找最大元素 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  8. C++11多元组类别

    [C++11多元组类别] 多元组可被视为是 struct 其数据成员的一般化.底下是一个多元组类别的定义和使用情况: 我们可以定义一个多元组类别对象 proof 而不指定其内容,前提是 proof 里 ...

  9. 应用TcpListener实现的socket服务器端

    前言 项目中要实现一个简单的socket服务器端,采用了TcpListener这个类.除了基本的功能之外,有几处需要注意的点. 要能同时接收多个客户端的连接,当然,不需要几千个那么多. 要能探测到客户 ...

  10. 8.1搜索专练DFS和BFS

    这是第一次全部做出来的依次练习了,有一些都是做过两遍了的,但是还是错了几回,更多时候我还是应该多注意下细节,就好像章爷笑我 的一样,像什么vis[]标记没清0,什么格式错误,还有什么题目没看清,还是的 ...