【洛谷】P1247取火柴游戏
2024-08-28 19:59:00
题目链接:https://www.luogu.org/problemnew/show/P1247
题意:nim取石子的题意,多了一个判断先手赢的话,输出先手第一把怎么拿,以及拿完之后每堆还剩多少。
题解:异或和为0直接lose。不为0的话,看res xor a[i]的值如果小于a[i]说明可以取该堆,也就是取a[i] - (res xor a[i])这么多。剩余的就是res xor a[i]。
证明。。洛谷题解的大佬写的挺好的。。QAQ
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define ll long long
const int maxn = ; int n,m,x;
int a[maxn]; void nim(){
cin>>n;
int res = ;
for(int i = ; i<= n; i++){
cin>>a[i];
res ^= a[i];
}
if(res == ){
cout<<"lose"<<endl;
}
else{
//cout<<res<<endl;
for(int i = ; i <= n; i++){
if((a[i]^res) < a[i]){
cout<<(a[i] - (a[i]^res))<<" "<<i<<endl;
a[i] ^= res;
break;
}
}
for(int i = ; i <= n; i++){
cout<<a[i]<<" ";
}
cout<<endl; }
} int main(){
nim();
return ;
}
最新文章
- Qml 写的弹出层控件
- Mysql:Forcing close of thread xxx user: &#39;root&#39; 的解决方法
- checkbox属性checked=";checked";通过js已设置,但是不勾选
- oracle日期操作
- git Please move or remove them before you can merge. 错误解决方案
- phpcms list页实现分页
- Linux kernel ‘aac_send_raw_srb’函数输入验证漏洞
- Ubuntu14.04+CUDA6.5环境下神经网络工具包Deepnet配置
- 最近点对问题 HDU Quoit Design 1007 分治法
- Ionic3新特性--页面懒加载2加载其他组件
- 笔试常考--浏览器兼容性问题及解决方案(CSS)
- spring boot+spring data jpa+gradle+mysql配置问题
- web应用与http协议
- 第二章 C#语法基础(2.1 C#语言的数据类型一)
- git push 和 pull 时 免密执行的方法
- Centos7 安装redis集群哨兵模式
- 【BZOJ2000】[HNOI2000]取石头游戏(贪心,博弈论)
- RAC 11.2的新特性
- java.lang.AbstractMethodError: com.microsoft.jdbc.base.BaseDatabaseMetaData.supportsGetGeneratedKeys()Z
- CSS之position体验