题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019

Problem Description
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
 
Source

官方题解:

代码例如以下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef __int64 LL;
vector<LL>v;
LL GCD(LL a, LL b)
{
if(b == 0)
return a;
return GCD(b,a%b);
} int main()
{
int t;
LL x, y, k;
scanf("%d",&t);
while(t--)
{
v.clear();
scanf("%I64d%I64d%I64d",&x,&y,&k);
LL tt = GCD(x,y);
// printf("%I64d\n",tt);
for(LL i = 1; i*i <= tt; i++)
{
if(tt%i == 0)
{
v.push_back(i);
if(i*i != tt)//防止放入两个i
v.push_back(tt/i);
}
}
sort(v.begin(), v.end());
if(k > v.size())
printf("-1\n");
else
printf("%I64d\n",v[v.size()-k]);
}
return 0;
}

HDU 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

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

  3. HDOJ 5019 Revenge of GCD

    Revenge of GCD In mathematics, the greatest common divisor (gcd), also known as the greatest common ...

  4. hdu 5018 Revenge of GCD

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

  5. hdu 5869 区间不同GCD个数(树状数组)

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  6. hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)

    CA Loves GCD  Accepts: 64  Submissions: 535  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: 2 ...

  7. hdu 4983 Goffi and GCD(数论)

    题目链接:hdu 4983 Goffi and GCD 题目大意:求有多少对元组满足题目中的公式. 解题思路: n = 1或者k=2时:答案为1 k > 2时:答案为0(n≠1) k = 1时: ...

  8. hdu 5019(第K大公约数)

    Revenge of GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. HDU 4983 Goffi and GCD(数论)

    HDU 4983 Goffi and GCD 思路:数论题.假设k为2和n为1.那么仅仅可能1种.其它的k > 2就是0种,那么事实上仅仅要考虑k = 1的情况了.k = 1的时候,枚举n的因子 ...

随机推荐

  1. leetcode:Reverse Integer(一个整数反序输出)

    Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  2. c 按范围快速指定整数

    以前用过octave, 和matlab类似的软件, 指定范围非常方便 i = 1:10:100;  就可以得到 10 20 30 ... 100 这一系列的数据, 但是在c里面, 必须手动写循环, 太 ...

  3. Hbase学习记录(1)|伪分布式安装

    概述 Hbase –Haddop Database 是一个高性能,高可靠性.面向列.可伸缩的分布式存储系统. Hbase利用HDFS作为文件存储系统,利用MapReduce来处理Hbase的海量数据, ...

  4. 【kd-tree】专题总结

    感谢orz神·小黑的指导 kd-tree就是用来计算若干维空间k近/远点的数(shou)据(suo)结(you)构(hua) 建树 假设题目是k维的点 第deep层就是用deep%k+1维把所有点分为 ...

  5. [cocos2d-js]长按按钮事件

    定义两个全局变量 var bLeftButtonClick = false; var bRightButtonClick = false; var MainLayer = cc.Layer.exten ...

  6. Nagle算法,tcp小包组合(延迟)发送的算法

    在j2ee中可能会引起业务的延迟,java自行决定是否需要使用 Socket.TCP_NODELAY 选项来禁用 nagle 策略算法.c语言的语法是: setsockopt( sock, IPPRO ...

  7. 第三百五十六天 how can I 坚持

    一年了,三百五十六天.写个算法算下对不对. 今天突然想买辆自行车了.云马智行车,还是捷安特,好想买一辆. 网好卡.貌似少记了一天呢,357了.好快. 睡觉了,还没锻炼呢,太晚了. 1458748800 ...

  8. Cocos2d-x项目移植到WinRT/Win8小记

    Cocos2d-x项目移植到WinRT/Win8小记 作者: K.C. 日期: 11/17/2013 Date: 2013-11-17 23:33 Title: Cocos2d-x项目移植到WinRT ...

  9. C++11空指针

    [C++11空指针] 早在 1972 年,C语言诞生的初期,常数 0 带有常数及空指针的双重身分. C 使用 preprocessor macro NULL 表示空指针, 让 NULL 及 0 分别代 ...

  10. TcxDBVerticalGrid优秀的编辑控件