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. Markdown编辑器——常用语法

    Markdown是什么? 简短来说,他就是一款特别适用于写博客的编辑器.为什么适合呢,因为它特别的方便.以博客园的编辑界面来说,它原本的界面是这样的(有没有一种Word2003的既视感): 但是,当你 ...

  2. LightOJ 1213 Fantasy of a Summation(规律 + 快数幂)

    http://lightoj.com/volume_showproblem.php?problem=1213  Fantasy of a Summation Time Limit:2000MS     ...

  3. MVC框架入门准备(三)事件类 - 事件的监听和触发

    在mvc框架中可以看到事件类,实现事件的监听和触发. 举例: <?php /** * 事件类 */ class Event { // 事件绑定记录 private static $events; ...

  4. 根据IP获取IP定位

    http://ip.taobao.com/service/getIpInfo.php?ip=27.17.60.152 {,"}} 不确定是否有次数限制

  5. by python3-XSStrike 测试XSS

    一.概述: XSStrike是一个Cross Site Scripting检测套件,配备四个手写解析器,一个智能有效载荷生成器,一个强大的模糊引擎和一个非常快速的爬虫. XSStrike不是像其他工具 ...

  6. 在myeclipse中有的项目上有个红色感叹号

    之前做项目的时候遇到过这个问题,最后确定原因是项目引用了很多放在D盘或E盘上的jar包,但是我们不小心把这些jar包删除或移动路径了,因此myeclipse识别不了出现红色的感叹号,解决方式是在mye ...

  7. mac编辑器vim美化

    mac编辑器vim美化 contents 环境 效果呈现 安装 quick start 环境 mac10.13.6,vim7(该版本mac自带的vim是7),git mac下vim的配置文件有两处 一 ...

  8. day03 --class --homework

    '''# >>>>>>2 :,有字符串s = "123a4b5c"#>>>>>^ 1: # 1)通过对s切片形成新 ...

  9. 钉钉机器人集成Jenkins推送消息模板自定义发送报告

    一.由于公司同样也使用了钉钉.那么在做Jenkins集成自动化部署的时候,也是可以集成钉钉的. 那种Jenkins下载钉钉插件集成,简单设置就可以完成了.我们今天要做的是,定制化的发送消息. 钉钉推送 ...

  10. 【JavaScript】__proto__和prototype的区别和联系【整理】

    var person={name:'ninja'}; person.prototype.sayName=function(){ return this.name; } Chrome运行结果: 提示找不 ...