实际上这题时GCD的。OK,想了想证明过程:
粗糙的归纳法:
1.2个数,显然就是GCD(a1,a2)
2.n个数,按此操作为GCD(a1,a2,a3…,an)
3.n+1个数,任取n个数按此操作得到的最小数GCD(a1,a2..,an),对第n+1个数a(n+1),按此操作则必定为GCD(a1,a2….,an,an+1)
归纳完毕。
当然上述肯定会有疑惑的地方,想必第3步时最令人困惑的,但给出这个公式就好了gcd(a,b)=gcd(a,b - a) = gcd(a,b + a) (b > a),再推广到多个数gcd的情况即可。
#include <iostream>
#include <cstdio>
#include <list>
#include <stack>
#include <queue>
#include <cstdlib>
#include <set>
#include <map>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
#define INF 0x3f3f3f3f
int a[105];
int main()
{
int n, x;
while (cin >> n) {
int c = 0;
memset(a, 0, sizeof(a));
for (int i = 1; i <= n; ++i) {
scanf("%d", &x);
a[x]++;
if (a[x] == 1) c++;
}
while (c != 1) {
int x, y, pre, Min = INF;
x = y = pre = -1;
for (int i = 1; i <= 100; ++i) {
if (pre == -1 && a[i]) {
pre = i;
continue;
}
if (a[i]) {
if (i - pre < Min) {
Min = i - pre;
x = pre, y = i;
}
pre = i;
}
}
if (x != -1) {
a[y - x]++;
if (a[y - x] == 1)c++;
a[y]--;
if (!a[y])c--;
}
}
for (int i = 1; i <= 100; ++i) {
if (a[i]) {
printf("%d\n", a[i] * i);
break;
}
}
}
return 0;
}