基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
收藏
关注
有一堆石子共有N个。A B两个人轮流拿,A先拿。每次拿的数量最少1个,最多不超过对手上一次拿的数量的2倍(A第1次拿时要求不能全拿走)。拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N,问最后谁能赢得比赛。
例如N = 3。A只能拿1颗或2颗,所以B可以拿到最后1颗石子。
Input
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 1000) 第2 - T + 1行:每行1个数N。(1 <= N <= 10^9)
Output
共T行,如果A获胜输出A,如果B获胜输出B。
Input示例
3 2 3 4
Output示例
B B A
斐波那契博弈 后手获胜情况的数字呈现斐波那契数列
#include <iostream> #include <cstring> #include <stdio.h> #include <algorithm> #include <queue> #include <vector> #include <iomanip> #include <math.h> #include <map> using namespace std; #define FIN freopen("input.txt","r",stdin); #define FOUT freopen("output.txt","w",stdout); #define INF 0x3f3f3f3f #define INFLL 0x3f3f3f3f3f3f3f #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef long long LL; typedef pair<int, int> PII; const int maxn = 1e6; LL a[maxn]; int main() { //FIN int T; scanf("%d", &T); LL n; while(T--) { scanf("%lld", &n); a[0] = 0; a[1] = 1; int flag = 0; for(int i = 2; i <= maxn; i++) { a[i] = a[i - 1] + a[i - 2]; if(a[i] == n) flag = 1; if(a[i] >= n) break; } if(!flag) puts("A"); else puts("B"); } return 0; }