题意:有n堆石子,每人每次可以从一堆中拿走任意多个,两人轮流操作,谁无子可取谁输。输入n堆石子各自的数量,输出先手是否能赢。
分析:NP问题,必胜态N(next player wins),必败态P(previous player wins)
如果某状态的直接后继中有必败态那么它一定是必胜态,否则为必败态。
SG函数。设函数g(x)。我们先把所有的最终局面(最终局面均为必败P局面)g(x)赋值为0。然后所有其他局面g(x)等于其直接后继状态中没有出现过的最小自然数。这样一来所有是g(x)=0的状态就是必败态,其他为必胜态。
根据定理:有这样一个游戏,是多个游戏共同进行,每个游戏都执行到底时才算整个游戏结束,每次一个选手可以把一个游戏进行一步。对于这样的游戏它的某状态的g(x)值,为每个子游戏的现在所处的状态的g(x)值抑或起来的结果。
所以对于本题,我们只需要研究一堆石子的g(x)的规律即可得出若干堆石子共同进行的胜败。
对于一个有n个石子的堆,其开始状态(有n个石子,没有被取过)g(x)=n;
View Code
#include
<
iostream
>
#include < cstdio >
#include < cstdlib >
#include < cstring >
using namespace std;
int main()
{
// freopen("t.txt", "r", stdin);
int n;
while (scanf( " %d " , & n) != EOF)
{
int ans = 0 ;
for ( int i = 0 ; i < n; i ++ )
{
int a;
scanf( " %d " , & a);
ans = ans ^ a;
}
if (ans)
printf( " Yes\n " );
else
printf( " No\n " );
}
return 0 ;
}
#include < cstdio >
#include < cstdlib >
#include < cstring >
using namespace std;
int main()
{
// freopen("t.txt", "r", stdin);
int n;
while (scanf( " %d " , & n) != EOF)
{
int ans = 0 ;
for ( int i = 0 ; i < n; i ++ )
{
int a;
scanf( " %d " , & a);
ans = ans ^ a;
}
if (ans)
printf( " Yes\n " );
else
printf( " No\n " );
}
return 0 ;
}