问题 J: 1-2-K Game
时间限制: 1 Sec 内存限制: 128 MB
提交: 520 解决: 153
[提交] [状态] [命题人:admin]
题目描述
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, 2 or k cells to the left (so, if the chip is currently in the cell i, the player can move it into cell i - 1, i - 2 or 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. 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.
输入
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of games. Next T lines contain one game per line. All games are independent.
Each of the next T lines contains two integers n and k (0 ≤ n ≤ 109, 3 ≤ k ≤ 109) — the length of the strip and the constant denoting the third move, respectively.
输出
For each game, print Alice if Alice wins this game and Bob otherwise.
样例输入
复制样例数据
4
0 3
3 3
3 4
4 4
样例输出
Bob
Alice
Bob
Alice
#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++){
int n,k;
scanf("%d%d",&n,&k);
if(k%3!=0){
if(n%3==0) printf("Bob\n");
else printf("Alice\n");
}
else{
if(k==3){
if(n%4==0) printf("Bob\n");
else printf("Alice\n");
}
else{
int tem=n%(k+1);
if(tem%3==0&&tem!=k) printf("Bob\n");
else printf("Alice\n");
}
}
}
}
下面这个是打表代码
#include <stdio.h>
#include <string.h>
#define MAXN 1000 + 10
#define N 20
int f[3],SG[MAXN],S[MAXN];//f数组是可以转移的方法的集合,sg是sg函数值,s是上个状态sg值的集合
void getSG(int n){
int i,j;
memset(SG,0,sizeof(SG));
SG[0]=0;SG[1]=1;SG[2]=2;SG[3]=0;
for(i = 4; i <= n; i++){
memset(S,0,sizeof(S));
for(j = 0; f[j] <= i && j <= 2; j++)
S[SG[i-f[j]]] = 1;
for(j = 0;;j++) if(!S[j]){
SG[i] = j;
break;
}
}
}
int main(){
f[0] = 1;
f[1]=2;
f[2]=12;//12这里改成k值
getSG(1000);
for(int i=0;i<=1000;i++) printf("i:%d SG:%d\n",i,SG[i]);
return 0;
}
学了一波sg函数
这个题只有神仙才能做出来8
打表发现除了3的倍数,其他的都是固定规律
3要特判
其他的3的倍数可以找出规律,每k+1循环一次
但是k要抠出来(k的时候先手必胜)