CF1151B Dima and a Bad XOR

题目乍一看很难做啊!你怎么找出一个xor和大于0的来啊?

好像没有固定算法啊!


那就不要用固定算法,用随机算法啊!(欺负那些脑子不灵通的蒟蒻,比如我)

做法很简单,在每一行都随机一个数,拿来xor,如果可以的话就输出,不能的话就继续找,找到快超时还没找到就直接判无解。

想不到8!我想不到!

代码:

#include<bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
const int maxn = 505;
struct Nodes {
    int val, idx;
} a[maxn][maxn];
int n, m;
int answer[maxn], tot;
int read() {
    int ans = 0;
    char ch = getchar();
    while(ch > '9' || ch < '0') ch = getchar();
    while(ch >= '0' && ch <= '9') ans = ans * 10 + ch - '0', ch = getchar();
    return ans;
}
int main() {
    srand(19260817);
    n = read(), m = read();
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            a[i][j].val = read(), a[i][j].idx = j;
        }
    }
    for(int t = 1; t <= 20000; t++) {
        int res = 0; tot = 0;
        for(int i = 1; i <= n; i++) {
            int idx = rand() % m + 1;
            res ^= a[i][idx].val; answer[++tot] = a[i][idx].idx;
        }
        if(res) {
            puts("TAK");
            for(int i = 1; i <= n; i++) printf("%d ", answer[i]);
            puts("");
            return 0;
        }
    }
    puts("NIE");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Garen-Wang/p/10959547.html
xor