HDU5019Revenge of GCD
发布日期:2021-06-29 05:37:46 浏览次数:3 分类:技术文章

本文共 1745 字,大约阅读时间需要 5 分钟。

Revenge of GCD

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2588    Accepted Submission(s): 736


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
32 3 12 3 28 16 3
 

Sample Output
1-12

题意:给两个数x和y,求这两个数的第k大公约数,如果不存在就输出-1.

解析:根据给的数据大小分析一下,开个平方的话应该可以不超时。所以先求出x和y的最大公约数z,然后求这个数z的约数,存在数组中。需要第几个输出即可。

代码:

#include 
#include
#include
#include
using namespace std;typedef long long ll;#define N 1000005ll a[N], b[N]; ll gcd(ll x, ll y){ return y == 0? x : gcd(y, x%y);}int main(){ int t; ll x, y, k; scanf("%d", &t); while(t--){ scanf("%lld%lld%lld", &x, &y, &k); if(k > x || k > y){ puts("-1"); continue; } ll z = gcd(x, y); ll s = sqrt(z); int cnt = 0, tol; for(ll i = 1; i <= s; i++){ if(z % i == 0){ a[cnt] = i; b[cnt] = z / i; cnt ++; } } tol = cnt * 2; if(s*s == z){ tol --; b[cnt++] = s; } if(k > tol) puts("-1"); else{ if(k <= cnt) printf("%lld\n", b[k-1]); else printf("%lld\n", a[tol-k]); } } return 0;}

转载地址:https://blog.csdn.net/zhj_fly/article/details/76094711 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:中国剩余定理
下一篇:除法取模和逆元

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月15日 01时01分34秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

知乎:硬件和软件哪个吃香? 2019-04-29
中国深圳,600架无人机的盛典! 2019-04-29
干货分享 JVM 之第 3 篇 —— Java 内存结构相关 2019-04-29
干货分享 JVM 之第 4 篇 —— 掌握 Jmeter 压力测试工具,熟悉 Jconsole.exe 工具 2019-04-29
干货分享 JVM 之第 5 篇 —— 类加载器 2019-04-29
干货分享 JVM 之第 6 篇 —— SpringBoot2.0 框架性能调优 2019-04-29
基于 Hystrix 高并发服务限流第 1 篇 —— 必须了解的相关概念 2019-04-29
基于 Hystrix 高并发服务限流第 2 篇 —— 服务隔离(线程池隔离、信号量隔离) 2019-04-29
基于 Hystrix 高并发服务限流第 3 篇 —— 服务熔断、服务降级 2019-04-29
基于 Hystrix 高并发服务限流第 4 篇 —— 基于 Feign 实现服务熔断降级处理 2019-04-29
基于 Hystrix 高并发服务限流第 5 篇 —— Hystrix 监控 2019-04-29
Eureka 如何快速的、优雅的停止某个微服务 2019-04-29
Eureka 实现安全认证 2019-04-29
基于 Hystrix 高并发服务限流第 6 篇 —— 服务限流,基于 RateLimiter 实现 2019-04-29
Nginx 反向代理、负载均衡配置、Location正则表达式 2019-04-29
SpringBoot + WebSocket 实现前后端的收发消息 2019-04-29
SpringBoot 整合 JWT 实现统一认证 2019-04-29
SpringBoot 使用 CompletableFuture 实现非阻塞异步编程 2019-04-29
即刻就业:本科毕业如何快速高薪就业? 2019-04-29
即刻就业:java的应用程序有哪些,java程序有哪些编码规范,开发java应用程序有哪些步骤 2019-04-29