51Nod 1068 Bash游戏 V3

版权声明:低调地前行,越努力越幸运! https://blog.csdn.net/SSYITwin/article/details/83065200

1068 Bash游戏 V3 

题目来源: Ural 1180

基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题

 收藏

 关注

有一堆石子共有N个。A B两个人轮流拿,A先拿。每次拿的数量只能是2的正整数次幂,比如(1,2,4,8,16....),拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N,问最后谁能赢得比赛。

例如N = 3。A只能拿1颗或2颗,所以B可以拿到最后1颗石子。(输入的N可能为大数)

Input

第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 1000)
第2 - T + 1行:每行1个数N。(1 <= N <= 10^1000)

Output

共T行,如果A获胜输出A,如果B获胜输出B。

Input示例

3
2
3
4

Output示例

扫描二维码关注公众号,回复: 3829230 查看本文章
A
B
A

SG()打表:

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[1002];
bool v[1002];
int sg[1002];
void init()
{
	a[0]=1;
	for(int i=1;i<=32;i++)
	{
		a[i]=a[i-1]*2;
	}
}
void SG()
{
	for(int i=1;i<=1000;i++)
	{
		memset(v,false,sizeof(v));
		for(int j=0;a[j]<=i;j++)
		   v[sg[i-a[j]]]=true;
		for(int j=0;j<=1000;j++)
		{
			if(v[j]==0)
			{
				sg[i]=j;
				break;
			}
		}
	}
}
int main()
{
	int n;
	init();
	SG();
	scanf("%d",&n);
	while(n--)
	{
		if(sg[n]==0)
		   cout<<"n="<<n<<"时:B"<<endl;
		else
		 cout<<"n="<<n<<"时:A"<<endl;
		 n--;
	}
	return 0;
}

打表可以得到规律: 

import java.util.Scanner;
import java.io.*;
import java.math.*;
public class Main {
	//static String str[]=new String[100010];
    public static void main(String args[]) {
    	BigInteger n;
    	int t;
    	Scanner scan=new Scanner(System.in);
    	t=scan.nextInt();
    	while(t>0)
    	{
    		n=scan.nextBigInteger();
    		if((n.mod(BigInteger.valueOf(3))).compareTo(BigInteger.ZERO)==0)
    		{
    			System.out.println("B");
    		}else
    		{
    			System.out.println("A");
    		}
    		t--;
    	}
    	
    }
}

猜你喜欢

转载自blog.csdn.net/SSYITwin/article/details/83065200