1. 문제 설명

GCD 합

2. 문제 분석

조합으로 페어를 구한 뒤, 각 GCD의 총합을 구하는 문제.

3. 나의 풀이

import sys
from itertools import combinations
import math

t = int(sys.stdin.readline().rstrip())
for _ in range(t):
    numbers = list(map(int, sys.stdin.readline().rstrip().split()))
    numbers.pop(0)
    total = 0
    for pair in list(combinations(numbers, 2)):
        num1, num2 = pair
        gcd = math.gcd(num1, num2)
        total += gcd
    print(total)
profile
JUST DO IT

0개의 댓글

Powered by GraphCDN, the GraphQL CDN