HDU 5019 Revenge of GCD

来源:互联网 发布:淘宝 不良资产 在哪里 编辑:程序博客网 时间:2024/06/13 14:19

链接 : http://acm.hdu.edu.cn/showproblem.php?pid=5019


题意:输出x和y的第k个最大公约数


先找出x和y的最大公约数,再对公约数找约数


#include <iostream>#include <algorithm>using namespace std;__int64 s[1000001];__int64 gcd(__int64 a,__int64 b){    __int64 t;    while(b)    {        t=a%b;        a=b;        b=t;    }    return a;}bool cmp(__int64 a,__int64 b){    return a>b;}int main(){    int T;    __int64 x,y,k,i,j;    cin>>T;    while(T-- && cin>>x>>y>>k)    {        __int64 gcd1=gcd(x,y);        __int64 cnt=0;        for(i=1;i*i<=gcd1;i++)        {            if(!(gcd1%i))            {                if(i*i==gcd1) s[cnt++]=i;                 else                {                    s[cnt++]=i;                    s[cnt++]=gcd1/i;                }            }        }        if(k>cnt) cout<<"-1"<<endl; //给定k大于找出的约数总数        else        {            sort(s,s+cnt,cmp);            cout<<s[k-1]<<endl;        }    }}


0 0
原创粉丝点击