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);
    }
}

猜你喜欢

转载自www.cnblogs.com/wzl19981116/p/9365576.html
今日推荐