hdu 5088 Revenge of Nim II(高斯消元)

题目链接:hdu 5088 Revenge of Nim II

题目大意:Nim游戏的变形,因为游戏很不公平,所以现在转变规则,后手可以选取若干堆石子剔除,剩下堆的石子用

来进行游戏,问说后手可能胜利吗。

解题思路:其实即为取出非0堆石子,使得Nim和为0。因为是Nim和(亦或),所以以每个位建立方程,列出40个方

程,进行亦或形式的高斯消元,因为全0肯定为一解,所以方程肯定有解,那么存在多解的情况即为存在自有变元。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 1000;
typedef long long ll;
typedef int Mat[maxn+5][maxn+5];

int gauss (Mat A, int m, int n) {
    int i = 0, j = 0, k, r, u;
    while (i < m && j < n) {
        r = i;
        for (k = i; k < m; k++) {
            if (A[k][j]) {
                r = k;
                break;
            }
        }

        if (A[r][j]) {
            if (r != i) {
                for (k = 0; k <= n; k++)
                    swap(A[r][k], A[i][k]);
            }

            for (u = i + 1; u < m; u++) {
                if (A[u][j])
                    for (k = i; k <= n; k++)
                        A[u][k] ^= A[i][k];
            }
            i++;
        }
        j++;
    }
    return n - i;
}

int N;
Mat a;

int main () {
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        ll x;
        scanf("%d", &N);
        memset(a, 0, sizeof(a));
        for (int i = 0; i < N; i++) {
            scanf("%I64d", &x);
            for (int j = 0; j < 45; j++)
                a[j][i] = (x>>j)&1;
        }
        int ans = gauss(a, 45, N);
        printf("%s\n", ans ? "Yes" : "No");
    }
    return 0;
}
时间: 2024-08-27 15:20:24

hdu 5088 Revenge of Nim II(高斯消元)的相关文章

HDU5088——Revenge of Nim II(高斯消元&amp;矩阵的秩)(BestCoder Round #16)

Revenge of Nim II Problem DescriptionNim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provided th

hdu 5088 Revenge of Nim II(BestCoder Round #16)

Revenge of Nim II                                                          Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 203    Accepted Submission(s): 63 Problem Description Nim is a mathema

HDU5088Revenge of Nim II(高斯消元求自由变元个数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5088 题意: 有n堆石头,可以去掉其中的一些堆(保证至少剩下一堆)问存不存在一种方法使第二个人赢: 分析: 这k个数边构成了一个01矩阵.那么能异或出0的充分条件是对这01矩阵高斯消元以后矩阵的秩小于矩阵的行数(也即存在一行全零,全零行就是异或出来的一行),那么我们只要对这个01矩阵高斯消元即可. 代码如下: #include<cstdio> #include<cstring> #in

hdu 3992 AC自动机上的高斯消元求期望

Crazy Typewriter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 391    Accepted Submission(s): 109 Problem Description There was a crazy typewriter before. When the writer is not very sober, it

HDOJ 5088 Revenge of Nim II 位运算

位运算.... Revenge of Nim II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 229    Accepted Submission(s): 79 Problem Description Nim is a mathematical game of strategy in which two players take

BZOJ 3105: [cqoi2013]新Nim游戏 [高斯消元XOR 线性基]

以后我也要用传送门! 题意:一些数,选择一个权值最大的异或和不为0的集合 终于有点明白线性基是什么了...等会再整理 求一个权值最大的线性无关子集 线性无关子集满足拟阵的性质,贪心选择权值最大的,用高斯消元判断是否和已选择的线性相关 每一位记录pivot[i]为i用到的行 枚举要加入的数字的每一个二进制为1的位,如果有pivot[i]那么就异或一下(消元),否则pivot[i]=这个数并退出 如果最后异或成0了就说明线性相关... #include <iostream> #include &l

HDU 3359 Kind of a Blur(高斯消元)

题意: H * W (W,H <= 10) 的矩阵A的某个元素A[i][j],从它出发到其他点的曼哈顿距离小于等于D的所有值的和S[i][j]除上可达点的数目,构成了矩阵B.给定矩阵B,求矩阵A. 题目先给宽再给高...坑我了一个小时 code /* 暴力确定每个位置有到那些位置的曼哈顿距离小于D 然后对你n*m个未知数,n*m个方程进行高斯消元 */ #include <iostream> #include <cstdio> #include <cmath> #

[ACM] hdu 2262 Where is the canteen (高斯消元求期望)

Where is the canteen Problem Description After a long drastic struggle with himself, LL decide to go for some snack at last. But when steping out of the dormitory, he found a serious problem : he can't remember where is the canteen... Even worse is t

BZOJ 3569 DZY Loves Chinese II 高斯消元

题目大意:给定一个[魞歄连通图],多次询问当图中某k条边消失时这个图是否联通 强制在线 我们找到这个图的任意一棵生成树 然后对于每条非树边将其的权值赋为一个随机数 对于每条树边 我们将这条树边的权值设为所有覆盖这条树边的边权的异或和 那么图不连通当且仅当删除一条树边和覆盖这条树边的所有边集 而由于刚才的处理一条树边和覆盖这条边的所有边集的异或和为零 于是问题转化成了对于给定的k条边是否存在一个边权的异或和为零的子集 果断高斯消元 由于使用了随机化所以碰撞率极低 好方法学习了...构思真是巧妙 记