HDU-578 Alice and Bob

Alice and Bob

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65535/65536 K (Java/Others)

Problem Description

Alice and Bob are playing a stone game in a board of n×m cells.

In the begining, the stone is in the upperleft cell. And in each turn, they can move the stone one cell to the right or one cell down, or diagonally k cells down to the right, which means if you are at (x,y), then you could move into (x+1,y), (x,y+1) or (x+k,y+k) at the next step. The player who can not move loses. They play in turns and Alice moves first.

Now given n, m and k, could you tell me who is the winner?

Input

First line contains an integer T(1≤T≤10), denoting the number of test cases.

In each test case, the first line is two integers Q and k.
In the following Q lines, each line contains n and m.(1≤Q≤1000,1≤k,n,m≤109)

Output

For each test case, output Q lines.
If Alice is the winner, output “Alice”. Otherwise “Bob”.

Sample Input

2
2 1
4 5
3 4
2 3
4 5
5 6

Sample Output

Alice
Alice
Alice
Bob

Reference Code

#include
#include
using namespace std;
int main(){
    int ca;
    scanf("%d",&ca);
    while(ca--){
        int Q,k;
        scanf("%d %d",&Q,&k);
        while(Q--){
            int n,m;
            scanf("%d %d",&n,&m);
            if(k==1){
                if(n&1 && m&1)printf("Bob\n");
                else printf("Alice\n");
            }
            else{
                if(n>=m)swap(n,m);
                if(n%(k+1)==0)printf("Alice\n");
                else{
                    int tmp=((n/(k+1))&1)^((m+n)&1);
                    if(tmp==1)printf("Alice\n");
                    else printf("Bob\n");
                }
            }
        }
    }
    return 0;
}

Tips

题意:
一个 n × m n\times m n×m 的棋盘,最初位置在左上角,能做的操作为向右移动一格,或向下移动一格,或向右移动 k k k 格并向下移动 k k k 格。两人轮流操作,无法再移动为负,问先手胜还是后手胜。

解法:
k = 1 k=1 k=1 时特判; k > 2 k>2 k>2 时打sg表找规律。
发现与 min ⁡ ( m , n )   m o d   ( k + 1 ) \min(m,n)\bmod(k+1) min(m,n)mod(k+1) 以及 ( ⌊ min ⁡ ( m , n ) k + 1 ⌋   m o d   2 ) ⊕ ( ( m + n )   m o d   2 ) \left(\lfloor\frac{\min(m,n)}{k+1}\rfloor\bmod2\right)\oplus\left((m+n)\bmod2\right) (k+1min(m,n)mod2)((m+n)mod2) 有关。

你可能感兴趣的:(组队赛题解)