http://acm.hdu.edu.cn/showproblem.php?pid=5088
求能否去掉几堆石子使得nim游戏胜利
我们可以把题目转化成求n堆石子中的k堆石子数异或为0的情况数。使用x1---xn表示最终第i堆石子到底取不取(1取,0不取),将每堆石子数画成2进制的形式,列成31个方程来求自由变元数,最后由于自由变元能取1、0两种状态,所以自由变元数多于0即可输出Yes。
注意有40+个方程,因为A[I]<=1e12....
#include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <string> #include <queue> #include <map> #include <iostream> #include <algorithm> using namespace std; #define RD(x) scanf("%d",&x) #define RD2(x,y) scanf("%d%d",&x,&y) #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z) #define clr0(x) memset(x,0,sizeof(x)) #define clr1(x) memset(x,-1,sizeof(x)) #define eps 1e-9 const double pi = acos(-1.0); typedef long long LL; typedef unsigned long long ULL; const int modo = 1e9 + 7; const int INF = 0x3f3f3f3f; const int inf = 0x3fffffff; const LL _inf = 1e18; const int maxn = 1005,maxm = 10005; #define MAXN 1100 #define MOD 1000007 #define weishu 42 LL a[MAXN], g[MAXN][MAXN]; int Gauss(int n) { int i, j, r, c, cnt; for (c = cnt = 0; c < n; c++) { for (r = cnt; r < weishu; r++) { if (g[r][c]) break; } if (r < weishu) { if (r != cnt) { for (i = 0; i < n; i++) swap(g[r][i], g[cnt][i]); } for (i = cnt + 1; i < weishu; i++) { if (g[i][c]) { for (j = 0; j < n; j++) g[i][j] ^= g[cnt][j]; } } cnt++; } } return n - cnt; } int main() { int c; int n, i, j; int ans, vary; scanf("%d", &c); while (c--) { int fuck = 0; scanf("%d", &n); for (i = 0; i < n; i++){ scanf("%I64d", &a[i]); fuck ^= a[i]; } for (i = 0; i < weishu; i++) { for (j = 0; j < n; j++) g[i][j] = (a[j] >> i) & 1; } vary = Gauss(n); //printf("%d\n", vary); if(vary <= 0 )//|| (fuck == 0 && vary <= 1)) puts("No"); else puts("Yes"); } return 0; }