本文主要是介绍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;
}
这篇关于A - Alice and Bob的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!