A - Alice and Bob

Alice and Bob

题目描述

Alice and Bob like playing games. There are two piles of stones with numbers n{n}n and m{m}m. Alice and Bob take turns to operate, each operation can take away k(k>0){k}(k>0)k(k>0) stones from one pile and take away s×k(s≥0)s \times k(s \geq 0)s×k(s≥0) stones from another pile. Alice plays first. The person who cannot perform the operation loses the game. 

Please determine who will win the game if both Alice and Bob play the game optimally.

输入描述:

The first line contains an integer T(1≤T≤104)T(1 \le T \le 10^4)T(1≤T≤104) denotes the total number of test cases.
Each test case contains two integers n,m(1≤n,m≤5×103)n,m(1 \le n,m \leq 5 \times 10^3)n,m(1≤n,m≤5×103) in a line, indicating the number of two piles of stones.

输出描述:

For each test case, print "Alice" if Alice will win the game, otherwise print "Bob".

示例1

输入

复制

5
2 3
3 5
5 7
7 5
7 7

输出

复制

Bob
Alice
Bob
Bob
Alice

思路:题意为当Alice操作完之后就没有了就是Alice赢。将两堆看作一个二维数组,所以我们记录下Alice赢的情况,标记为1,注意可以先取堆A,也可先取堆B。sg函数(虽然我也不太懂,hhh),此处用dp[N]][N]表示,初始化为0,即必输的状态。然后暴力枚举赢的状态,标记为1.

代码如下:

#include <bits/stdc++.h>
using namespace std;
#define N 5010
bool dp[N][N];//为1即Alice win
int t,n,m;
int main(){
    //暴力打表 hhh
    for(int i=0;i<=5000;i++)
    for(int j=0;j<=5000;j++){
            if(!dp[i][j]){
                //暴力枚举Alice必赢的情况
                for(int k=1;i+k<=5000;k++)//先取堆A:i,再取堆B :j
                for(int s=0;j+k*s<=5000;s++)
                dp[i+k][j+k*s]=1;
                    
                
                for(int k=1;j+k<=5000;k++)//先取堆B:j,再取堆A:i
                for(int s=0;i+k*s<=5000;s++)
                dp[i+s*k][j+k]=1;
        }
    }
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        if(dp[n][m]) printf("Alice\n");
        else printf("Bob\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值