zoukankan      html  css  js  c++  java
  • HDU 5019 Revenge of GCD

    Revenge of GCD

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

    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
     
    Source
     

    条体就是问 a , b 两个数的第k大GCD是什么 -。-

    比赛的时候用根号n找出所有GCD, 然后排序 , 结果T 了 。

    后来用两个数组分别存 1 ~ sqrt(a) 还有 sqrt(a)~a 的。

    其实求的顺序已经一个从小到大, 一个从大到小 。。

    然后复杂度是  T * sqrt(n)。。。 0.5秒过了。

    之前多加排序  T * ( 2 * sqrt(n) log( 2 * sqrt(n) ) + sqrt(n)  )   .. 果断超了~~

    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <vector>
    #include <cstdio>
    using namespace std;
    typedef long long LL;
    const int N = 10010 ;
    LL a , b, k;
    
    vector<LL>f1,f2;
    
    void cal()
    {
        for(LL i = 1; i * i <= a ; ++i ){
            if( a % i == 0 ){
                if(  b % i == 0 ){
                    f1.push_back(i);
                }
                LL temp = a / i ;
                if( temp != i && b % temp == 0 ){
                    f2.push_back( temp );
                }
            }
        }
    }
    
    int main() { #ifdef LOCAL freopen("in.txt","r",stdin); #endif // LOCAL int cas =1 , _; scanf("%d",&_); while(_--) { scanf("%I64d%I64d%I64d",&a,&b,&k); if( a > b ){ swap(a , b); } f1.clear(); f2.clear(); cal(); int len1 = (int ) f1.size() ,len2 = (int )f2.size(); if( len1 + len2 < k ) puts("-1"); else{ if( k <= len2 ) printf("%I64d ",f2[ k - 1 ]); else { k -= len2 ;
    printf("%I64d ",f1[len1-k]); } } } return 0; }
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    常用排序算法(七)归并排序
    常用排序算法(六)堆排序
    常用排序算法(五)选择排序
    常用排序算法(四)希尔排序
    常用排序算法(三)直接插入排序
    常用排序算法(二)快速排序
    常用排序算法(一)冒泡排序
    Linux内核中双向链表的经典实现
    回溯法
    迪杰斯特拉Dijkstra算法介绍
  • 原文地址:https://www.cnblogs.com/hlmark/p/3982683.html
Copyright © 2011-2022 走看看