题意:

给你两个数: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. Windows 11抢先体验

    SHA1值: 3B6DA9194BA303AC7DBBF2E521716C809500919C 谷歌云:https://drive.google.com/file/d/1sH0cBI9hwh8EdlV ...

  2. PHP中的文件系统函数(三)

    总算来到我们最关心的部分了,也就是 f 相关函数的操作.基本上大部分的文件操作都是以今天学习的这些内容为基础的,话不多说,我们就一个一个的来学习学习吧. 文件读取 文件的读取其实非常简单,fopen( ...

  3. pyqt5 GUI教程

    from PyQt5 import QtCore, QtGui, QtWidgets import sys import qtawesome class MainUi(QtWidgets.QMainW ...

  4. [转载]Nginx负载均衡配置实例详解

    负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦. 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可 ...

  5. 使用python3中的2to3.py执行数据迁移

    1.在python默认安装的位置找到Tools\scripts 2.找到2to3.py 3.在所在文件夹shift+右键打开终端 4.执行命令python 2to3.py -w 需要做数据迁移的数据路 ...

  6. 鸿蒙内核源码分析(VFS篇) | 文件系统和谐共处的基础 | 百篇博客分析OpenHarmony源码 | v68.01

    子曰:"质胜文则野,文胜质则史.文质彬彬,然后君子." <论语>:雍也篇 百篇博客系列篇.本篇为: v68.xx 鸿蒙内核源码分析(VFS篇) | 文件系统和谐共处的基 ...

  7. P3706-[SDOI2017]硬币游戏【高斯消元,字符串hash】

    正题 题目链接:https://www.luogu.com.cn/problem/P3706 题目大意 给出 \(n\) 个长度为 \(m\) 的 \(H/T\) 串. 开始一个空序列,每次随机在后面 ...

  8. CF11D-A Simple Task【状压dp】

    正题 题目链接:https://www.luogu.com.cn/problem/CF11D 题目大意 给出\(n\)个点\(m\)条边的一张简单无向图,求它的简单环的个数. \(1\leq n\le ...

  9. 几分钟就能学会的Python虚拟环境教程

    什么是虚拟环境 我们在使用Python的时候,通常用pip来进行包管理.比如我们要安装一个叫requests的库,那么我们就会采用以下命令去安装: pip install requests 那你知道, ...

  10. 如何从阿里云Code升级至云效Codeup

    如果你还在使用阿里云Code,不防看看如何从阿里云Code升级至云效Codeup,云效代码管理Codeup是阿里云出品的一款企业级代码管理平台,提供代码托管.代码评审.代码扫描.质量检测等功能,全方位 ...