题目链接: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. .hpp文件

    hpp在C++中的含义 以前在开源代码里面遇到过,今天看boost源码的时候又遇到了,故学习一下. hPP,计算机术语,用C/C++语言编写的头文件,通常用来定义数据类型,声明变量.函数.结构和类.而 ...

  2. Yii 1.11 获取当前的模块名 控制器名 方法名

    $this->module->id; #模块名$this->action->id; #方法名$this->uniqueId; #控制器名称 Yii: 获取当前模块名.控制 ...

  3. ArcGIS Desktop打开慢的解决办法

    问题:ArcGIS Desktop打开巨慢,不管是ArcMap还是CataLog都是一样的,打开一次有时候需要10多分钟. 解决方法:C:\Users\[用户名]\AppData\Roaming\ES ...

  4. oracle 的行级触发器

    create or replace trigger tri_insertAcceptList after insert on STDOC.DEVELOPER for each row DECLARE ...

  5. oracle学习 九 游标的使用(持续更)

    为什么要使用? 笔者查阅了一些资料之后得到的结论是, 关系型数据库是面向集合的,而游标是面向行的,游标可对取出来的集合(结果集)中每一行进行相同或不同的操作,还提供对基于游标位置而对表中数据进行删除或 ...

  6. Java 理论与实践: 流行的原子——新原子类是 java.util.concurrent 的隐藏精华(转载)

    简介: 在 JDK 5.0 之前,如果不使用本机代码,就不能用 Java 语言编写无等待.无锁定的算法.在 java.util.concurrent 中添加原子变量类之后,这种情况发生了变化.请跟随并 ...

  7. Compactness问题

    今天偶然在论坛讨论在halcon下紧密度的问题,现在总结一下 Calculation: If L is the length of the contour (see contlength) and F ...

  8. 项目视频讲解_[HeyJava][尚学堂][CMS文章内容管理系统]

    [HeyJava][尚学堂][CMS文章内容管理系统] http://pan.baidu.com/s/1c0imHrE

  9. android ListView进阶

    ListView 1.在android 开发中很多时候都要用到ListView的这个控件的,但用这个控件的时候会遇到一些问题,如在ListView中有Button按钮,就需要将按钮的监听事件给分离出来 ...

  10. 负载均衡SESSION同步总结

    1.redis/分布式文件存储系统/数据库 存储session,解决负载均衡集群中session不一致问题 http://www.cnblogs.com/painsOnline/p/5194851.h ...