【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\n",a[i],temp);
}
}
return 0;
}
最新文章
- hibernate三种状态
- C#写爬虫,版本V2.1
- jquery+ajax实现分页
- java中多线程模拟(多生产,多消费,Lock实现同步锁,替代synchronized同步代码块)
- 知道创宇研发技能表v3.1
- C#正则表达式分组使用
- LruCache--远程图片获取与本地缓存
- LoadRunner界面分析(二)
- 12096 - The SetStack Computer UVA
- JQuery事件处理的注意事项
- java构造方法的不同
- js获取地址栏url以及获取url参数
- HTTP各状态消息说明
- tr069开源协议EasyCwmp移植
- 201521123051 《Java程序设计》第4周学习总结
- 客户端 未登录页面 (clearfix 分割线)
- Linux内核源码分析 day01——内存寻址
- Exp6 信息搜集与漏洞扫描 20164313 杜桂鑫
- 解决python logging重复写日志问题
- Nginx+IIS简单的部署