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


    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循环暴力求即可

    #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
    ");
            }
            else
            {
                sort(x, x + j);
                printf("%lld
    ", x[j - k]);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    js原型链
    js的__proto__,prototype、constructor属性
    百度ife2015-小白的弯路2
    百度ife2015-小白的弯路1
    Visaul Studio 密钥
    pycharm常用的一些快捷键
    python3练习题--字符串
    字符串相关方法
    python3 基本数据类型
    在python中缩进的重要性
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798493.html
Copyright © 2020-2023  润新知