首页 > 代码库 > HDU 5019 Revenge of GCD
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.
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
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
32 3 12 3 28 16 3
Sample Output
1-12
Source
BestCoder Round #10
条体就是问 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。