取(m堆)石子游戲(尼姆博弈)

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

解題思路:

基本的尼姆博弈類型題。相關內容http://blog.csdn.net/userluoxuan/article/details/38336287

AC代碼:

#include <iostream>
#include <cstdio>
using namespace std;
int a[200005];
int main()
{
    int m, ans;
    while(scanf("%d", &m) && m)
    {
        ans = 0;
        for(int i = 0; i < m; i++)
        {
            scanf("%d", &a[i]);
            ans ^= a[i];
        }
        if(ans)
        {
            printf("Yes\n");
            for(int i = 0; i < m; i++)
            {
                int k = ans ^ a[i];
                if(k < a[i])
                    printf("%d %d\n", a[i], k);
            }
        }
        else
            printf("No\n");
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章