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. [LeetCode 题解]: Partition List

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  2. Java Socket实现基于TCP和UDP多线程通信

    一.通过Socket实现TCP编程 1.1 TCP编程 TCP协议是面向连接,可靠的,有序的,以字节流的方式发送数据.基于TCP协议实现网络通信的类有客户端的Socket类和服务器端的ServerSo ...

  3. Tomcat不自动解压问题

    问题: 版本迭代上线,需要更换新的war包, 1.先将老的war和文件夹删除掉,再放入新的war到webapps中, 2.发现启动Tomcat后没有解压该war包, 3.需要先将server.xml中 ...

  4. Proxy account failing to run SSIS Error (Proxy (11) is not allowed for subsystem "SSIS" and user "AB\testuser ".

    USE [msdb]EXEC msdb.dbo.sp_grant_login_to_proxy @proxy_name=N'SSISProxyAgentV1', @login_name=N'WTC\E ...

  5. golang plugin的依赖问题

    golang plugin的依赖问题 此文中涉及的plugin运行环境为mac 10.14,go版本为1.11 主要是想讨论一下插件依赖的第三方库的问题. 例子是在https://github.com ...

  6. MongoDB账号管理及实践

    此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 目前蜂巢(云计算基础服务)MongoDB上已经有数十个实例,其中不少是企业用户或公司内部产品用户的.用户多了 ...

  7. sqlite3简单操作

    最近在操作公司视频设备的tutk转发服务器的时候,用到的数据库是sqlite,在此复习一下 目录 1 建立数据库档案 2 在sqlite3提示列下操作 3 SQL的指令格式 4 建立资料表 5 建立索 ...

  8. 【Oracle 12c】CUUG OCP认证071考试原题解析(30)

    30.choose the best answer Examine the commands used to create DEPARTMENT_DETAILS and COURSE_DETAILS: ...

  9. JavaScript一个页面中有多个audio标签,其中一个播放结束后自动播放下一个,audio连续播放

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Hibernate 干货2

    @ORM框架 对象关系映射,用于实现面向对象编程语言里不同系统的数据之间的转换 @实例public void demo01(){  User user = new User();  user.setU ...