设为首页 加入收藏

TOP

hdu 5019 Revenge of GCD
2015-07-24 05:21:25 来源: 作者: 【 】 浏览:6
Tags:hdu 5019 Revenge GCD

?

题目大意:给出A,B两个数,求第k大的公约数,如果没有输出-1

思路:直接把A,B的公约数全部求出来,然后找出来就行啦,当时没有注意数据大小居然是10^12,用的int ,所以果断错啦,赛完才发现,坑呀。。。。。

注意要用long long或是__int64。。。。。

code:

?

#include
  
   
#include
   
     #include
    
      #include
     
       #include
      
        using namespace std; typedef __int64 LL; LL gcd(LL a,LL b) { LL r; while(b>0) { r=a%b; a=b; b=r; } return a; } bool cmp(LL a,LL b) { return a>b; } LL f[100000]; int main() { int T; LL x,y,k,i,len; scanf(%d,&T); while(T--) { len=0; scanf(%I64d%I64d%I64d,&x,&y,&k); LL r=gcd(x,y); LL d=sqrt(r); while(d*d<=r) d++; //这个循环和接下来的那个循环可以不要不要,只是曾经写过一个代码是这么写过就这么写的 while(d*d>r) d--; for(i=1;i<=d;i++) { if(r%i==0) { if(i*i==r) { f[len++]=i; } if(i*i
       
        =k) { printf(%I64d ,f[k-1]); } else { printf(-1 ); } } return 0; }
       
      
     
    
   
  


?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇UVA - 10601 Cubes (组合+置换) 下一篇zoj 1944 Tree Recovery (二叉树)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: