Revenge of GCD HDU - 5019

传送门

题意:给两个数x,y,求他们的第k大的因子;
思路:首先找到他们的最大公因数,直接用自带的函数__gcd()找到就行,而且这里其他的公因子也一定是最大公因子的因子,直接从1暴力枚举到最大公因子,判断是不是他的因子,同时w/i肯定也是他的因子,但是这里要判断一下i*i==w,避免重复;然后就他的所有因子都找到了,然后直接sort排序一下,输出答案即可

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<queue>
#define lson l, mid, root << 1
#define rson mid + 1, r, root << 1 | 1
#define ftr  l , r , root
#define lowbit(x) ( x & ( - x ) )
#define mod 2009
#define ll long long
using namespace std;
const int maxn = 1e6+10;
const int inf = 0x3f3f3f3f;
ll a[maxn];
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		ll x,y,k;
		scanf("%lld%lld%lld",&x,&y,&k);
		memset(a,0,sizeof(a));
		ll w=__gcd(x,y);
		int cnt=0;
		for(ll i=1;i*i<=w;i++){
			if(w%i==0){			
				a[cnt++]=i;
				if(i*i!=w){
					a[cnt++]=w/i;
				}
			}
		}
		sort(a,a+cnt);
		if(k>cnt)printf("-1\n");
		else printf("%lld\n",a[cnt-k]);
	} 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值