【HDU】 5088 Revenge of Nim II 高斯消元

http://blog.csdn.net/u013368721/article/details/40706731

亦或操作在二进制中就是加减操作~

题目分析:题意其实就是:取k个数,使得可以异或出0来。而我们如果将这k个数视为k行,每个数的每一位视为一列,那么这k个数边构成了一个01矩阵。那么能异或出0的充分条件是对这01矩阵高斯消元以后矩阵的秩小于矩阵的行数(也即存在一行全零,全零行就是异或出来的一行),那么我们只要对这个01矩阵高斯消元即可。如果不存在全零行则输出No,否则输出Yes。

PS:小优化,1e12比2^40略小,所以列数不会超过40,因为矩阵的秩不会超过min(行数,列数),所以当n>40时一定存在全零行,直接输出Yes即可,不然就高斯消元判断。



代码1:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std ;

#define clr( a , x ) memset ( a , x , sizeof a )

typedef long long LL ;

const int MAXN = 40 ;

LL a[MAXN] ;
int n ;

int gauss ( int n , int var ) {
	int r , c , tmp_r ;
	for ( r = 0 , c = 0 ; r < n && c < var ; ++ r , ++ c ) {
		for ( tmp_r = r ; tmp_r < n ; ++ tmp_r ) if ( a[tmp_r] & ( 1LL << c ) ) break ;
		if ( tmp_r == n ) {
			-- r ;
			continue ;
		}
		swap ( a[tmp_r] , a[r] ) ;
		for ( int i = r + 1 ; i < n ; ++ i ) if ( a[i] & ( 1LL << c ) ) a[i] ^= a[r] ;
	}
	return r < n ;
}

int solve () {
	scanf ( "%d" , &n ) ;
	for ( int i = 0 ; i < n ; ++ i ) {
		if ( i > 40 ) scanf ( "%*I64d" ) ;
		else scanf ( "%I64d" , &a[i] ) ;
	}
	if ( n > 40 ) return 1 ;
	if ( gauss ( n , 40 ) ) return 1 ;
	return 0 ;
}

int main () {
	int T ;
	scanf ( "%d" , &T ) ;
	while ( T -- ) printf ( solve () ? "Yes\n" : "No\n" ) ;
	return 0 ;
}

代码2:

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

using namespace std;

typedef long long int LL;

int n;
LL c[60];
LL a[1100];

int main()
{
    int T_T;
    scanf("%d",&T_T);
    while(T_T--)
    {
        memset(c,0,sizeof(c));
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            cin>>a[i];
        bool flag=false;
        for(int i=1;i<=n;i++)
        {
            for(int j=60;j>=0;j--)  // 进行列主消元,求上三角
            {
                if(c[j]==0&&(a[i]&(1LL<<j)))
                {
                    c[j]=a[i];
                    break;
                }
                else if(a[i]&(1LL<<j))
                {
                    a[i]^=c[j];
                    if(a[i]==0) flag=true;
                }
            }
        }
        if(flag==true) puts("Yes");
        else puts("No");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值