Codeforces 1198F

题目链接:Codeforces 1198F

Description

给出 \(n\) 个数,把它们分成两组使得两组的 \(\gcd\) 均为 \(1\),或判断无解。

Solution

随机的都是好东西

太菜了不会正解只能用随机来骗分。

先把序列打乱。

如果当前数加到第一组使其 \(\gcd\) 变小了就加到第一组,否则加到第二组。

然后卡一下时间。

证明不存在的


Code

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <algorithm>
 
const int MAXN = 1e5 + 10;
int n, a[MAXN], id[MAXN], ans[MAXN];
 
int gcd(int x, int y) {
    return y ? gcd(y, x % y) : x;
}
 
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), id[i] = i;
    clock_t st = clock();
    while (clock() - st < 0.4 * CLOCKS_PER_SEC) {
        std::random_shuffle(id + 1, id + n + 1);
        int X = 0, Y = 0;
        for (int i = 1; i <= n; ++i) {
            int t = gcd(X, a[id[i]]);
            if (t == X) Y = gcd(Y, a[id[i]]), ans[id[i]] = 2;
            else X = t, ans[id[i]] = 1;
        }
        if (X == 1 && Y == 1) {
            puts("YES");
            for (int i = 1; i <= n; ++i) printf("%d%c", ans[i], " \n"[i == n]);
            return 0;
        }
    }
    puts("NO");
    return 0;
}

转载于:https://www.cnblogs.com/realSpongeBob/p/11416754.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值