• HDU 5019 Revenge of GCD


    http://acm.hdu.edu.cn/showproblem.php?pid=5019

    Revenge of GCD

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


    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
     1 #include <iostream>
     2 #include <vector>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 long long gcd(long long a, long long b)
     7 {
     8     if (b == 0)
     9         return a;
    10     return gcd(b, a%b);
    11 }
    12 
    13 
    14 int main() {
    15     vector<long long> vec;
    16     int T;
    17     long long x, y, k;
    18     cin >> T;
    19 
    20     while (T--) {
    21         vec.clear();
    22         cin >> x >> y >> k;
    23         long long mg = gcd(x, y);
    24         
    25         for (long long  i = 1; i*i <= mg; i++) {
    26             if (mg%i == 0) {
    27                 vec.push_back(i);
    28                 if (i*i != mg)
    29                     vec.push_back(mg / i);
    30             }
    31         }
    32 
    33         sort(vec.begin(), vec.end());
    34         if (vec.size() <k) cout <<-1<< endl;
    35         else cout << vec[vec.size() - k] << endl;
    36    }
    37     return 0;
    38 }
    自己选的路,跪着也要把它走完------ACM坑
  • 相关阅读:
    代码收藏
    sql语句收藏
    网站收藏
    Api代码收集
    [网络收集]html 一个属性,多个数据
    [转] C#自定义的字符串操作增强类
    WebBrowser记录
    [转载]用C#的GDI+技术生成复杂型彩色验证码
    js addEventListener attachEvent用法
    正则表达式 获取常见所有标点符号
  • 原文地址:https://www.cnblogs.com/IKnowYou0/p/6059691.html
Copyright © 2020-2023  润新知