题解:AT_arc168_b [ARC168B] Arbitrary Nim
jinglinbankemeng · · 题解
前言:
竟然有如此水的绿题。
思路:
这是一道非常经典的 Nim 游戏问题。
首先我们可以先看
假如先手赢不了的话,那么我们就要考虑加限制条件。所以我们可以先将出现两次的数给消掉,因为这样就可以通过模仿对方的行为来进行消除这两堆。
进行完消除以后(以下都在进行消除后的基础之上),我们设最大的一堆为
由于先手赢不了时异或和为
当然还有以个特别的条件,当
AC Code:
#include <bits/stdc++.h>
using namespace std;
set<int>s;
long long n,sum=0;
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
std::ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin >> n;
for(long long i=1,x;i<=n;i++)
{
cin >> x;
sum^=x;
if(s.find(x)!=s.end())
{
s.erase(x);
}
else
{
s.insert(x);
}
}
if(sum)
{
cout << "-1" << endl;
}
else if(s.empty())
{
cout << "0" << endl;
}
else
{
cout << *--s.end()-1 << endl;
}
return 0;
}