51 NOD _ 1185 威佐夫游戏 V2

题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1185


基准时间限制:1 秒 空间限制:131072 KB 分值: 0  难度:基础题
 收藏
 关注
有2堆石子。A B两个人轮流拿,A先拿。每次可以从一堆中取任意个或从2堆中取相同数量的石子,但不可不取。拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出2堆石子的数量,问最后谁能赢得比赛。
例如:2堆石子分别为3颗和5颗。那么不论A怎样拿,B都有对应的方法拿到最后1颗。
Input
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 10000)
第2 - T + 1行:每行2个数分别是2堆石子的数量,中间用空格分隔。(1 <= N <= 10^18)
Output
共T行,如果A获胜输出A,如果B获胜输出B。
Input示例
3
3 5
3 4
1 9
Output示例
B
A
A
相关问题
Bash游戏 V2 
10
 
 
Bash游戏
0
 
威佐夫游戏
0
 
Bash游戏 V4 
40
 
 
Nim游戏
0
Bash游戏 V3 
20
 


解析:这个题主要是卡精度,double存不下, 具体见下面代码


JAVA:

//import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

public class Main {
	public static Scanner s = new Scanner(System.in);

	public static void main(String args[]) throws Exception {
		BigDecimal v = new BigDecimal("1.61803398874989484820458");
		Scanner s = new Scanner(System.in);
		BigInteger a, b;
//		System.out.println(v);
		int T = s.nextInt();
		for(int i = 1; i <= T; i++) {
			a = s.nextBigInteger(); b = s.nextBigInteger();
			if(a.compareTo(b) < 0) {
				BigInteger t = a;
				a = b; b = t;
			}
			BigDecimal z = BigDecimal.valueOf(a.longValue() - b.longValue());
			if(z.multiply(v).toBigInteger().compareTo(b) == 0) 
				System.out.println("B");
			else 
				System.out.println("A");
		}
		
		
	}

}



C++:


#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#define N 50009
using namespace std;
const int inf = 0x3f3f3f3f;
typedef long long LL;
const int mod = 1000000000;

LL G[3] = {618033988, 749894848, 204586834};

int main()
{
    LL a, b;
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%lld%lld", &a, &b);
        LL pre, now, d;
        if(a < b) swap(a, b);
        //类比大整数乘法
        d = a - b;
        pre = d / mod; now = d % mod;
        LL a1 = now * G[2];
        LL a2 = now * G[1] + pre * G[2] + a1 / mod;
        LL a3 = now * G[0] + pre * G[1] + a2 / mod;
        LL a4 = d + pre * G[0] + a3 / mod; //算最高位即可,其他位忽视
        if(a4 == b) puts("B");
        else puts("A");
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值