hdu5902 GCD is Funny

题目链接: hdu5902 ( GCD is Funny )

一开始大意了,直接求出两两 \(gcd\) 就交了,毫无悬念地 \(WA\) 了一发。

实际是求所有 \(2\le size <n\) 的子集的 \(gcd\) .

/**
 * hdu5902 GCD is Funny
 *
 */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
LL gcd(LL a, LL b)
{
    LL t;
    while (b) {
        t = a%b;
        a = b;
        b = t;
    }
    return a;
}

const int N = 1003;
int g[N][N], a[N], book[N];
int main()
{

    for (int i = 0; i <= 1000; ++i) {
        for (int j = 0; j <= 1000; ++j) {
            g[i][j] = gcd(i, j);
        }
    }

    int T;
    scanf("%d", &T);
    for (int cas = 1; cas <= T; ++cas) {
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; ++i) scanf("%d", a+i);
        for (int i = 1; i <= n; ++i) {
            for (int j = i+1; j <= n; ++j) {
                book[ g[a[i]][a[j]] ] = cas;
            }
        }
        bool flag;
        for (int _ = 4; _ <= n; ++_) {
            flag = 1;
            for (int i = 1; i <= 1000; ++i) {
                if (book[i] != cas) continue;
                for (int j = 1; j <= n; ++j) {
                    int t = g[i][a[j]];
                    if (book[t] != cas) flag = 0;
                    book[t] = cas;
                }
            }
            if (flag) break;
        }
        flag = 1;
        for (int i = 1; i <= 1000; ++i) if (book[i] == cas) {
            if (flag) flag = 0, printf("%d", i);
            else printf(" %d", i);
        }
        puts("");
    }
    return 0;
}

posted @ 2021-02-17 20:03  Zewbie  阅读(38)  评论(0编辑  收藏  举报