zoukankan      html  css  js  c++  java
  • 数学--数论--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;
    }
    
  • 相关阅读:
    珠海洪锐在线监测agent_linux系统
    python中的不定长参数
    记一次刻苦铭心得安装zabbix经历
    狼书第三章Jinja2模板总结
    关于消息闪现的问题
    了解HTTP状态码
    关于用Flask建立一个简单的web应用
    将模块安装到Site-packages
    在Centos6中安装python3.6
    unity 生成缩略图 , 图片缩放
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798493.html
Copyright © 2011-2022 走看看