HackerRank - "Sherlock and GCD"

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")
时间: 2024-12-26 19:28:27

HackerRank - "Sherlock and GCD"的相关文章

Sherlock and GCD

1 import fractions, functools, sys 2 3 4 if __name__ == '__main__': 5 T = int(sys.stdin.readline()) 6 7 for _ in range(T): 8 N = int(sys.stdin.readline()) 9 A = list(map(int, sys.stdin.readline().split())) 10 11 gcd = functools.reduce(fractions.gcd,

HackerRank - Sherlock and Queries

All about pruning and duplication removal. Took me several submissions to get it AC: #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <unordered_map> using namespace

HackerRank - Sherlock and The Beast

Greedy beats DP this time... I tried several DP solutions first, but all failed with RE\TLE. If you 'feel' the problem, Greedy should be working: (A solution from discussion) def getPivot(n): while n > 0: if n % 3 == 0: break; else: n -= 5 return n T

HackerRank - Sherlock and Anagram

Please note input constraints. String length will not exceed 100, which means, we can use relatively naive representation\calculation for anagrams: sorting. #include <cmath> #include <cstdio> #include <vector> #include <iostream> #

【HackerRank】Sherlock and MiniMax

题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer M between P and Q(both inclusive), such that, min {|Ai-M|, 1 ≤ i ≤ N} is maximised. If there are multiple solutions, print the smallest one. Input For

【HackerRank】Sherlock and Array

Watson gives an array A1,A2...AN to Sherlock. Then he asks him to find if there exists an element in the array, such that, the sum of elements on its left is equal to the sum of elements on its right. If there are no elements to left/right, then sum

【HackerRank】 Sherlock and The Beast

Sherlock and The Beast Sherlock Holmes is getting paranoid about Professor Moriarty, his archenemy. All his efforts to subdue Moriarty have been in vain. These days Sherlock is working on a problem with Dr. Watson. Watson mentioned that the CIA has b

Hackerrank GCD Product(莫比乌斯反演)

题意 题目链接 Sol 一道咕咕咕了好长时间的题 题解可以看这里 #include<bits/stdc++.h> #define LL long long using namespace std; const int MAXN = 1e7 + 5e6 + 10, mod = 1e9 + 7, mod2 = 1e9 + 6; int N, M, vis[MAXN], prime[MAXN], mu[MAXN], f[MAXN], tot; int add(int x, int y) { if(x

dutacm.club 1094: 等差区间(RMQ区间最大、最小值,区间GCD)

1094: 等差区间 Time Limit:5000/3000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/Others)Total Submissions:655   Accepted:54 [Submit][Status][Discuss] Description 已知一个长度为 n 的数组 a[1],a[2],-,a[n],我们进行 q 次询问,每次询问区间 a[l],a[l+1],-,a[r?1],a[r] ,数字从小到大