有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 分析:高精度+黄金分割定律,N到2<<64,用double表示黄金分割率会出现精度问题,所以要将其分割成三部分每部分9位。 代码:#include<bits/stdc++.h> using namespace std; typedef long long LL; const int MOD=1e9; int Gold[3]={618033988,749894848,204586834}; int main(){ int T;scanf("%d",&T); while(T--){ LL a,b,temp; scanf("%lld%lld",&a,&b); if(a<b) swap(a,b); LL dist=a-b,ta=dist/MOD,tb=dist%MOD; temp=Gold[2]*tb; temp=ta*Gold[2]+tb*Gold[1]+temp/MOD; temp=ta*Gold[1]+tb*Gold[0]+temp/MOD; temp=dist+ta*Gold[0]+temp/MOD; if(temp==b) printf("B\n"); else printf("A\n"); } return 0; }