Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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")
posted on 2015-03-12 05:42  Tonix  阅读(288)  评论(0编辑  收藏  举报