前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >数学--数论--HDU 5019 revenge of GCD

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

作者头像
风骨散人Chiam
发布2020-11-06 00:48:35
3590
发布2020-11-06 00:48:35
举报
文章被收录于专栏:CSDN旧文CSDN旧文

Revenge of GCD

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

for循环暴力求即可

代码语言:javascript
复制
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
long long  x[500000], a, b, k;
int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        memset(x, 0, sizeof(x));
        scanf("%lld%lld%lld", &a, &b, &k);
        long long  d = __gcd(a, b);
        long long  i, j = 0;
        for (i = 1; i * i <= d; ++i)
        {
            if (d % i == 0)
            {
                x[j++] = i;
                if (i * i != d)
                    x[j++] = d / i;
            }
        }

        if (j < k)
        {
            printf("-1\n");
        }
        else
        {
            sort(x, x + j);
            printf("%lld\n", x[j - k]);
        }
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-12-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档