本文共 909 字,大约阅读时间需要 3 分钟。
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博弈的模板题,想要证明实在是太麻烦了,其实只要记住结论和推论就可以做出来了,如果每堆石子的个数异或之后得到一个数k,如果k是0则先手必败,然后这道题目要求输出获胜策略,就需要记住一个推论,如果每堆石子的数量a异或这个k之后得到的石子数量p小于原先这堆石子的数量,即p<a,则获胜策略就是这一堆取出a-p个石子
下面附上ac代码
#include#include #include #include #include #include #include using namespace std;typedef long long ll;ll a[2000010];int main(){ std::ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); ll m; while(cin>>m&&m!=0) { ll k=0; for(ll i=1;i<=m;i++) { cin>>a[i]; k^=a[i]; } if(!k) cout<<"No"<
转载地址:http://kzlo.baihongyu.com/