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, 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.
Input
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.
Output
For each game, print Alice if Alice wins this game and Bob otherwise.
Example
input
Copy
4 0 3 3 3 3 4 4 4
output
Copy
Bob Alice Bob Alice
这次cf莫名奇妙变成多组数据输入,有点不习惯,前面的题加return 0 wa了两发。。这个题刚开始看错题了,以为是从左往右走,但是不影响我sg打表:dp[i]为0代表着这个位置出发,后手能赢,为1代表着这个位置先手能赢。那么从后手能赢推导出哪些位置为1.。。。找规律,没找到规律(菜呀),看别人的博客才知道规律。。
打表代码:
#include
using namespace std;
const int N=1e6+10;
int dp[N];
int main()
{
int t;
int n,k;
memset(dp,0,sizeof(dp));
cin>>n>>k;
dp[n]=0;
dp[n-1]=1;
dp[n-2]=1;
for(int i=n-3;i>=0;i--){
if(i+k<=n){
if(dp[i+1]==0||dp[i+2]==0||dp[i+k]==0) dp[i]=1;
}
else if(dp[i+1]==0||dp[i+2]==0) dp[i]=1;
}
for(int i=0;i<=n;++i) printf("%d %d\n",i,dp[i]);
}
AC代码:
规律来自:https://www.cnblogs.com/qieqiemin/p/11186681.html
#include
using namespace std;
const int N=1e5+10;
int dp[N];
int main()
{
int t;
cin>>t;
while(t--){
int n,k;
cin>>n>>k;
if(n<=k){
if(n==k) printf("Alice\n");
else{
if(n%3!=0) printf("Alice\n");
else printf("Bob\n");
}
continue;
}
if(k==3){
if(n%4!=0) printf("Alice\n");
else printf("Bob\n");
continue;
}
if (k % 3) {
if (n % 3) printf("Alice\n");
//ok = 1;
else printf("Bob\n");
//ok = 0;
continue;
}
int tmp=n%(k+1);
if(tmp%3!=0||tmp==k)printf("Alice\n");
else printf("Bob\n");
/*printf("%d %d %d\n",(n-k)%k,(n-1)%k,(n-2)%k);
if((n-k)>0&&(n-k)%k==0||(n-1)>0&&(n-1)%k==0||(n-2)>0&&(n-2)%k==0){
printf("Alice\n");
}
else printf("Bob\n");*/
}
}