Skip to content

Commit

Permalink
[Gold V] Title: 같은 나머지, Time: 0 ms, Memory: 2156 KB -BaekjoonHub
Browse files Browse the repository at this point in the history
  • Loading branch information
belowyoon committed Mar 25, 2024
1 parent 2e9d6d2 commit c84f8ec
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
32 changes: 32 additions & 0 deletions 백준/Gold/1684. 같은 나머지/README.md
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>

43 changes: 43 additions & 0 deletions 백준/Gold/1684. 같은 나머지/같은 나머지.cc
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;
}

0 comments on commit c84f8ec

Please sign in to comment.