Baekjoon Case

[파이썬 / 백준 9613번] GCD 합

Scarlett_C 2021. 11. 24. 11:38
728x90

https://www.acmicpc.net/problem/9613

728x90

Combination 함수로 조합을 구한다음에 그 조합의 공약수를 더하면 끗!

from itertools import combinations
from sys import stdin
def ul(a,b):
    while True:
        r=a%b
        if r==0: 
            return b
            break
        else: 
            a,b=b,r
input=stdin.readline
N=int(input())
for _ in range(N):
    NL=list(map(int,input().split()))
    n=NL[0]
    del NL[0]
    SL=list(combinations(NL,2))
    ans=0
    for i in range(len(SL)):
        x=max(SL[i][0],SL[i][1])
        y=min(SL[i][1],SL[i][0])
        ans+=ul(x,y)

    print(ans)
728x90