[알고리즘/백준] 9613: GCD 합(python)

유현민·2022년 4월 10일
0

알고리즘

목록 보기
115/253

파이썬의 gcd와 combinations를 사용해서 문제를 풀었다. 두개를 짝지어서 gcd를 구해주면 된다.

2중 for문으로 풀어도 되지만 combinations를 쓰는게 더 편함

from math import gcd
from itertools import combinations


for i in range(int(input())):
    ans = 0
    a = list(map(int, input().split()))
    for j in combinations(a[1:], 2):
        ans += gcd(j[0], j[1])
    print(ans)
profile
smilegate megaport infra

0개의 댓글