标签:博弈论
Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 730 Accepted Submission(s): 288
Problem Description
Mr.Frost is a child who is too simple, sometimes naive, always plays some simple but interesting games with his friends. Today,he invents a new game again:
At the beginning of the game they pick N (1<=N<=100) piles of stones, Mr.Frost and his friend move the stones in turn. At each step of the game, the player chooses a pile, removes at least one stone from the pile, the first player can’t make a move, and loses.
So smart is the friends of Mr.Frost that Mr.Frost always loses. Having been a loser for too many times, he wants to play a trick. His plan is to remove some piles, and then he can find a way to make sure that he would be the winner after his friends remove
stones first.
Now, he wants to know how many ways to remove piles which are able to achieve his purpose. If it’s impossible to find any way, please print “-1”.
Input
The first line contains a single integer t (1<=t<=20), that indicates the number of test cases. Then follow the t cases. Each test case begins with a line contains an integer N (1 <= N <= 100), representing the number of the piles. The next n lines, each of
which has a positive integer Ai(1<=Ai<=2^31 - 1) represent the number of stones in this pile.
Output
For each case, output a line contains the number of the way mod 1000007, If it’s impossible to find any way, please print “-1”.
Sample Input
Sample Output
Source
题解及代码:
这道题目是和博弈论挂钩的高斯消元。本题涉及的博弈是nim博弈,结论是:当先手处于奇异局势时(几堆石子数相互异或为0),其必败。
那么我们就可以把题目转化成求n堆石子中的k堆石子数异或为0的情况数。使用x1---xn表示最终第i堆石子到底取不取(1取,0不取),将每堆石子数画成2进制的形式,列成31个方程来求自由变元数,最后由于自由变元能取1、0两种状态,所以,最终答案是2^k。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int a[33][110];
int equ,var;
int Gauss()
{
int k,col,max_r;
for(k=0,col=0; k<equ&&col<var; k++,col++)
{
if(a[k][col]==0)
{
max_r=k;
for(int i=k+1; i<equ; i++)
if(a[i][col]>a[max_r][col])
{
max_r=i;break;
}
if(max_r!=k)
for(int i=0; i<=var; i++)
{
swap(a[max_r][i],a[k][i]);
}
}
if(a[k][col]==0)
{
k--;
continue;
}
for(int i=k+1; i<equ; i++)
{
if(a[i][col])
for(int j=col; j<=var; j++)
{
a[i][j]=a[i][j]^a[k][j];
}
}
}
return k;
}
int main()
{
equ=31;
int cas,n,X,mod=1000007;
scanf("%d",&cas);
while(cas--)
{
memset(a,0,sizeof(a));
scanf("%d",&n);
var=n;
for(int i=0;i<n;i++)
{
scanf("%d",&X);
int t=0;
while(X)
{
a[t++][i]=X&1;
X>>=1;
}
}
int k=Gauss(),ans=1;
k=n-k;
for(int i=0;i<k;i++)
{
ans=(ans*2)%mod;
}
printf("%d\n",ans);
}
return 0;
}
hdu 3915 Game
标签:博弈论
原文地址:http://blog.csdn.net/knight_kaka/article/details/39029913