您的位置:首页 > 其它

威佐夫博弈(模板+减少精度算法)

2016-09-15 20:19 148 查看


1072 威佐夫游戏


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


Output
共T行,如果A获胜输出A,如果B获胜输出B。


Input示例
3
3 5
3 4
1 9


Output示例
B
A
A


相关问题

当n很大时可能会出现精度缺失的问题,因此我们需要模拟乘法

就将0.618033988749894848204586834

折成整数存进数组里

<strong>#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<limits.h>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<map>
using namespace std;
#define maxn 50005
int main()
{
int T,n,m;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
int temp;
if(n<m)
{
temp=n;
n=m;
m=temp;
}
int k=n-m;
n=(int)(k*(1+sqrt(5))/2.0);
if(n==m)
printf("B\n");
else
printf("A\n");
}
}
</strong>


减少精度:

#include<stdio.h>
long long t[3] = {618033988, 749894848, 204586834};
#define mod 1000000000
int main(void)
{
int T;
long long a, b, l, r, ans, temp;
scanf("%d", &T);
while(T--)
{
scanf("%lld%lld", &a, &b);
if(a>b)
temp = a, a = b, b = temp;
l = (b-a)/mod;
r = (b-a)%mod;
ans = ((r*t[2]/mod+(r*t[1]+l*t[2]))/mod+(r*t[0]+l*t[1]))/mod+l*t[0]+b-a;
if(ans==a)
printf("B\n");
else
printf("A\n");
}
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: