http://codeforces.com/problemset/problem/389/A

实际上这题时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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值