Revenge of GCD

  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

3
2 3 1
2 3 2
8 16 3

Sample Output

1
-1
2

解题思路:
  本题可恶的最大公约数要向你复仇,给你测试数量t与3个整数x, y, k要求你求出x与y的第k大的公约数,如果不存在就输出-1。

  x与y的第1大的公约数就是最大公约数,记为gcdxy,x与y小于gcdxy的其他公约数一定是gcdxy的约数。本题就是求两个数的最大公约数的约数的问题。

  我们可以用一个容器记录x与y的所有约数,由小到大排序后如果k > 容器元素数量则不存在,若存在,则下标为容量 - k的元素即为所求。

  注意在求解时直接遍历小于gcdxy的所有数字会超时,但由于我们找到 i 为gcdxy的约数时也可以确定 gcdxy / i 也是gcdxy的约数,这样我们只需找2-sqrt(gcdxy)即可找全所有约数。

样例解析:

  2 3 1  2 与 3 的最大公约数是1,1的约数只有自身,所以2 与 3 只有一个公约数1,第1大的公约数为 1;

  2 3 2   同上2 与 3 只有一个公约数1,第2大的公约数不存在;

  8 16 3  8 与 16 的最大公约数是8,8有约数 8 4 2 1,8 与 16的所有公约数有 8 4 2 1,第3大的公约数为2。

 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 0x7fffffff;
vector<LL> h; //h记录x与y所有公约数
LL gcd(LL a, LL b){ //求x与y的最大公约数
if(b == )
return a;
else
return gcd(b , a % b);
}
int main()
{
int t;
scanf("%d", &t); //输入测试数量
while(t--){
LL x, y, k;
scanf("%lld%lld%lld", &x, &y, &k); //输入x y与k
LL gcdxy = gcd(x, y); //求出x与y的最大公约数
h.clear(); //清空容器
if(gcdxy != ) //判断最大公约数是否为1以免重复加入容器
h.push_back(gcdxy);
h.push_back(); //1肯定是x与y的公约数
int sqrtGcd = sqrt(gcdxy);
for(int i = ; i <= sqrtGcd; i++){
if(gcdxy % i == ){ //若i为gcdxy的约数
h.push_back(i); //i加入容器
h.push_back(gcdxy / i); //顺便计算并记录另一个约数
}
}
sort(h.begin(), h.end()); //由小到大排序
//我做过从大到小的排序但是wa,诸位强力人要是了解为什么请指导我
if(k > h.size()){ //判断是否存在第k大的公约数
printf("-1\n");
}else{
printf("%lld\n" , h[h.size() - k]);
}
}
return ;
}

  

HDOJ 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(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019 Problem Description In mathematics, the greatest ...

  3. HDU 5019 Revenge of GCD

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

  4. hdoj 5087 Revenge of LIS II 【第二长单调递增子】

    称号:hdoj 5087 Revenge of LIS II 题意:非常easy,给你一个序列,让你求第二长单调递增子序列. 分析:事实上非常easy.不知道比赛的时候为什么那么多了判掉了. 我们用O ...

  5. BestCoder10 1002 Revenge of GCD(hdu 5019) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019 题目意思:给出 X 和 Y,求出 第 K 个 X 和 Y 的最大公约数. 例如8 16,它们的公 ...

  6. Revenge of GCD HDU5019

    Description In mathematics, the greatest common divisor (gcd), also known as the greatest common fac ...

  7. HDOJ 5088 Revenge of Nim II 位运算

    位运算.. .. Revenge of Nim II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  8. HDOJ 5087 Revenge of LIS II DP

    DP的时候记录下能否够从两个位置转移过来. ... Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

  9. hdu 5018 Revenge of GCD

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

随机推荐

  1. 转发一篇能看懂的关于ingress的说明

    最近发现好多人问 Ingress,同时一直也没去用 Nginx 的 Ingress,索性鼓捣了一把,发现跟原来确实有了点变化,在这里写篇文章记录一下 https://mritd.me/2017/03/ ...

  2. 将引用了第三方jar包的Java项目打包成jar文件的两种方法

    方案一:用Eclipse自带的Export功能 步骤1:准备主清单文件 “MANIFEST.MF”, 由于是打包引用了第三方jar包的Java项目,故需要自定义配置文件MANIFEST.MF,在该项目 ...

  3. 知识记录——CSS规范(文章内容为转载)

    原作者信息 作者:词晖 链接:http://www.zhihu.com/question/19586885/answer/48933504 来源:知乎 著作权归原作者所有,转载请联系原作者获得授权. ...

  4. form 认证 读取

    class Program { public static CookieContainer cc { get; set; } static void Main(string[] args) { str ...

  5. iOS 模拟器网络设置

    在 iOS Simulator 下,点上面菜单栏最左面的苹果标志,第三项 网络设置,然后先最后面一项,网络设置. 然后在网络设置界面下可以开启或关闭网络.

  6. 为什么 React Elements 会有 $$typeof 这个属性?

    简评:debug 的时候看到 element 对象中有 $$typeof 这属性,于是查了一下这到底干嘛的. 我们知道,通过 JSX 创建一个 React Elements 时: <marque ...

  7. 结合业务,精炼SQL

    现代网站,性能的瓶颈都围绕着数据库的性能来谈.数据库是存储的核心部件,在日益增长的流量中会凸显数据库的性能瓶颈.从<淘宝技术十年>书中来看,淘宝发展历程中从MYSQL换成了ORACLE又换 ...

  8. time 模块学习

    前情提要: time模块是经常使用的模块.主要是用来记录时间,以及时间上的相关操作 一:时间戳 1:第一种形式 import time print(time.time()) 从1970 1 1 0:0 ...

  9. Windows Server 2012 R2 部署DC及主辅DC

    背景信息: 资源组:hlmdcn DC1:windows Server 2012 R2 Datacenter, A2, hlmdc1, 10.8.0.4DC2:windows Server 2012 ...

  10. git 命令摘录

    回滚 n 个 commit (增加了revert commit) git revert -n commit_id 回滚到指定的commit_id(不增加commit,回滚的commit_id被删除) ...