[GCD 합]-9613번
My answer
import sys
import itertools
n,sums=int(input()),0
for i in range(n):
tmp=list(map(int,sys.stdin.readline().split()))
for j in itertools.combinations(tmp[1:], 2):
a,b=max(j[0],j[1]),min(j[0],j[1])
while(b!=0):
a=a%b
a,b=b,a
sums=sums+a
print(sums)
sums=0
Another answer
from itertools import *
from math import gcd
for _ in range(int(input())):
l = list(map(int, input().split()))[1:]
print(sum(starmap(gcd, combinations(l,2))))
728x90
반응형
'코딩테스트 > 백준[Python]' 카테고리의 다른 글
[Python/백준] #2089- [-2진수] (0) | 2021.12.06 |
---|---|
[Python/백준] #11005- [진법 변환 2] (0) | 2021.12.05 |
[Python/백준] #1850- [최대공약수] (0) | 2021.12.04 |
[Python/백준] #1934- [최소공배수] (0) | 2021.12.04 |
[Python/백준] #1158- [요세푸스 문제] (0) | 2021.12.04 |