zoukankan      html  css  js  c++  java
  • 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坑
  • 相关阅读:
    XML WebService完全实例详细解析
    List (Java 2 Platform SE 5.0)
    frameset
    关于在outlook2007里面编辑签名的问题
    关于javax.servlet.Http.*;不能被引用的问题
    select标签HTML,刚做地。
    UIButton中setTitleEdgeInsets和setImageEdgeInsets的使用
    玩转UICollectionViewLayout
    常用公共方法
    cell嵌套UIWebView遇到的几个问题
  • 原文地址:https://www.cnblogs.com/IKnowYou0/p/6059691.html
Copyright © 2011-2022 走看看