题解:AT_arc168_b [ARC168B] Arbitrary Nim
ds_SegmentTree · · 题解
前言
这道题的基础是 洛谷 P2197 ,建议做完这道题再看题解。
思路
首先可以想到二分答案,可惜这道题的
其次,我们知道,两个相同的数对同一个数取模,结果一样。(不会有人不知道这个吧……)
我们又知道,一个数异或上他本身,结果是
否则,我们将剩下的数存下来,取最大值为
神奇小代码
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+10;
int n,f;
set<int> st;
signed main(){
cin>>n;
for(int i=1;i<=n;i++){
int a;
cin>>a;
f^=a;
if(st.find(a)==st.end()) st.insert(a);//无则插入
else st.erase(a);//有则抵消
}
if(f>0) cout<<"-1";//k无穷大
else if(st.size()==0) cout<<"0";//先手必败
else cout<<(*(--st.end()))-1;//剩余a[i]中最大的-1
return 0;
}