首页 > 其他 > 详细

HDU 5019 Revenge of GCD

时间:2014-10-14 16:41:20      阅读:195      评论:0      收藏:0      [点我收藏+]

题解:筛出约数,然后计算即可。

#include <cstdio>
#include <algorithm>
typedef long long LL; 
LL a1[1000005],a2[1000005],x,y,k,g;
int cnt1,cnt2,T;
LL gcd(LL a,LL b){if(b==0)return a;else return gcd(b,a%b);}
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%I64d%I64d%I64d",&x,&y,&k);
        g=gcd(x,y); cnt1=cnt2=0;
        if(k==1){printf("%I64d\n",g);continue;}
        if(k>2000010){puts("-1");continue;}
        for(LL i=1;i*i<=g;i++)if(g%i==0){
            a1[++cnt1]=i;
            if(i*i!=g)a2[++cnt2]=g/i;
        }if(k>cnt1+cnt2)puts("-1");else if(k<=cnt2)printf("%I64d\n",a2[k]);else printf("%I64d\n",a1[cnt1-k+cnt2+1]);
    }return 0;
}
//注意循环也要用long long 否则会出错

HDU 5019 Revenge of GCD

原文:http://www.cnblogs.com/forever97/p/4024493.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!