码迷,mamicode.com
首页 > 其他好文 > 详细

HackerRank - "Sherlock and GCD"

时间:2015-03-12 06:23:29      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

This is a very smart observation:
http://www.martinkysel.com/hackerrank-sherlock-and-gcd-solution/

# http://www.martinkysel.com/hackerrank-sherlock-and-gcd-solution/
#
from functools import reduce

def gcd(a, b):
    if a == 1 or b == 1:
        return 1
    if a % b == 0:
        return b
    return gcd(b, a % b)
    
def multiple_gcd(arr):
    return reduce(lambda x,y: gcd(x, y), arr)
    
#################
T = int(input())
for i in range(0,T):
    n = int(input())
    arr = list(map(int, input().split()))
    if len(arr) < 2:
        print ("NO")
        continue
    if (multiple_gcd(arr) == 1):
        print ("YES")
    else:
        print ("NO")

HackerRank - "Sherlock and GCD"

标签:

原文地址:http://www.cnblogs.com/tonix/p/4331426.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!