1. 程式人生 > >A - 取(m堆)石子遊戲

A - 取(m堆)石子遊戲

ace 博弈 for iostream fin num n) stdio.h printf

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

A - 取(m堆)石子遊戲