codeforces - 15C Industrial Nim(位运算+尼姆博弈)

本文解析了一道关于工业背景下的尼姆博弈问题。玩家轮流从不同容量的石堆中取石子,先无法取者输。文章通过位运算技巧提供了一个高效的解决方案,并附带AC代码及详细解释。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Industrial Nim
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

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 has xi + 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».

Input

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

Output «tolik» if the oligarch who takes a stone first wins, and «bolik» otherwise.

Examples
Input
Copy
2
2 1
3 2
Output
tolik
Input
Copy
4
1 1
1 1
1 1
1 1
Output
bolik

这题是变形的尼姆博弈,知道什么是尼姆博弈的话,难点就在于位运算了。看到一篇博客的代码写的很详细。

附ac代码(有注释):
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 
 5 typedef long long LL;
 6 LL get(LL x,LL m) //n^(n+1)=1(n为偶数)  1^1=0  0^0=0
 7 {
 8     LL ans;
 9     if(m&1){
10         if(x&1)
11             ans=x;//后面的可以配成对
12         else
13             ans=x+m-1;//除去最后一项前面的可以配成对
14         m--;
15     }
16     else{
17         if(x&1){
18             ans=x^(x+m-1);//中间的可以配成对
19             m-=2;
20         }
21         else
22             ans=0;
23     }
24     if(m%4)//判断是否为偶数对
25        return ans^1;
26     else
27         return ans;
28 }
29 int main()
30 {
31     int n;
32     long long a,b;
33     while(~scanf("%d",&n)){
34         long long  ans=0;
35         for(int i=0;i<n;i++){
36             scanf("%lld%lld",&a,&b);
37             ans^=get(a,b);
38         }
39         if(ans)
40             puts("tolik");
41         else
42             puts("bolik");
43     }
44     return 0;
45 }
View Code

 

博客地址:http://blog.youkuaiyun.com/bigbigship/article/details/31031815

转载于:https://www.cnblogs.com/zmin/p/8490701.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值