【hdu 2176】取(m堆)石子游戏

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

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

【题目链接】:http://acm.hdu.edu.cn/showproblem.php?pid=2176

【题解】

先明确,先手胜的话肯定是m堆石子的各个堆石子数异或值不为0;
则枚举第i堆石子,石头数目要减少;
如果其他m-1堆石子的全部异或小于第i堆石子的数目;
则可以把第i堆石子的数目降低到和其他m-1堆石子的全部异或相同的数量;
这样留给对手的全部石子的异或就为0了;对手变成先手了;则对手必败;

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int MAXM = 2e5+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

int m;
LL a[MAXM];

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    while (~scanf("%d",&m))
    {
        if (m==0)
            break;
        LL tot = 0;
        rep1(i,1,m)
        {
            rel(a[i]);
            tot = tot ^ a[i];
        }
        if (tot!=0)
            puts("Yes");
        else
            puts("No");
        rep1(i,1,m)
        {
            LL temp = tot ^ a[i];
            if (a[i]>temp)
                printf("%I64d %I64d
",a[i],temp);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626729.html