更好的阅读体验

D. Yet Another Number Game

开启传送门

题目描述

Since most contestants do not read this part, I have to repeat that Bitlandians are quite weird. They have their own jobs, their own working method, their own lives, their own sausages and their own games!

Since you are so curious about Bitland, I’ll give you the chance of peeking at one of these games.

BitLGM and BitAryo are playing yet another of their crazy-looking genius-needed Bitlandish games. They’ve got a sequence of nnn non-negative integers a1,a2,...,ana_1, a_2, ..., a_na1​, a2​, ..., an​. The players make moves in turns. BitLGM moves first. Each player can and must do one of the two following actions in his turn:

  • Take one of the integers (we’ll denote it as aia_iai​). Choose integer xxx (1≤x≤ai1 \le x \le a_i1 ≤ x ≤ ai​). And then decrease aia_iai​ by xxx, that is, apply assignment: aia_iai​ = aia_iai​ - xxx.
  • Choose integer xxx (1≤x≤mini=1nai1\le x \le min_{i=1}^n a_i1≤x≤mini=1n​ai​). And then decrease all aia_iai​ by xxx, that is, apply assignment: ai=ai−xa_i = a_i - xai​ = ai​ − x, for all iii.

The player who cannot make a move loses.

You’re given the initial sequence a1,a2,…,ana_1, a_2, \dots, a_na1​, a2​, …, an​. Determine who wins, if both players plays optimally well and if BitLGM and BitAryo start playing the described game in this sequence.

Input

The first line contains an integer nnn (1≤n≤31 \le n \le 31 ≤ n ≤ 3).

The next line contains nnn integers a1,a2,…,ana_1, a_2, \dots, a_na1​, a2​, …, an​ (0≤ai<3000 \le a_i \lt 3000 ≤ ai​ < 300).

Output

Write the name of the winner (provided that both players play optimally well). Either “BitLGM” or “BitAryo” (without the quotes).

Example

input1

2
1 1

output1

BitLGM

input2

2
1 2

output2

BitAryo

input3

3
1 2 1

output3

BitLGM

题意

两个人玩取石子游戏,按照如下规则取石子:

  1. 每次可以选择一堆去掉任意多个。
  2. 每次选择所有的堆,去掉任意多个(每堆去掉的数量相同)。

不能操作的人为负,BitLGM先手,BitAryo后手,问你谁必胜。

分析

注意到数据范围,1≤n≤3,0≤ai<3001\le n\le 3, 0\le a_i \lt 3001≤n≤3,0≤ai​<300

不难看出,这个可以转换为三个子问题。

  1. 只有一堆
  2. 只有两堆
  3. 有三堆。

下面分情况讨论。

只有一堆

不难发现,对于先手来说,我可以直接取走这一堆,所以只要 a1≠0a_1 \ne 0a1​​=0 那么必然有先手必胜 。

只有两堆

不难想到的是,我们可以用一个二维数组来判定当前的胜负态,也即:定义 G(i,j)G(i, j)G(i,j) 表示第一堆有 iii 个石子,第二堆有 jjj 个石子的胜负状态。那么有:
KaTeX parse error: No such environment: align at position 8: \begin{̲a̲l̲i̲g̲n̲}̲ G(i,j)=\left\…
然后我们发现每个状态可以 O(ai)O(a_i)O(ai​) 转移,总复杂度 O(ai3)O(a_i^3)O(ai3​) 。

(特别的,你可以简单发现,负状态很少很少,可以打表,然后贴表过,我在线计算直接T上天了,没办法交表过了

有三堆

博弈的题目,尤其是各种取石子游戏,都有个特别的技巧:一言不合算异或。这可能是题目做多了的奇怪直觉

不难发现当 $a_0\hat{}a_1\hat{}a_2 != 0 $ 时有先手必胜,否则先手必败。

下面是证明:

我们定义三元组 (a0,a1,a2)(a_0, a_1, a_2)(a0​,a1​,a2​) 为一个局面,g(a0,a1,a2)g(a_0, a_1, a_2)g(a0​,a1​,a2​) 表示当前局面的估值函数。

首先,∀(a0,a1,a2)≤2\forall (a_0,a_1,a_2) \le 2∀(a0​,a1​,a2​)≤2 我们都可以根据暴力的方法枚举出来,满足上述性质。

然后通过数学归纳法来证明~~(虽然我并不知道这个证明严不严谨)~~

我们假设对于 ∀a0≤x,a1≤y,a2≤z\forall a_0 \le x, a_1\le y, a_2\le z∀a0​≤x,a1​≤y,a2​≤z 满足上述性质,三者不同时取等号,下面证明 ∀a0′=x,a1′=y,a2′=z\forall a_0^{'}=x, a_1^{'}=y, a_2^{'}= z∀a0′​=x,a1′​=y,a2′​=z 满足上述性质。

也即需要证明两个子结论:

  1. $a_0^{’} \hat{} a_1{’}\hat{}a_2{’} = 0 $ 时,是个必败态,也即所有后继都是必胜态。

  2. $a_0^{’} \hat{} a_1{’}\hat{}a_2{’} \ne 0 $ 时,是个必胜态,也即存在后继是必败态。

必败态证明

也即需要证明 ∀a0≤x,a1≤y,a2≤z\forall a_0\le x, a_1 \le y, a_2\le z∀a0​≤x,a1​≤y,a2​≤z ,三者不同时为0时, g(a0,a1,a2)≠0g(a_0, a_1, a_2) \ne 0g(a0​,a1​,a2​)​=0 。

这将需要证明四个子结论

  1. a0=x−k<x⇒g(a0,a1,a2)≠0a_0 = x-k \lt x \Rightarrow g(a_0, a_1, a_2) \ne 0a0​=x−k<x⇒g(a0​,a1​,a2​)​=0
  2. a1=y−k<y⇒g(a0,a1,a2)≠0a_1 =y-k\lt y \Rightarrow g(a_0, a_1, a_2) \ne 0a1​=y−k<y⇒g(a0​,a1​,a2​)​=0
  3. a2=z−k<z⇒g(a0,a1,a2)≠0a_2 =z-k\lt z \Rightarrow g(a_0, a_1, a_2) \ne 0a2​=z−k<z⇒g(a0​,a1​,a2​)​=0
  4. a0=x−k,a1=y−k,a2=z−k⇒g(a0,a1,a2)≠0a_0=x-k,a_1=y-k,a_2=z-k\Rightarrow g(a_0, a_1, a_2) \ne 0a0​=x−k,a1​=y−k,a2​=z−k⇒g(a0​,a1​,a2​)​=0

其中前三个证明方式相同,下面仅证明 111 和 444
KaTeX parse error: No such environment: align at position 8: \begin{̲a̲l̲i̲g̲n̲}̲ &g(a_0, a_1, a…

上述 111 得证。

下面证明 444 ,因为我们有 $ a_0^{’} \hat{} a_1{’}\hat{}a_2{’} = 0$ ,所以对于 a0′,a1′,a2′a_0^{'}, a_1^{'}, a_2^{'}a0′​,a1′​,a2′​ 的任意一个二进制位来说,要么有两个这一位是 111 ,要么三个这一位都是 000 。因为我们有 k≠0k\ne 0k​=0 ,所以 kkk 一定存在一个二进制位为 111,如果我们只考虑这个二进制位 iii ,那么有

KaTeX parse error: No such environment: align at position 8: \begin{̲a̲l̲i̲g̲n̲}̲ &((a_{0}^{'}-k…

所以一定会有一个比特位不为 000 ,命题 444 得证。

至此,所有子状态都是必胜态,所以当前状态是必胜态,原命题得证。

必胜态证明

也即我们需要找到一个 (a0,a1,a2)(a_0, a_1, a_2)(a0​,a1​,a2​) 使其满足 a0^a1^a2=0a_0\hat{}a_1\hat{}a_2 = 0a0​^a1​^a2​=0

我们令 a0′^a1′^a2′=ka_0^{'}\hat{}a_1^{'}\hat{}a_2^{'} = ka0′​^a1′​^a2′​=k 。

  1. 如果 a0′>a0′^ka_0^{'} \gt a_0^{'}\hat{}ka0′​>a0′​^k , 我们令 a0=a0′^ka_0 = a_0^{'}\hat{}ka0​=a0′​^k 有上式成立。

  2. 如果 a1′>a1′^ka_1^{'} \gt a_1^{'}\hat{}ka1′​>a1′​^k , 我们令 a1=a1′^ka_1=a_1^{'}\hat{}ka1​=a1′​^k 有上式成立。

  3. 如果 a2′>a2′^ka_2^{'} \gt a_2^{'}\hat{}ka2′​>a2′​^k , 我们令 a2=a2′^ka_2=a_2^{'}\hat{}ka2​=a2′​^k 有上式成立。

下面证明不存在除此之外的第四种情况。

我们找到 kkk 的最高位,因为 kkk 的这一位为 111 ,说明一定存在 axa_xax​ 的这一位也为 111 。(这个可以通过反证法简单得到,不再赘述)。

那么我们可以得到 $a_x\hat{}k $ 在这一位上会被翻转,也即一个较高位被反转了,后面的位无论是变成什么,我们都有 $a_x\hat{}k \lt a_x $ 。

也即上述三个条件,一定至少有一个为真,原命题得证。

至此正反面都证明完毕,也即结论成立。

下面是代码:

#include<bits/stdc++.h>
using namespace std;
const int upper = 301;
int xx[upper] = {0,1,3,4,6,8,9,11,12,14,16,17,19,21,22,24,25,27,29,30,32,33,35,37,38,40,42,43,45,46,48,50,51,53,55,56,58,59,61,63,64,66,67,69,71,72,74,76,77,79,80,82,84,85,87,88,90,92,93,95,97,98,100,101,103,105,106,108,110,111,113,114,116,118,119,121,122,124,126,127,129,131,132,134,135,137,139,140,142,144,145,147,148,150,152,153,155,156,158,160,161,163,165,166,168,169,171,173,174,176,177,179,181,182,184,186};
int yy[upper] = {0,2,5,7,10,13,15,18,20,23,26,28,31,34,36,39,41,44,47,49,52,54,57,60,62,65,68,70,73,75,78,81,83,86,89,91,94,96,99,102,104,107,109,112,115,117,120,123,125,128,130,133,136,138,141,143,146,149,151,154,157,159,162,164,167,170,172,175,178,180,183,185,188,191,193,196,198,201,204,206,209,212,214,217,219,222,225,227,230,233,235,238,240,243,246,248,251,253,256,259,261,264,267,269,272,274,277,280,282,285,287,290,293,295,298,301};
int main() {int n, a=0, b=0, c=0;cin>>n;if(n==1) {cin>>c;cout<<(c!=0?"BitLGM":"BitAryo")<<endl;}if(n==2) {cin>>b>>c;if(b>c) swap(b, c);bool flag = true;for(int i = 0;i<upper;i++) {if(b==xx[i]&&c==yy[i]) flag = false;}cout<<(flag?"BitLGM":"BitAryo")<<endl;}if(n==3) {cin>>a>>b>>c;cout<<((a^b^c)!=0?"BitLGM":"BitAryo")<<endl;}return 0;
}

彩蛋

在这个题过完之后,还是觉得自己的证明不够优雅,于是我翻了一下原题解下的博客评论,发现了对于二堆这种情况,有一个更为优雅的结论。

定义 φ=5+12\varphi = \frac{\sqrt{5}+1}{2}φ=25​+1​ ,也就是黄金分割比,那么我们有所有的必败局面为 (⌊kφ⌋,⌊kφ2⌋)(\lfloor k\varphi\rfloor, \lfloor k\varphi^2\rfloor)(⌊kφ⌋,⌊kφ2⌋) ,例如:

  1. k=0k=0k=0 有必败局面 (0,0)(0,0)(0,0)
  2. k=1k=1k=1 有必败局面 (1,2)(1, 2)(1,2)
  3. k=2k=2k=2 有必败局面 (3,5)(3, 5)(3,5)
  4. …\dots…
  5. k=50k=50k=50 有必败局面 (80,130)(80,130)(80,130)
  6. …\dots…

证明的论文在这里:传送门。

所以本质上是可以在线打表 O(1)O(1)O(1) 过掉原题的,甚至可以限制 axa_xax​ 的范围在 [0,1018][0,10^{18}][0,1018]

codeforces 282 D. Yet Another Number Game相关推荐

  1. Codeforces 617E XOR and Favorite Number

    Discription Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Eac ...

  2. CodeForces - 617E XOR and Favorite Number (莫队+前缀和)

    Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is g ...

  3. 【CodeForces - 245H 】Queries for Number of Palindromes (带容斥的区间dp)

    题干: You've got a string s = s1s2... s|s| of length |s|, consisting of lowercase English letters. The ...

  4. CodeForces - 617E XOR and Favorite Number(莫队)

    题目链接:点击查看 题目大意:给出一个由n个数组成的数列,现在给出m组询问,每次询问包含一个l和一个r,要求回答在闭区间[l,r]中有多少组(i,j)满足[i,j]闭区间内的所有数的异或和等于k 题目 ...

  5. codeforces 617E XOR and Favorite Number 莫队

    https://vjudge.net/problem/CodeForces-617E 题目大意:给nnn个数,mmm个询问,以及一个数kkk,每次询问要输出[l,r][l,r][l,r]内满足a[i] ...

  6. CodeForces -617E XOR and Favorite Number(莫队)

    题目链接:点击这里 题目大意: 给定一个长度为 nnn 的序列 a1,a2,...,ana_1,a_2,...,a_na1​,a2​,...,an​ ,再给出一个数字 kkk , mmm 组询问每组询 ...

  7. 一起开心集训队第一周训练赛2021/3/14

    文章目录 比赛链接 A CodeForces 1481D AB Graph 题意: 题解: 代码: B CodeForces 1481E Sorting Books 题意: 题解: 代码: C Cod ...

  8. 莫队算法(普通莫队、带修莫队、树上莫队、不删除莫队)学习笔记【理解+套路/核心代码+例题及题解】

    一.理解 我的理解就是巧妙的暴力,利用双指针以及分块思想,巧妙的移动双指针,时间复杂度可以达到O(NlogN). 强推博客:写的又好又全.链接 二.套路 1.普通莫队 [1]核心代码 bool cmp ...

  9. MySQL Cluster2个数据节点压力测试--mysqlslap工具压400W写

    锅巴哥的个人建议:cluster叫电信运营商版本,所以基本上在很大的用户并发量的情况下才会用到,对连接数的线性增长要求高的场景,千兆就不用想了, 没万兆就不用玩了. 很不幸,我的就是千兆网络,我的数据 ...

最新文章

  1. hibernate 悲观锁乐观锁
  2. [第16天]IIS UNICODE 编码漏洞
  3. MapReduce的简单实例WordCount
  4. Solaris是出色的Java开发平台的原因
  5. 地灾应急暨地灾危险性评估培训班学习笔记
  6. javascript中的Date类型
  7. 利用计算机的说课稿,《计算机的发展与应用》说课稿
  8. AssetBundle Manager and Example Scenes
  9. audio-音频标签
  10. Python学习手册--第一部分(使用入门)
  11. ping测试告警软件,SmartPing:一个服务器Ping值监测工具,带报警功能
  12. 计算机科学与ICT技术书籍、资料推荐
  13. 执念于当下的平淡为美好
  14. Python爬取《你好李焕英》猫眼实时票房
  15. ZJNU——1695(分栗子)
  16. CSS设计美丽之百合花(小作品)
  17. JMS(Java Messaging Service)基础
  18. 玩转Jetson Nano(一)烧写系统
  19. 标准查询运算符(SQO)
  20. 信息学奥赛一本通1000:c++入门测试题

热门文章

  1. 使用watermark.js给HTML、Word、PPT、Excel等添加水印
  2. 以下选项中、不是python对文件的打开模式的是_以下选项中,不是Python对文件的打开模式的是...
  3. 【串口助手】Python从零开始制作温湿度串口上位机
  4. html5 css3 图片画廊,js和CSS3 3D立方体图片画廊特效
  5. 第一届中国数字藏品大会顺利召开
  6. 根轨迹分析 matlab,MATLAB的根轨迹分析
  7. Fuzzing及Sulley简介
  8. todesk远程桌面没声音
  9. 步进电机S曲线的生成
  10. 数学归纳法求证欧几里得算法