51Nod 1185 威佐夫游戏 V2 (威佐夫博弈)

基准时间限制: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

析:根据威佐夫博弈来说,只要不是奇异局势一定是先手必胜,也就是一个公式ak = k * (1+√5)/2,bk = ak + k。很明显题目给的是ak和bk,只要满足这个式子就是奇异局势,但是由于给定的数太大了,所以可能会有精度误差,所以可以用高精度来解决,但是因为n 和 m,是不超过1e18的,所以还有一个更好的办法,就是模拟小数的乘法,把(1+√5)/2,先算出来一个足够的精度,1.6180339887498948482045868343656381177203091798057625,然后考虑需要前几位,前24位就够了,由于要保证乘法过程不超过64位,所以拆成3个每个8位,然后还有一个算子1e8,然后就可以模拟 k * 该小数,无非是分块来计算,不再是一个数一个数的乘了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2000 + 1;
const int maxm = 2e4 + 10;
const LL mod = 100000000;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}
// 1.6180339887498948482045868343656381177203091798057625
const int a[] = {61803398, 87498948, 48204586};

int main(){
  int T;  scanf("%d", &T);
  while(T--){
    LL n, m;
    scanf("%lld %lld", &n, &m);
    if(n > m)  swap(n, m);
    LL k = m - n;
    LL x = k / mod, y = k % mod;
    LL tmp = y * a[2];
    tmp = x * a[2] + y * a[1] + tmp / mod;
    tmp = x * a[1] + y * a[0] + tmp / mod;
    tmp = k + x * a[0] + tmp / mod;
    if(tmp == n)  printf("B
");
    else printf("A
");
  }
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/8422343.html