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坑
  • 相关阅读:
    oracle 当行函数 日期
    veridata实验举例(1)验证TCUSTMER与TCUSTORD两节点同步情况
    sdut1730 数字三角形问题(dp入门题)
    Android4.0 Design之UI设计易犯的错误2
    怎样提高团队管理能力6
    Effective C++ 29-33
    内存补齐序列一:关于内存对齐和填充
    【 D3.js 入门系列 --- 10.1 】 简化 GeoJSON 文件
    Android TrafficStats类的使用
    新手上路:Laravel-控制器基础
  • 原文地址:https://www.cnblogs.com/IKnowYou0/p/6059691.html
Copyright © 2011-2022 走看看