http://acm.hdu.edu.cn/showproblem.php?pid=5019

给出X 和Y,求出第K 大的 X 和Y 的公约数。

暴力求出所有公约数

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include<set>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
LL gcd(LL a,LL b){ return b == 0? a:gcd(b,a%b);}
LL x,y,k;
LL p[1000005];
LL cmp(LL a,LL b)
{
return a > b;
}
int main() {
int _;
RD(_);
while(_--){
int cnt = 0;
scanf("%I64d%I64d%I64d",&x,&y,&k);
LL op = gcd(x,y);
for(LL i = 1;i < (int)sqrt(op)+1;++i){
if(op%i == 0){
p[cnt++] = i;
if(i*i != op){
p[cnt++] = op/i;
}
}
}//cout<<cnt;
sort(p,p+cnt,cmp);
if(cnt < k){
puts("-1");
}
else{
printf("%I64d\n",p[k-1]);
}
} return 0;
}

hdu 5019的更多相关文章

  1. BestCoder10 1002 Revenge of GCD(hdu 5019) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019 题目意思:给出 X 和 Y,求出 第 K 个 X 和 Y 的最大公约数. 例如8 16,它们的公 ...

  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. hdu 5019(第K大公约数)

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

  5. 数学--数论--HDU 5019 revenge of GCD

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

  6. xudyh的gcd模板

    hdu 5019 #include <cstdlib> #include <cctype> #include <cstring> #include <cstd ...

  7. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  9. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

随机推荐

  1. spring 3.x + hibernate4.x 实现数据延迟加载

      Spring为我们解决Hibernate的Session的关闭与开启问题. Hibernate 允许对关联对象.属性进行延迟加载,但是必须保证延迟加载的操作限于同一个 Hibernate Sess ...

  2. C# 窗口页面卡的处理方案-异步编程委托

    今天用winform做了一个小程序,主要是用于远程数据的登录采集,因为数据量非常大,到时每次点击按钮执行程序的时候界面都会出现假死状态,具体表现是无法拖动窗口,无法最小化或关闭等,只能任务管理进程结束 ...

  3. 有名管道FIFO

    管道和FIFO的特征之一是它们的数据是一个字节流.这是UNIX的原生I/O模型.进程往其中写入的是字节流,系统不对它作解释. FIFO不存数据,只是通过它找到内核文件. 一.建立有名管道 1.命令mk ...

  4. Windows驱动手动卸载与安装

    彻底卸载的流程 1.删除C:\windows\inf\oem.inf路径下的所有oem文件 2.删除c:\windows\system32\drivers路径下对应的sys文件 3.(重要) 第一步: ...

  5. 使用HTTP头去绕过WAF(bypasswaf)

    在用node http get 请求的时候,发现的 解决方案: Add headers to all Burp requests to bypass some WAF products. This e ...

  6. poj3666(DP+离散化)

    题目链接:http://poj.org/problem?id=3666 思路: 看了讨论区说本题的数据比较弱,只需要考虑不减序列即可,比较懒,所以我也只写了这一部分的代码,思路都一样,能AC就行了. ...

  7. cf-Round542-Div2-C(暴力+DFS)

    题目链接:http://codeforces.com/contest/1130/problem/C 思路: 利用DFS搜索(r1,c1)和(r2,c2)可到达的点的集合,分别存在a1,a2中,若a1= ...

  8. Django之ORM使用以及模板语言

    一.ORM版增删改查 1.ORM的语句 1.类名.objects.all()          --> 返回一个列表 2.类名.objects.filter()       --> 返回一 ...

  9. 10-能不能组成m

    /*                                找数达人 时间限制:1000 ms  |  内存限制:65535 KB      难度:2 描述 小明最近做出了一道题:如何在一组数 ...

  10. SQL Cursor 基本用法[用两次FETCH NEXT FROM INTO语句?]

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...