首页 > 代码库 > HDU 5019 Revenge of GCD

HDU 5019 Revenge of GCD

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

#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