codeforces - 15C Industrial Nim(位运算+尼姆博弈)
2 seconds
64 megabytes
standard input
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».
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
这题是变形的尼姆博弈,知道什么是尼姆博弈的话,难点就在于位运算了。看到一篇博客的代码写的很详细。
附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 }
博客地址:http://blog.csdn.net/bigbigship/article/details/31031815
codeforces - 15C Industrial Nim(位运算+尼姆博弈)的更多相关文章
- Nim游戏(尼姆博弈)
这里是尼姆博弈的模板,前面的博弈问题的博客里也有,这里单列出来. 有N堆石子.A B两个人轮流拿,A先拿.每次只能从一堆中取若干个,可将一堆全取走,但不可不取,拿到最后1颗石子的人获胜.假设A B都非 ...
- codeforces 15C. Industrial Nim
题目链接:http://codeforces.com/problemset/problem/15/C $NIM$游戏是次要的,直接异或石头堆就可以了,问题在于给出的石头堆的数量极多. 考虑利用异或的性 ...
- Codeforces 15C Industrial Nim 简单的游戏
主题链接:点击打开链接 意甲冠军: 特定n 下列n行,每一行2的数量u v 表达v礧:u,u+1,u+2···u+v-1 问先手必胜还是后手必胜 思路: 首先依据Nim的博弈结论 把全部数都异或一下, ...
- 51Nod 1069:Nim游戏(尼姆博弈)
1069 Nim游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 有N堆石子.A B两个人轮流拿,A先拿.每次只能从一堆中取若干个,可将一堆全取走, ...
- Light OJ 1253 Misere Nim (尼姆博弈(2))
LightOJ1253 :Misere Nim 时间限制:1000MS 内存限制:32768KByte 64位IO格式:%lld & %llu 描述 Alice and Bob ar ...
- codeforces 812 E. Sagheer and Apple Tree(树+尼姆博弈)
题目链接:http://codeforces.com/contest/812/problem/E 题意:有一颗苹果树,这个苹果树所有叶子节点的深度要不全是奇数,要不全是偶数,并且包括根在内的所有节点上 ...
- POJ 2975 Nim 尼姆博弈
题目大意:尼姆博弈,求先手必胜的情况数 题目思路:判断 ans=(a[1]^a[2]--^a[n]),求ans^a[i] < a[i]的个数. #include<iostream> ...
- POJ2975 Nim 博弈论 尼姆博弈
http://poj.org/problem?id=2975 题目始终是ac的最大阻碍. 问只取一堆有多少方案可以使当前局面为先手必败. 显然由尼姆博弈的性质可以知道需要取石子使所有堆石子数异或和为0 ...
- 尼姆博弈(Nimm's Game)
题型: 有3堆各若干个物品,两个人轮流从某一堆取任意多的物品,规定每次至少取1个,多者不限,最后取光者得胜. 思路 首先自己想一下,就会发现只要最后剩两堆物品一样多(不为零),第三堆为零,那面对这种局 ...
随机推荐
- Hadoop2.7.7阿里云安装部署
阿里云的网络环境不需要我们配置,如果是在自己电脑上的虚拟机,虚拟机的安装步骤可以百度.这里是单机版的安装(也有集群模式的介绍)使用Xshell连接阿里云主机,用命令将自己下载好的安装包上传到服务器 # ...
- unstable sort
$sort (aggregation) - MongoDB Manual https://docs.mongodb.com/manual/reference/operator/aggregation/ ...
- macro-name replacement-text 宏 调试开关可以使用一个宏来实现 do { } while(0)
C++ 预处理器_w3cschool https://www.w3cschool.cn/cpp/cpp-preprocessor.html C++ 预处理器 预处理器是一些指令,指示编译器在实际编译之 ...
- Share Memory By Communicating 一等公民
Share Memory By Communicating - The Go Programming Language https://golang.google.cn/doc/codewalk/sh ...
- Communicating sequential processes CSP 通信顺序进程 CSP writing to a file by name (process, Erlang) vs. writing to a file descriptor (channel, Go)
the-way-to-go_ZH_CN/01.2.md at master · Unknwon/the-way-to-go_ZH_CN https://github.com/Unknwon/the-w ...
- 慕课网金职位 Python工程师 百度网盘下载
百度网盘链接:https://pan.baidu.com/s/1xshLRO3ru0LAsQQ0pE67Qg 提取码:bh9f 如果失效加我微信:610060008[视频不加密,资料代码齐全,超清一手 ...
- SpringMVC传递JSON数据
文章目录 一.前后端传递和接收JSON数据 1:是要Ajax默认格式来传递数据(*) 2:使用application/json格式来传递数据 二.spring-web.xml中需要如下配置 一.前后端 ...
- 网页开发(HTML 基础)
网页的标准是W3C,目前模式是HTML.CSS和JavaScript. HTML,全称"Hyper Text Markup Language(超文本标记语言)",简单来说,网页就是 ...
- java日期
// 完整显示日期时间 String str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(new Date ...
- 1. Centos 7重置root密码
1.开机启动系统,不断按"↑"和"↓",在如下引导界面按"e",编辑引导项 2.按"↓"找到下图显示的代码 删除代码最后 ...