题目链接: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
解析:这个题主要是卡精度,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;
}