Description
ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)
栏目:公司新闻 发布时间:2024-07-16
 #include <iostream>  #include <cstdio>  #include <cstdlib>  #include <cmath>  #include <cstring>  #include <algorithm>  #include <set>  #include <map>

  #include <iostream>

  #include <cstdio>

  #include <cstdlib>

  #include <cmath>

  #include <cstring>

  #include <algorithm>

  #include <set>

  #include <map>

  #include <queue>

  #include <string>

  #define LL long long

  using namespace std;

  const int maxN = 105;

  const int MOD = 1000007;

  int n, s[maxN];

  void input()

  {

  scanf("%d", &n);

  for (int i = 0; i < n; ++i)

  scanf("%d", &s[i]);

  }

  //xor高斯消元求线性基

  //时间复杂度O(30n)

  int xorGauss(int n)

  {

  int row = 0;

  for (int i = 30; i >= 0; i--)

  {

  int j;

  for (j = row; j < n; j++)

  if(s[j]&(1<<i))

  break;

  if (j != n)

  {

  swap(s[row], s[j]);

  for (j = 0; j < n; j++)

  {

  if(j == row) continue;

  if(s[j]&(1<<i))

  s[j] ^= s[row];

  }

  row++;

  }

  }

  return row;

  }

  void work()

  {

  int row, ans, k;

  row = xorGauss(n);

  ans = n-row;

  if (ans != -1)

  {

  k = 1;

  while (ans)

  {

  k <<= 1;

  k %= MOD;

  ans--;

  }

  ans = k;

  }

  else

  ans = -1;

  printf("%d

  ", ans);

  }

  int main()

  {

  //freopen("test.in", "r", stdin);

  int T;

  scanf("%d", &T);

  for (int times = 0; times < T; ++times)

  {

  input();

  work();

  }

  return 0;

  }