AtCoder Regular Contest 105 ---- B

题目链接:https://atcoder.jp/contests/arc105/tasks/arc105_b

题意:给定n个数字,有两个操作:第一个就是将最大值的数字更新为最大值-最小值,第二个就是当所有数字都相同时,退出程序,输出此时的值。

解题思路:由于gcd(x,y) = gcd(x, y - x),于是我们就可以找出这n个数字的最大公约数a,这样就能使得每一个数字再减去不同次的a后,都变得相同。

#include <bits/stdc++.h>

using namespace std;

int main() {
	int n, g = 0;	cin >> n;
	for(int i = 1; i <= n; i++) {
		int x;	cin >> x;
		g = __gcd(x, g);
	}
	cout << g << endl;
	return 0;
}

  

posted @ 2020-10-12 09:24  ACM-Epoch  阅读(132)  评论(0编辑  收藏  举报