-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Gold V] Title: 같은 나머지, Time: 0 ms, Memory: 2156 KB -BaekjoonHub
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# [Gold V] 같은 나머지 - 1684 | ||
|
||
[문제 링크](https://www.acmicpc.net/problem/1684) | ||
|
||
### 성능 요약 | ||
|
||
메모리: 2156 KB, 시간: 0 ms | ||
|
||
### 분류 | ||
|
||
유클리드 호제법, 수학, 정수론 | ||
|
||
### 제출 일자 | ||
|
||
2024년 3월 25일 17:31:56 | ||
|
||
### 문제 설명 | ||
|
||
<p>정수 N을 정수 D로 나눴을 때의 몫을 Q, 나머지를 R이라고 하면 항등식 R = N - Q×D가 성립한다.</p> | ||
|
||
<p>n개의 정수로 된 수열이 있을 때, 모든 정수를 한 정수 D로 나눴을 때 나머지가 같아지는 경우가 있다. 그리고 수열에 따라서는 이러한 정수 D가 여러 개 존재할 수 있다.</p> | ||
|
||
<p>n개의 정수로 된 수열이 주어졌을 때, 가장 큰 D를 구하는 프로그램을 작성하시오.</p> | ||
|
||
### 입력 | ||
|
||
<p>첫째 줄에 n(1 ≤ n ≤ 1,000)이 주어진다. 다음 줄에는 절댓값이 1,000,000을 넘지 않는 n개의 정수들이 주어진다.</p> | ||
|
||
### 출력 | ||
|
||
<p>첫째 줄에 가장 큰 D를 출력한다. 항상 가장 큰 D가 존재하는 경우만 입력으로 주어진다.</p> | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <vector> | ||
using namespace std; | ||
|
||
int gcd(int a, int b) { | ||
while(b) { | ||
int c = a % b; | ||
a = b; | ||
b = c; | ||
} | ||
return a; | ||
} | ||
|
||
int main() | ||
{ | ||
ios_base::sync_with_stdio(0); | ||
cin.tie(0); | ||
//freopen("input.txt", "r", stdin); | ||
|
||
int n, sum = 0; | ||
|
||
cin >> n; | ||
vector<int> arr(n,0); | ||
|
||
for (int i = 0; i < n; i++) { | ||
cin >> arr[i]; | ||
} | ||
sort(arr.begin(), arr.end()); | ||
|
||
vector<int> subs; | ||
for (int i = 1; i < n; i++) { | ||
subs.push_back(arr[i] - arr[i-1]); | ||
} | ||
|
||
int result = subs[0]; | ||
for (int i = 1; i < n-1; i++) { | ||
result = gcd(subs[i], result); | ||
} | ||
|
||
cout << result; | ||
return 0; | ||
} |