zoukankan      html  css  js  c++  java
  • HDU 2176:取(m堆)石子游戏(Nim博弈)

    取(m堆)石子游戏

    Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4610 Accepted Submission(s): 2775

    Problem 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
    

    思路

    可以看出这是典型的Nim博弈
    对于先手必败的情况,直接输出No就行了
    对于先手必胜,我们需要让先手第一次取走后剩下的石子保证奇异局势为0(即后手拿的时候必败)的状态。
    输出的时候,让原本的奇异局势与当前堆的石子数异或,如果异或结果小于等于当前石子数,输出即可

    AC代码

    /*
    * @Author: WZY
    * @School: HPU
    * @Date:   2019-01-03 16:54:58
    * @Last Modified by:   WZY
    * @Last Modified time: 2019-01-03 17:21:29
    */
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <math.h>
    #include <limits.h>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <set>
    #include <string>
    #include <time.h>
    #define ll long long
    #define ull unsigned long long
    #define ms(a,b) memset(a,b,sizeof(a))
    #define pi acos(-1.0)
    #define INF 0x7f7f7f7f
    #define lson o<<1
    #define rson o<<1|1
    #define bug cout<<"---------"<<endl
    #define debug(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<"
    "
    const double E=exp(1);
    const int maxn=1e6+10;
    const int mod=1e9+7;
    using namespace std;
    int a[maxn];
    int main(int argc, char const *argv[])
    {
    	ios::sync_with_stdio(false);
    	#ifndef ONLINE_JUDGE
    	    freopen("in.txt", "r", stdin);
    	    freopen("out.txt", "w", stdout);
    	    double _begin_time = clock();
    	#endif
    	int n;
    	while(cin>>n&&n)
    	{
    		int ans=0;
    		for(int i=0;i<n;i++)
    		{
    			cin>>a[i];
    			ans^=a[i];
    		}
    		if(!ans)
    			cout<<"No"<<endl;
    		else
    		{
    			cout<<"Yes"<<endl;
    			for(int i=0;i<n;i++)
    			{
    				if((ans^a[i])<=a[i])
    					cout<<a[i]<<" "<<(ans^a[i])<<endl;
    			}
    		}
    	}
    	#ifndef ONLINE_JUDGE
    	    double _end_time = clock();
    	    printf("time = %lf ms.", _end_time - _begin_time);
    	#endif
    	return 0;
    }
    
  • 相关阅读:
    php遍历目录下的所有文件夹
    PHP 遍历文件
    PHP中public protected private的区别
    mysql数据库优化的方法
    Thinkphp 验证器
    PHP 接口
    php获取表单的值
    PHP 数组
    php 递归
    [go系列] 函数
  • 原文地址:https://www.cnblogs.com/Friends-A/p/10324311.html
Copyright © 2011-2022 走看看