题目链接: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. Mapreduce执行过程分析(基于Hadoop2.4)——(二)

    4.3 Map类 创建Map类和map函数,map函数是org.apache.hadoop.mapreduce.Mapper类中的定义的,当处理每一个键值对的时候,都要调用一次map方法,用户需要覆写 ...

  2. 关于dom节点绑定滑动事件导致浏览器上下滑动失效解决方案--黄丕巧

    1.移动端开发往往需要添加一下自定义的左右滑动事件,但是添加了左右滑动事件之后就要阻止浏览器大默认事件,否则dom节点的滑动事件和浏览器本身的滑动会出现冲突,导致滑动的时候会出现消失瞬间再出现的效果 ...

  3. asp web api 怎么使用put和delete。

    Method Overriding RESTful services allow the clients to act on the resources through methods such as ...

  4. 第二百零四天 how can i 坚持

    我应该不会看错吧.最近媒体热炒小米衰落了,有必要那么大张旗鼓的报道吗?小米.华为,坚决看好小米.感觉华为品牌有些杂乱,在走三星的老路,小米有些苹果的影子,但是,多了个互联网.互联网... 未来孰优孰劣 ...

  5. 第二百三十天 how can I 坚持

    上周日去蟒山摘的松子吗?应该是松子吧,裂开了呢.为啥呢.原来博客园可以上传图片,只是上传起来好费劲啊. 今天程哥问给我分的活多不多,我竟然说了句好多,哎.其实很多问题可以用还好来回答,还好,还行,哈哈 ...

  6. 动态生成xml文件

    使用xmlParser动态生成xml,输入的字符编码是gbk,结果怎么生成都不行,后来把输入转成utf8之后, 再生成就ok了

  7. Umbraco部署到IIS中权限问题(back office没有权限新建template)

    在开发项目中,发现把基于Umbraco平台开发的网站部署到服务器的IIS之后,访问该网站的back office 在back office中增加一个template时,发送错误,提示 Access t ...

  8. JDBC 是什么

    JDBC is a Java database connectivity technology (Java Standard Edition platform) from Oracle Corpora ...

  9. spring与jpa整合 简化persistence.xml配置文件 使用属性文件 数据源dbcp访问数据库

    ===========appliction.xml配置文件======================= <?xml version="1.0" encoding=" ...

  10. XML与DataSet相互转换,DataSet查询

    以FileShare.Read形式读XML文件: string hotspotXmlStr = string.Empty; try { Stream fileStream = new FileStre ...