题目链接:
题意:nim取石子的题意,多了一个判断先手赢的话,输出先手第一把怎么拿,以及拿完之后每堆还剩多少。
题解:异或和为0直接lose。不为0的话,看res xor a[i]的值如果小于a[i]说明可以取该堆,也就是取a[i] - (res xor a[i])这么多。剩余的就是res xor a[i]。
证明。。洛谷题解的大佬写的挺好的。。QAQ
代码:
1 #include2 #include 3 #include 4 #include 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"<