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;
    }
    
  • 相关阅读:
    HDOJ 1556 线段树
    POJ 3977 折半枚举
    2017ACM省赛选拔赛题解
    关于四舍五入和截断
    POJ 3422 最小费用最大流
    Codeforces Round #407 (Div. 2) D. Weird journey 思维+欧拉
    POJ 3155 最大密度子图
    无向图最小割 stoer_wagner算法
    最大权闭合子图
    L2-001. 紧急救援 Dijkstra
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798493.html
Copyright © 2011-2022 走看看