码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5019 Revenge of GCD

时间:2014-09-19 23:53:16      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   java   ar   

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\n",f2[ k - 1 ]); else { k -= len2 ;
printf("%I64d\n",f1[len1-k]); } } } return 0; }

 

HDU 5019 Revenge of GCD

标签:des   style   blog   http   color   io   os   java   ar   

原文地址:http://www.cnblogs.com/YRETSIM/p/3982683.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!