A - 取(m堆)石子游戏

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

就是nim博弈;比异或最高位大就可以改

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define pb push_back
#define mm(a,b) memset((a),(b),sizeof(a))
#include<vector>
typedef long long ll;
typedef long double ld;
const ll mod=1e9+7;
using namespace std;
const double pi=acos(-1.0);
int a[200000];
void chuli(int n)
{
	int x=a[0];
	for(int i=1;i<n;i++)
	{
		x^=a[i];
	}
	if(x==0)
	{
		pf("No\n");
	}else
	{
		pf("Yes\n");
		int num=0,k=x;
		while(x!=1)
		{
			num++;
			x>>=1;
		}
		for(int i=0;i<n;i++)
		{
			if(a[i]>=(1<<num))
			{
				x=k;
				pf("%d %d\n",a[i],a[i]^k);
			}
		}
	}
}
int main()
{
	int n;
	while(1)
	{
		sf("%d",&n);
		if(n==0) return 0;
		mm(a,0);
		for(int i=0;i<n;i++)
		sf("%d",&a[i]);
		chuli(n);
	}
}

更新有bug,虽然这个oj能过,测试点太水了,比如5 8 8 的时候,异或是1001,但是8不能和1001异或,会变成13,的,所以不能根据if(a[i]>=(1<<num))来判断能不能,要根据a[i]>=(a[i]^x)来判断

		pf("Yes\n");
		
		for(int i=0;i<n;i++)
		{
			if(a[i]>=(a[i]^x))
			{
				pf("%d %d\n",a[i],a[i]^k);
			}
		}

posted @ 2018-07-25 13:56  一无所知小白龙  阅读(199)  评论(0编辑  收藏  举报