传送门:【HDU】5088 Revenge of Nim II
题目分析:题意其实就是:取k个数,使得可以异或出0来。而我们如果将这k个数视为k行,每个数的每一位视为一列,那么这k个数边构成了一个01矩阵。那么能异或出0的充分条件是对这01矩阵高斯消元以后矩阵的秩小于矩阵的行数(也即存在一行全零,全零行就是异或出来的一行),那么我们只要对这个01矩阵高斯消元即可。如果不存在全零行则输出No,否则输出Yes。
PS:小优化,1e12比2^40略小,所以列数不会超过40,因为矩阵的秩不会超过min(行数,列数),所以当n>40时一定存在全零行,直接输出Yes即可,不然就高斯消元判断。
代码如下:
#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 equ , int var ) {
int r , c , tmp_r ;
for ( r = 0 , c = 0 ; r < equ && c < var ; ++ r , ++ c ) {
for ( tmp_r = r ; tmp_r < equ ; ++ tmp_r ) if ( a[tmp_r] & ( 1LL << c ) ) break ;
if ( tmp_r == equ ) {
-- r ;
continue ;
}
swap ( a[tmp_r] , a[r] ) ;
for ( int i = r + 1 ; i < equ ; ++ i ) if ( a[i] & ( 1LL << c ) ) a[i] ^= a[r] ;
}
return r < equ ;
}
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 ;
}