hdu2176取(m堆)石子游戏 组合游戏初步——NIM

来源:互联网 发布:淘宝怎么修改折扣价 编辑:程序博客网 时间:2024/06/06 09:47

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

245 4533 6 955 7 8 9 100
 

Sample Output

NoYes9 5Yes8 19 010 3
 

记住结论吧

#include <iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn = 200000+100;int a[maxn];int main(){    int m;    while(scanf("%d",&m)!=EOF){        if(m==0) break;        int t=0;        int sum=0;        memset(a,0,sizeof(a));        for(int i=0;i<m;i++){            scanf("%d",&a[i]);            t=(t^a[i]);        }        if(t==0){            printf("No\n");        }        else {            printf("Yes\n");            for(int i=0;i<m;i++){                if((t^a[i])<a[i]){                    sum++;                    printf("%d %d\n",a[i],(t^a[i]));                }            }        }    }    return 0;}


0 0
原创粉丝点击