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. Mahout的taste里的几种相似度计算方法

    欧几里德相似度(Euclidean Distance) 最初用于计算欧几里德空间中两个点的距离,以两个用户x和y为例子,看成是n维空间的两个向量x和y,  xi表示用户x对itemi的喜好值,yi表示 ...

  2. Solr中的一些查询参数

    fl: 是逗号分隔的列表,用来指定文档结果中应返回的 Field 集.默认为 “*”,指所有的字段. defType: 指定query parser,常用defType=lucene, defType ...

  3. shell中调用jenkins API批量运行历史任务

    shell中调用jenkins API批量运行jenkins带参数的任务: #!/bin/sh #startdate=20150127 startdate=20150201 while [ " ...

  4. JAVA 字符串编码转换

    /** * 字符串编码转换的实现方法 * @param str 待转换编码的字符串 * @param newCharset 目标编码 * @return * @throws UnsupportedEn ...

  5. C# 多线程task

    1.异步和多线程的区别?没什么太大区别.异步是目的,使用多线程实现.想想AJAX异步加载,不就是不想让浏览器界面卡住嘛,所以在程序中对于某些单独的操作,比如写日志,我们不想等它完成后再执行其它操作(因 ...

  6. c#设计模式之观察者模式(Observer Pattern)

    场景出发 一个月高风黑的晚上,突然传来了尖锐的猫叫,宁静被彻底打破,狗开始吠了,大人醒了,婴儿哭了,小偷跑了 这个过程,如果用面向对象语言来描述,简单莫过于下: public class Cat { ...

  7. ASP.NET中Onclick和OnserverClick事件的区别 (转)

    对于服务器按钮控件(即<asp:Button>类型的按钮): 服务器响应事件:OnClick 客户端响应属性:OnClientClick 对于html按钮控件(即<input typ ...

  8. kubectl get componentstatus ERROR:HTTP probe failed with statuscode: 503

    通过kubectl命令可以查看k8s各组件的状态: [root@wecloud-test-k8s-1 ~]# kubectl get cs NAME STATUS MESSAGE ERROR cont ...

  9. 决定以后再做公司的项目的时候,能够用其他语言的绝对不用delphi

    1.delphi7的IDE真的很不友好 2.delphi7的控件有的有问题 3.delphi7居然不支持结构体的泛型存储 4.网上的解决文档超少,一些小bug,就要折腾半天 5.pascal语法太过结 ...

  10. 【文文殿下】[51nod1469] 淋漓尽致子串

    SAM的经典应用 一个状态的SIze==1绝对不合法. 一个状态在parent树上有一个Size>1的后继绝对不合法(前面可以再补字符) 一个状态可以转移到Size>1的节点绝对不合法,因 ...