1070 Bash游戏 V4

有一堆石子共有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

题解:答案很简单,当n为fibonacci数时,先手必败,否则先手必胜。

简单证明:1、当n等于2的时候结论成立,假设小于fibonacci数n的所有fibonacci数满足结论,则有n = a+b,a、b也为fibonacci数,a>b。又对于a与b,先手不能先拿完b,因为2*b>a,所以在b中取,此时必败,成为a中先手,又必败,故结论成立。

2、当n为非fibonacci数时,先手必胜,根据Zeckendorf定理:任何一个正整数可以分解为若干个不连续的fibonacci数之和。于是n = a + b + ...... + j + k + l。先手先拿l,因为不连续,所以2*l<k,于是在k中取,因为是fibonacci数,所以先手必败,以此类推,故后手必败。

代码:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-10;
const int maxn = 1e5+7;
const int mod = 1e9+7;

int T;
int a;
int fa[1000];
void init()
{
    fa[0] = fa[1] = 1;
    for(int i = 2;i < 1000;i++){
        fa[i] = fa[i-1]+fa[i-2];
    }
}
int main()
{
    init();
    scanf("%d",&T);
    while(T--){
        scanf("%d",&a);
        bool sign = 1;
        for(int i = 0;i < 1000;i++){
            if(fa[i]==a){
                sign = 0;
                break;
            }
        }
        if(sign) puts("A");
        else puts("B");
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值