There are n stone quarries in Petrograd.
Each quarry owns mi dumpers (1 ≤ i ≤ n). It is known that the first dumper of the i-th quarry has xi stones in it, the second dumper hasxi + 1 stones in it, the third has xi + 2, and the mi-th dumper (the last for the i-th quarry) has xi + mi - 1 stones in it.
Two oligarchs play a well-known game Nim. Players take turns removing stones from dumpers. On each turn, a player can select any dumper and remove any non-zero amount of stones from it. The player who cannot take a stone loses.
Your task is to find out which oligarch will win, provided that both of them play optimally. The oligarchs asked you not to reveal their names. So, let's call the one who takes the first stone «tolik» and the other one «bolik».
The first line of the input contains one integer number n (1 ≤ n ≤ 105) — the amount of quarries. Then there follow n lines, each of them contains two space-separated integers xi and mi (1 ≤ xi, mi ≤ 1016) — the amount of stones in the first dumper of the i-th quarry and the number of dumpers at the i-th quarry.
Output «tolik» if the oligarch who takes a stone first wins, and «bolik» otherwise.
2 2 1 3 2
tolik
4 1 1 1 1 1 1 1 1
bolik
题意:以前从来没尝试写过博弈,我的处女博弈题啊!T.T比赛的时候没出来......NIM博弈记得只要a1^a2^.....^an=0就可以了,但是这题会卡TLE,太伤了。想了一下,想起之前搞最大流是用过的^运算符,那时是处理两条相反边的,其实就4^1=5,5^1=4,其实就是对于一个偶数n,则n^(n+1)=1,....可是脑子太混乱,没调试出来....
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
__int64 x,m,ans,i,j,n;
while(scanf("%I64d",&n)!=EOF)
{
ans=0;
for(i=0;i<n;i++)
{
scanf("%I64d%I64d",&x,&m);
if(x%2)
{
if(((m-1)/2)%2)
{if((m-1)%2)
ans^=(x^1^(x+m-1));
else ans^=(x^1);
}
else
{if((m-1)%2)
ans^=(x^0^(x+m-1));
else ans^=(x^0);
}
}
else
{
if((m/2)%2)
{
if(m%2)
ans^=((x+m-1)^1);
else ans^=1;
}
else
{
if(m%2)
ans^=((x-1+m)^0);
else ans^=0;
}
}
}
if(ans==0) puts("bolik");
else puts("tolik");
}
return 0;
}
Nim博弈策略解析

本文解析了一道关于Nim博弈的编程题目,介绍了如何通过计算每个采石场中特定数量石头的异或值来确定哪位玩家将赢得游戏。文章详细阐述了根据不同条件下石头数量的变化规律,使用异或运算来实现最优策略。
381

被折叠的 条评论
为什么被折叠?



