尼姆博弈基本思想:
两人从n堆物品中取任意个,先取完者胜。
即将n堆物品的数量异或,得到的值如果为0,则先手败,反之先手胜。
如果要求先手在胜的条件下,得到奇异局势的方法数,则判断异或的值与每一堆原值异或后(结果应该表示该堆没有参加异或时的异或值)与原值比较大小,
如果小于,则方法数加一。且对应的方法后,该堆的数目应变为异或的值与每一堆原值异或的值。
Problem Description
m堆石子,两人轮流取.只能在1堆中取.取完者胜.先取者负输出No.先取者胜输出Yes,然后输出怎样取子.例如5堆 5,7,8,9,10先取者胜,先取者第1次取时可以从有8个的那一堆取走7个剩下1个,也可以从有9个的中那一堆取走9个剩下0个,也可以从有10个的中那一堆取走7个剩下3个.
Input
输入有多组.每组第1行是m,m<=200000. 后面m个非零正整数.m=0退出.
Output
先取者负输出No.先取者胜输出Yes,然后输出先取者第1次取子的所有方法.如果从有a个石子的堆中取若干个后剩下b个后会胜就输出a b.参看Sample Output.
Sample Input
2 45 45 3 3 6 9 5 5 7 8 9 10 0
Sample Output
No Yes 9 5 Yes 8 1 9 0 10 3
#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
using namespace std;
int min(int a, int b)
{
if(a<=b)
return a;
return b;
}
int max(int a, int b)
{
if(a>=b)
return a;
return b;
}
double min(double a, double b)
{
if(a<=b)
return a;
return b;
}
double max(double a, double b)
{
if(a>=b)
return a;
return b;
}
int main()
{
int n;
int s[200005];
while(cin>>n)
{
if(n==0)
break;
int ans = 0;
for(int i =0 ;i < n; ++i)
{
cin >> s[i];
ans^=s[i];
}
if(!ans)
printf("No\n");
else
{
printf("Yes\n");
for(int i = 0; i < n;++i)
{
int k = ans ^ s[i];
if( k< s[i])
printf("%d %d\n", s[i], k);
}
}
}
return 0;
}
当然有的题目只是要求输出可以行走的步数就可以了,这样在后面亦或的时候我们就不需要输出了,直接cnt++就可以了,比如HDOJ1850
努力努力.....