zoukankan      html  css  js  c++  java
  • 【洛谷】P1247取火柴游戏

    题目链接: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

    代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 using namespace std;
     6 #define ll long long
     7 const int maxn = 500005;
     8 
     9 int n,m,x;
    10 int a[maxn];
    11 
    12 void nim(){
    13     cin>>n;
    14     int res = 0;
    15     for(int i = 1; i<= n; i++){
    16         cin>>a[i];
    17         res ^= a[i];
    18     }
    19     if(res == 0){
    20         cout<<"lose"<<endl;
    21     }
    22     else{
    23         //cout<<res<<endl;
    24         for(int i = 1; i <= n; i++){
    25             if((a[i]^res) < a[i]){
    26                 cout<<(a[i] - (a[i]^res))<<" "<<i<<endl;
    27                 a[i] ^= res;
    28                 break;
    29             }
    30         }
    31         for(int i = 1; i <= n; i++){
    32             cout<<a[i]<<" ";
    33         }
    34         cout<<endl;
    35 
    36     }
    37 }
    38 
    39 
    40 int main(){
    41     nim();
    42     return 0;
    43 }
  • 相关阅读:
    SysRq组合键是一组"魔术组合键"
    iptables FTP脚本
    (转)SSH的端口转发
    进制转换
    指定的参数已超出有效值的范围
    静态类
    接口
    快捷键,移到最上层
    gitbook
    nginx服务无法停止(Windows)
  • 原文地址:https://www.cnblogs.com/Asumi/p/9759712.html
Copyright © 2011-2022 走看看