51Nod 1185 威佐夫游戏 V2

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

有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

题解:博弈论之黄金分割定律,属于奇异局。这里因为数据比较大,所以需要使用到乘法模拟
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cmath>
 6 using namespace std;
 7 typedef long long ll;
 8 ll tmp[3] = {618033988,749894848,204586834};
 9 ll MOD = 1000000000;
10 int main()
11 {
12     int t;
13     ll m,n;
14     cin>>t;
15     while(t--){
16         cin>>m>>n;
17         if(m<n) swap(n,m);
18         ll cha=m-n;
19         ll ta=cha/MOD,tb=cha%MOD;
20         ll tp=tb*tmp[2];
21         tp=ta*tmp[2]+tb*tmp[1]+tp/MOD;
22         tp=ta*tmp[1]+tb*tmp[0]+tp/MOD;
23         tp=cha+ta*tmp[0]+tp/MOD;
24         if(tp==n) cout<<"B"<<endl;
25         else cout<<"A"<<endl;
26     }
27     return 0;
28 }
posted @ 2017-08-12 10:43  wydxry  阅读(261)  评论(0编辑  收藏  举报
Live2D