D. 1-2-K Game
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 i − 1, i − 2 i - 2 i − 2 or i − k i - k i − k). 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 < k i < 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(1 ≤ T ≤ 100) — 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(0 ≤ n ≤ 109,3 ≤ k ≤ 109) — 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 i−1处 ( ( (当 i ≥ 1 ) i\geq1) i≥1),或者将小球移到 i − 2 i-2 i−2处 ( ( (当 i ≥ 2 ) i\geq 2) i≥2),再给定数 k k k,每次可以将小球移到 i − k i-k i−k处 ( ( (当 i ≥ k ) i\geq k) i≥k)
题解
- 可以考虑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"); } } }