「CF1194D」1-2-K Game【博弈论】

D. 1-2-K Game

time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output
Alice and Bob play a game. There is a paper strip which is divided into $n + 1$ cells numbered from left to right starting from $0$. There is a chip placed in the $n-th$ cell (the last one).

Players take turns, Alice is first. Each player during his or her turn has to move the chip 1 1 1, 2 2 2 or k k k cells to the left (so, if the chip is currently in the cell i i i, the player can move it into cell i   −   1 i - 1 i1, i   −   2 i - 2 i2 or i   −   k i - k ik). The chip should not leave the borders of the paper strip: it is impossible, for example, to move it k cells to the left if the current cell has number i   &lt;   k i &lt; k i<k. The player who can’t make a move loses the game.

Who wins if both participants play optimally?

Alice and Bob would like to play several games, so you should determine the winner in each game.

Input

The first line contains the single integer T ( 1   ≤   T   ≤   100 ) T (1 \leq T \leq 100) T(1T100) — the number of games. Next T T T lines contain one game per line. All games are independent.

Each of the next T T T lines contains two integers n n n and k ( 0   ≤   n   ≤   1 0 9 , 3   ≤   k   ≤   1 0 9 ) k (0 \leq n \leq 10^9, 3 \leq k \leq 10^9) k(0n109,3k109) — the length of the strip and the constant denoting the third move, respectively.

Output

For each game, print Alice if Alice wins this game and Bob otherwise.

Example

input
4
0 3
3 3
3 4
4 4
output
Bob
Alice
Bob
Alice

题意

  • 就是一个小球放在位置 n n n处,两个人轮流玩游戏,每次可以将小球移到 i − 1 i-1 i1 ( ( i ≥ 1 ) i\geq1) i1,或者将小球移到 i − 2 i-2 i2 ( ( i ≥ 2 ) i\geq 2) i2,再给定数 k k k,每次可以将小球移到 i − k i-k ik ( ( i ≥ k ) i\geq k) ik

题解

  • 可以考虑sg函数打表找规律
  • sg函数打表方法也就是找出当前状态所有可达的下一个状态,在这些状态的sg函数值中找出第一个不被包含在其中的最小自然数就是当前状态的sg函数值

打表代码

  •   #include<bits/stdc++.h>
      
      using namespace std;
      const int maxn=1e5+10;
      
      int n=100,k,sg[maxn];
      
      void solve(int k)
      {
      	memset(sg,0,sizeof(sg));
      
      	set<int> s;
      	for(int i=1;i<=n;i++){
      		s.clear();
      		if(i-1>=0) s.insert(sg[i-1]);
      		if(i-2>=0) s.insert(sg[i-2]);
      		if(i-k>=0) s.insert(sg[i-k]);
      
      		int cnt=0,ans=-1;
      		for(auto j:s) {
      			if(j!=cnt) {
      				ans=cnt;
      				break;
      			}
      			cnt++;
      		}
      		if(ans==-1) ans=s.size();
      		sg[i]=ans;
      	}
      	printf("--------%d---------\n",k);
      	for(int i=1;i<=n;i++) printf("%d%c",sg[i],i%10==0?'\n':' ');
      	printf("-------------------\n");
      }
      
      int main()
      {
      	while(~scanf("%d",&k)){
      		solve(k);
      	}
      }
    
    结果:
  •   3
      --------3---------
      1 2 3 0 1 2 3 0 1 2
      3 0 1 2 3 0 1 2 3 0
      1 2 3 0 1 2 3 0 1 2
      3 0 1 2 3 0 1 2 3 0
      1 2 3 0 1 2 3 0 1 2
      3 0 1 2 3 0 1 2 3 0
      1 2 3 0 1 2 3 0 1 2
      3 0 1 2 3 0 1 2 3 0
      1 2 3 0 1 2 3 0 1 2
      3 0 1 2 3 0 1 2 3 0
      -------------------
      4
      --------4---------
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      -------------------
      5
      --------5---------
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      -------------------
      6
      --------6---------
      1 2 0 1 2 3 0 1 2 0
      1 2 3 0 1 2 0 1 2 3
      0 1 2 0 1 2 3 0 1 2
      0 1 2 3 0 1 2 0 1 2
      3 0 1 2 0 1 2 3 0 1
      2 0 1 2 3 0 1 2 0 1
      2 3 0 1 2 0 1 2 3 0
      1 2 0 1 2 3 0 1 2 0
      1 2 3 0 1 2 0 1 2 3
      0 1 2 0 1 2 3 0 1 2
      -------------------
      7
      --------7---------
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      -------------------
      8
      --------8---------
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      -------------------
      9
      --------9---------
      1 2 0 1 2 0 1 2 3 0
      1 2 0 1 2 0 1 2 3 0
      1 2 0 1 2 0 1 2 3 0
      1 2 0 1 2 0 1 2 3 0
      1 2 0 1 2 0 1 2 3 0
      1 2 0 1 2 0 1 2 3 0
      1 2 0 1 2 0 1 2 3 0
      1 2 0 1 2 0 1 2 3 0
      1 2 0 1 2 0 1 2 3 0
      1 2 0 1 2 0 1 2 3 0
      -------------------
      10
      --------10---------
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      -------------------
      11
      --------11---------
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      -------------------
      12
      --------12---------
      1 2 0 1 2 0 1 2 0 1
      2 3 0 1 2 0 1 2 0 1
      2 0 1 2 3 0 1 2 0 1
      2 0 1 2 0 1 2 3 0 1
      2 0 1 2 0 1 2 0 1 2
      3 0 1 2 0 1 2 0 1 2
      0 1 2 3 0 1 2 0 1 2
      0 1 2 0 1 2 3 0 1 2
      0 1 2 0 1 2 0 1 2 3
      0 1 2 0 1 2 0 1 2 0
      -------------------
      13
      --------13---------
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      2 0 1 2 0 1 2 0 1 2
      0 1 2 0 1 2 0 1 2 0
      1 2 0 1 2 0 1 2 0 1
      -------------------
    

规律就很明显了

代码

  •   #include<bits/stdc++.h>
      
      using namespace std;
      
      int main()
      {
          int t;scanf("%d",&t);
          for(int i=1,n,k;i<=t;i++){
              scanf("%d %d",&n,&k);
              if(k%3) printf(n%3?"Alice\n":"Bob\n");
              else {
                  int len=3*((k/3)-1)+4;
                  int mo=n%len;
      
                  if(mo<=len-4&&mo%3==0) printf("Bob\n");
                  else printf("Alice\n"); 
              }
          }
      }
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值