zoukankan      html  css  js  c++  java
  • 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], " 
    "[i == n]);
    			return 0;
    		}
    	}
    	puts("NO");
    	return 0;
    }
    
  • 相关阅读:
    HTML超文本标记语言(八)——表单<form>
    如何理解JavaScript中给变量赋值,是引用还是复制
    PostCSS 实战
    sass 工具库
    微信小程序-制作简易豆瓣笔记
    学习pano2vr制作html5全景笔记
    git命令笔记
    Gulp实战
    html5-离线存储
    html5
  • 原文地址:https://www.cnblogs.com/realSpongeBob/p/11416754.html
Copyright © 2011-2022 走看看