※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 23604번 문제인 Chinese Remainder Theorem이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/23604
23604번: Chinese Remainder Theorem
Johnny is a computer science student. This semester he became well versed with the Chinese Remainder Theorem. While waiting for a next lecture he heard that Maggie complained that she cannot solve her homework; as he heard the familiar words "modulo" and
www.acmicpc.net
따라서 모든
두 수의 최대공약수는 유클리드 호제법(Euclidean Algorithm)을 이용해 계산할 수 있다. 여러 수의 최대공약수는 둘씩 최대공약수를 구하는 것을 반복하는 것으로 구할 수 있다.
아래는 제출한 소스코드이다.
#include <iostream>
using namespace std;
typedef long long ll;
ll gcd(ll x, ll y) {
if (y) return gcd(y, x % y);
return x;
}
int N; ll ans;
ll A[100000], B;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 0; i < N; i++) cin >> A[i];
cin >> B; ans = abs(A[0] - B);
for (int i = 1; i < N; i++) {
cin >> B;
ans = gcd(ans, abs(A[i] - B));
}
cout << ans;
}
'BOJ' 카테고리의 다른 글
[BOJ 14515 // C++] Yin and Yang Stones (0) | 2024.04.24 |
---|---|
[BOJ 4324 // C++] XYZZY (0) | 2024.04.23 |
[BOJ 16481 // C++] 원 전문가 진우 (0) | 2024.04.21 |
[BOJ 16509 // C++] 장군 (1) | 2024.04.20 |
[BOJ 17236 // C++] Heights (1) | 2024.04.19 |