jae_coding

(백준 알고리즘 문제풀이) 21920번 서로소 평균 본문

알고리즘 문제/수학(math)

(백준 알고리즘 문제풀이) 21920번 서로소 평균

재코딩 2022. 7. 28. 14:59
반응형

문제

문제 링크

 

문제 접근

  • math.gcd활용 ( 최대 공약수 )
  • 최대 공약수가 1이라면 그 두개의 수는 서로소

코드

import sys, math
input = sys.stdin.readline

# 서로소: 1을 제외한 나머지 공약수중 곂치는것이 없는 수
n = int(input())
a = list(map(int, input().split()))
x = int(input())

result = list()
for i in a:
    gcd = math.gcd(i, x)
    if gcd == 1:
        result.append(i)

print(sum(result) / len(result))
반응형
Comments