17087鬼ごっこ(C++)


#include <iostream>
#include<cstdlib>
using namespace std;

int gcd(int a, int b) {
	while (b != 0) {
		int r = a % b;
		a = b;
		b = r;
	}
	return a;
}
int main() {
	int N, S, g;
	cin >> N >> S;
	int arr[100000];
	for (int i = 0; i < N; i++) {
		cin >> arr[i];
	}
	g = S - arr[0];
	for (int i = 1; i < N; i++) {
		g = gcd(g, (S - arr[i]));
	}
	cout << abs(g);
}
最大公約数/ユークリッドアーク除算関連投稿->https://velog.io/@kimeunseo58/%EC%B5%9C%EB%8C%80%EA%B3%B5%EC%95%BD%EC%88%98%EC%B5%9C%EC%86%8C%EA%B3%B5%EB%B0%B0%EC%88%98%EC%9C%A0%ED%81%B4%EB%A6%AC%EB%93%9C%ED%98%B8%EC%A0%9C%EB%B2%95