유클리드를 O(n)만큼 시도해야 한다는 것에 주의.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int Euclid(int a,int b)
{
if (b == 0) return a;
else return Euclid(b, a % b);
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
int N, S;
cin >> N >> S;
vector<int> v;
if (N == 1)
{
int input;
cin >> input;
cout << abs(input - S);
return 0;
}
else
{
for (int i = 0; i < N; i++)
{
int input;
cin >> input;
v.push_back(abs(input - S));
}
sort(v.begin(), v.end());
int tmp = v[0];
for (int i = 1; i < N; i++)
{
tmp = Euclid(tmp, v[i]);
}
cout << tmp;
}
return 0;
}
'프로그래밍 공부 > 백준 (C++)' 카테고리의 다른 글
[C++/백준 1212번] -2진수 (0) | 2024.10.28 |
---|---|
[C++/백준 1212번] 8진수 2진수 (0) | 2024.10.27 |
[C++/백준 9613번] GCD 합 (0) | 2024.10.26 |
[C++/백준 2004번] 조합 0의 개수 (0) | 2024.10.26 |
[C++/백준 6588번] 골드바흐의 추측 (0) | 2024.10.25 |