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. [Elixir001]使用tty做一个简单的日记记录

    大半年前实践的使用Evernote做知识管理 https://www.zhihu.com/question/20232993/answer/34270710里面的记录日记模块大概长成这样子 用了半年, ...

  2. Jenkins启动Tomcat时提示Neither the JAVA_HOME nor the JRE_HOME environment variable is defined

      Jenkins构建提示: [SSH] executing...Neither the JAVA_HOME nor the JRE_HOME environment variable is defi ...

  3. 类文件结构与javap的使用

    此文已由作者赵计刚薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1.javap的使用与类文件结构 使用过程: java源代码:  1 package compile;   ...

  4. 【题解】 AGC029-A Irreversible operation

    传送门 定位:思维好题. 考虑无论如何每一个W都会和前面的B在一起交换一次,所以直接求和就好了. 注意long long的使用. #include<stdio.h> #include< ...

  5. Django_Restframwork_序列号组件

     第一种序列化方式. 第二种方法通过Model_to_dict方法进行创建 第三种方式序列号组件Serializers: 第四种方法序列化 五.ModelSerializer组件. POST校验 PU ...

  6. 超详细!CentOS 7 + Hadoop3.0.0 搭建伪分布式集群

    超详细!CentOS 7 + Hadoop3.0.0 搭建伪分布式集群 ps:本文的步骤已自实现过一遍,在正文部分避开了旧版教程在新版使用导致出错的内容,因此版本一致的情况下照搬执行基本不会有大错误. ...

  7. 【awk】按小时切割日志

    需求: 把日志按日志内容中的小时数做切割 {hostname=ali-beijing-msync-3512} 2017-05-17 23:17:52.694 [info] <0.27292.70 ...

  8. python爬取小说详解(一)

    整理思路: 首先观察我们要爬取的页面信息.如下:  自此我们获得信息有如下: ♦1.小说名称链接小说内容的一个url,url的形式是:http://www.365haoshu.com/Book/Cha ...

  9. Microsoft Visual C++ 14.0 is required

    building 'twisted.test.raiser' extensionerror: Microsoft Visual C++ 14.0 is required. Get it with &q ...

  10. TeamView 连接2、3事

    问题1: 客户通过本地远程上服务器开TeamView让我们连,我们连上后发现开户一把她的远程关掉就卡住了. 如图,原来TeamView会启动多用户增强支持. 原来用的是用户ID连的,用户断掉远程后就不 ...