1. 程式人生 > >51nod_1179_計算N個數之間兩兩之間GCD的最大值

51nod_1179_計算N個數之間兩兩之間GCD的最大值

這裡寫圖片描述




#include<iostream>
#include<cstdio>
using namespace std;
const int maxs = 1e6 + 10;
int D[maxs];
int main()
{
    int n;
    cin >> n;
    long long x,themax = 0;
    for (int i = 0;i < n;i++)
    {
        scanf("%lld", &x);
        D[x]++;
        if (x > themax)
        {
            themax = x;
        }
    }
    int
ans = -1; for (int i = themax;i > 0;i--) { int sum = 0; for (int j = i;j <= themax&&sum<2;j += i) { sum += D[j]; }if (sum == 2) { ans = i; break; } } cout << ans << endl; return
0; }