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坑
  • 相关阅读:
    ApplicationContext
    消息队列种类
    Zookeeper的功能以及工作原理
    java多线程(1)模式 不可变对象
    dubbo容错,负载均衡,协议
    dubbo的底层原理
    消息事务处理
    多线程循环打印 abc
    Spring 事务管理
    文件上传和下载
  • 原文地址:https://www.cnblogs.com/IKnowYou0/p/6059691.html
Copyright © 2011-2022 走看看