题意:

给你两个数:X和Y  。输出它们的第K大公约数。若不存在输出 -1

数据范围:

1 <= X, Y, K <= 1 000 000 000 000

思路:

它俩的公约数一定是gcd(X,Y)的因数。(把它俩分解成质因数相乘的形式就可以看出)

故找出gcd(x,y)所有的因数,从大到小排序,输出第K个即可。

代码:

#include <cstdio>
#include <iostream>
#include <string.h>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#include <stack>
using namespace std;
int const uu[4] = {1,-1,0,0};
int const vv[4] = {0,0,1,-1};
typedef long long ll;
int const maxn = 50005;
int const inf = 0x3f3f3f3f;
ll const INF = 0x7fffffffffffffffll;
double eps = 1e-10;
double pi = acos(-1.0);
#define rep(i,s,n) for(int i=(s);i<=(n);++i)
#define rep2(i,s,n) for(int i=(s);i>=(n);--i)
#define mem(v,n) memset(v,(n),sizeof(v))
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1 ll gcd(ll a,ll b){
return b==0?a:gcd(b,a%b);
} ll factor[1000005];
ll x,y,k;
int T; bool cmp(ll a,ll b){
return a>b;
}
int main(){
cin >> T;
while(T--){
scanf("%I64d%I64d%I64d",&x,&y,&k);
ll d = gcd(x,y);
ll m = sqrt(d+0.5);
int num = 0;
rep(i,1,m) if(d%i==0){
factor[++num] = i;
if(i!=d/i) factor[++num] = d/i;
}
sort(factor+1,factor+1+num,cmp);
if(k>num) printf("-1\n");
else printf("%I64d\n",factor[k]);
}
}

  

hdu 5018 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. hdu 5018 Revenge of Fibonacci

    大水题 #include<time.h> #include <cstdio> #include <iostream> #include<algorithm&g ...

  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. HDOJ 5019 Revenge of GCD

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

  8. hdu 4983 Goffi and GCD(数论)

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

  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. error: subscripted value is neither array nor pointer问题解决

    在运行程序的时候报错:error: subscripted value is neither array nor pointer 原因分析:下标值不符合数组或指针要求,即操作的对象不允许有下标值. 出 ...

  2. 5cms使用sql语句给网站添加内容

    <!--list:{$Sql=UPDATE [{pre}Content] SET Indexpic="/uploadfile/201405/25/lsgjyst.jpg",t ...

  3. ecshop 加入购物车和直接购买同时存在的方法

    一.首先将直接购买的链接设置为 <a href="javascript:bool =1;addToCart({$goods.goods_id})"> bool值为1,g ...

  4. DEDE留言板调用导航的方法

    DEDE留言板调用导航的方法 dede里的留言板guestbook.htm用{dede:include filename="../default/head.htm"/}不能自动生成 ...

  5. Nginx系列(2)- 正向代理和反向代理

    Nginx作用 Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反向代理 正向代理是代理客户端,反向代理是代理服务端 正向代理要知道访问服务器的地址,反向代理不需要知道访问服务器的真实 ...

  6. Docker系列(20)- 数据卷容器

    数据卷容器 什么是数据卷容器? 容器和容器之间实现数据共享 一个容器先于宿主机创建挂载方式,宿主机就会有改卷的目录 第二个容器使用命令--volumes-from 第一个容器,共享使用了第一个容器与宿 ...

  7. Docker 配置国内镜像加速器

    Docker 默认是从官方镜像地址 Docker Hub 下下载镜像,由于服务器在国外的缘故,导致经常下载速度非常慢.为了提升镜像的下载速度,我们可以手动配置国内镜像加速器,让下载速度飚起来. 国内的 ...

  8. self this

    面向对象编程(OOP,Object OrientedProgramming)现已经成为编程人员的一项基本技能.利用OOP的思想进行PHP的高级编程,对于提高PHP编程能力和规划web开发构架都是很有意 ...

  9. SpringBoot 如何进行限流?老鸟们都这么玩的!

    大家好,我是飘渺.SpringBoot老鸟系列的文章已经写了四篇,每篇的阅读反响都还不错,那今天继续给大家带来老鸟系列的第五篇,来聊聊在SpringBoot项目中如何对接口进行限流,有哪些常见的限流算 ...

  10. CF850E Random Elections 题解

    题目传送门 题目大意 没法描述,过于繁杂. 思路 果然自己是个菜鸡,只能靠读题解读题,难受极了,其实不是很难自己应该做得出来的....哎.... 不难发现可以统计 \(A\) 获胜的情况乘上 \(3\ ...