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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5088

题意:

有n堆石头,可以去掉其中的一些堆(保证至少剩下一堆)问存不存在一种方法使第二个人赢;

分析:

这k个数边构成了一个01矩阵。那么能异或出0的充分条件是对这01矩阵高斯消元以后矩阵的秩小于矩阵的行数(也即存在一行全零,全零行就是异或出来的一行),那么我们只要对这个01矩阵高斯消元即可。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1005;
int step[maxn],dp[maxn], num[maxn];
int main ()
{
    int T;
    scanf("%d", &T);
    while(T--){
        int n;
        scanf("%d", &n);
        memset(dp, 0, sizeof(dp));
        memset(step, 0, sizeof(step));
        dp[0] = 1;
        for(int i = 1; i <= n; i++)
            scanf("%d", &num[i]);
        int Max = -1;
        for(int i = 1; i <= n; i++){
            for(int j = 0; j < i; j++){
                if(num[i] > num[j]){
                    if(step[j]+1 > step[i]){
                        step[i] = step[j] + 1;
                        dp[i] = dp[j];
                    }
                    else if(step[j] + 1 == step[i])
                        dp[i] += dp[j];
                }
            }
            Max = max(Max, step[i]);
        }
        int flag = 0;
        int ok = 1;
        for(int i = 1; i <= n; i++){
            if(step[i] == Max){
                if(dp[i] > 1) ok = 0;
                else{
                    if(flag) ok = 0;
                    flag = 1;
                }
            }
        }
        printf("%d\n", Max - ok );
    }
    return 0;
}
时间: 2024-08-09 19:54:11

HDU5088Revenge 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(高斯消元)

题目链接:hdu 5088 Revenge of Nim II 题目大意:Nim游戏的变形,因为游戏很不公平,所以现在转变规则,后手可以选取若干堆石子剔除,剩下堆的石子用 来进行游戏,问说后手可能胜利吗. 解题思路:其实即为取出非0堆石子,使得Nim和为0.因为是Nim和(亦或),所以以每个位建立方程,列出40个方 程,进行亦或形式的高斯消元,因为全0肯定为一解,所以方程肯定有解,那么存在多解的情况即为存在自有变元. #include <cstdio> #include <cstring

SGU 275 To xor or not to xor 高斯消元求N个数中选择任意数XORmax

275. To xor or not to xor The sequence of non-negative integers A1, A2, ..., AN is given. You are to find some subsequence Ai 1, Ai 2, ..., Ai k (1 <= i 1 < i 2 < ... < i k<= N) such, that Ai 1 XOR Ai 2 XOR ... XOR Ai k has a maximum value.

hdu 2262 高斯消元求期望

Where is the canteen Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1070    Accepted Submission(s): 298 Problem Description After a long drastic struggle with himself, LL decide to go for some

高斯消元 求整数解模版

#include <iostream> #include <string.h> #include <cmath> using namespace std; const int maxn = 105; int equ, var; // 有equ个方程,var个变元.增广阵行数为equ, 分别为0到equ - 1,列数为var + 1,分别为0到var. int a[maxn][maxn]; int x[maxn]; // 解集. bool free_x[maxn]; //

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

hdu 4418 高斯消元求期望

Time travel Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1480    Accepted Submission(s): 327 Problem Description Agent K is one of the greatest agents in a secret organization called Men in B

uva 10828 高斯消元求数学期望

Back to Kernighan-RitchieInput: Standard Input Output: Standard Output You must have heard the name of Kernighan and Ritchie, the authors of The C Programming Language. While coding in C, we use different control statements and loops, such as, if-the

HDU4870_Rating_双号从零单排_高斯消元求期望

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 654    Accepted Submission(s): 415 Special Judge Problem Description A little gir