hdu 1730 Nim博弈

时间:2023-01-06 03:13:40

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1730

Nim博弈为:n堆石子,每个人可以在任意一堆中取任意数量的石子

n个数异或值为0就后手赢,否则先手赢

将这题转化成Nim游戏

可以在任意一行中移动任意距离,可以向左或右,但是仔细观察发现,其实只能接近对方棋子,如果你远离对方棋子,对方可以接近你相同距离

和nim相似的是,不能不移,所以两个棋子的距离差就是SG值

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,m;
while(cin>>n>>m)
{
int ans=0;
for(int i=1;i<=n;i++)
{
int a,b;
scanf("%d %d",&a,&b);
ans^=int(abs(a-b)-1);
}
if(ans==0)cout<<"BAD LUCK!"<<endl;
else cout<<"I WIN!"<<endl;
}
return 0;
}