문제) 백준 - 수학 - 서로소 평균
https://www.acmicpc.net/problem/21920
21920번: 서로소 평균
첫 번째 줄에 입력될 수들의 개수 $N$이 주어진다. $(2 \le N \le 500,000)$ 두 번째 줄에는 수열 $A$를 이루는 자연수 $A_{i}$ 가 공백으로 구분되어 주어진다. $(2 \le A_{i} \le 1,000,000)$ 수열 $A$에 $X$와 서로
www.acmicpc.net
서로소를 만족하는 수들의 평균을 구하는 문제였습니다. 유클리드 호제법을 통해 서로소를 판별합니다.
C++ 소스코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#define endl "\n" | |
#define ll long long | |
#define INF 987654321 | |
#define MAX 500001 | |
#define PRIMEMAX 1000001 | |
#define int ll | |
using namespace std; | |
int N; | |
int arr[MAX]; | |
int X; | |
// 6 8 | |
int solve(int a, int b){ | |
if(a > b) swap(a, b); | |
if(b % a == 0) return a; | |
return solve(b % a, a); | |
} | |
signed main() { | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); cout.tie(0); | |
cin >> N; | |
for(int i = 0; i < N; ++i) | |
cin >> arr[i]; | |
cin >> X; | |
double ans = 0; | |
int cnt = 0; | |
for(int i = 0; i < N; ++i){ | |
if(solve(X, arr[i]) == 1){ | |
cnt++; | |
ans += arr[i]; | |
} | |
} | |
printf("%lf",ans / cnt); | |
return 0; | |
} |
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 11568번 - 민균이의 계략 (C++) 문제 및 풀이 (0) | 2022.03.11 |
---|---|
[백준] 10159번 - 저울 (C++) 문제 및 풀이 (0) | 2022.03.11 |
[백준] 13164번 - 행복 유치원 (C++) 문제 및 풀이 (0) | 2022.03.11 |
[백준] 14863번 - 서울에서 경산까지 (C++) 문제 및 풀이 (0) | 2022.03.08 |
[백준] 9024번 - 두 수의 합 (C++) 문제 및 풀이 (0) | 2022.03.07 |
댓글