博弈——A Simple Game(巴什博弈、尼姆博弈)
题目链接:
HDU - 1851
Agrael likes play a simple game with his friend Animal during the classes. In this Game there are n piles of stones numbered from 1 to n, the 1st pile has M1 stones, the 2nd pile has M2 stones, … and the n-th pile contain Mn stones. Agrael and Animal take turns to move and in each move each of the players can take at most L1 stones from the 1st pile or take at most L2 stones from the 2nd pile or … or take Ln stones from the n-th pile. The player who takes the last stone wins.
After Agrael and Animal have played the game for months, the teacher finally got angry and decided to punish them. But when he knows the rule of the game, he is so interested in this game that he asks Agrael to play the game with him and if Agrael wins, he won’t be punished, can Agrael win the game if the teacher and Agrael both take the best move in their turn?
The teacher always moves first(-_-), and in each turn a player must takes at least 1 stones and they can’t take stones from more than one piles.
Input
The first line contains the number of test cases. Each test cases begin with the number n (n ≤ 10), represent there are n piles. Then there are n lines follows, the i-th line contains two numbers Mi and Li (20 ≥ Mi > 0, 20 ≥ Li > 0).
Output
Your program output one line per case, if Agrael can win the game print “Yes”, else print “No”.
Sample Input
2
1
5 4
2
1 1
2 2
Sample Output
Yes
No
题目大意
有 n 堆石子,两人轮流从任意一堆中取,每堆每次最多能取的石子数也有限制,取走最后一颗石子的人获胜。
分析
这题是巴什博奕和尼姆博弈的综合。
设 Si = Mi % (Li + 1)
可以得到 ans = S1 xor S2 xor … xor Sn
所以当 ans = 0 时,先手必败,而当 ans > 0 时,先手则有办法让 ans = 0,此时,先手必胜。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,n;
cin>>t;
while(t--){
cin>>n;
int ans=0,m,l;
for(int i=0;i<n;i++){
cin>>m>>l;
ans^=m%(l+1);
}
if(ans){
cout<<"No"<<endl;
}
else{
cout<<"Yes"<<endl;
}
}
return 0;
}