Description
m堆石子,每堆有对应一个(Bi,Ni),表示这堆有Ni个石子,每次可以拿Bi^x个石子(1<=Bi^x<=Ni),谁不能拿谁输,1和2轮流拿,两人足够机智,问谁必胜
Input
第一行一整数T表示用例组数,每组用例首先输入石子堆数m,之后2*m个整数(Bi,Ni)来表示每堆石子
(1<=m<=100,1<=Bi,Ni<=1e9)
Output
输出必胜者
Sample Input
3
1 2 3
1 7 3
2 6 11 3 2
Sample Output
2
1
2
Solution
组合游戏,关键在于怎么计算出每堆石子的sg值
B为奇数,那么B^x必为奇数,根据奇偶性轻易得知当N为奇数时先手必胜(sg=1),当N为偶数时后手必胜(sg=0)
B为偶数,由于B^x=(B+1-1)^x=K*(B+1)+1或K*(B+1)+B,令N=X*(B+1)+T(0<=T<=B),那么问题其实变成T个石子,每次可以拿1个或B个,由于T<=B,所以只有当T=B时先手有机会第一次拿B个,其他所有情况两个人都无法拿B个,当T < B时,T为奇则先手胜(sg=1),T为偶则后手胜(sg=0),T=B时两个后继状态分别是B-1和0,sg值分别是0和1,故T=B时sg=2
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 1111
int main()
{
freopen("powers.in","r",stdin);
int T,m,b,n;
scanf("%d",&T);
while(T--)
{
int ans=0;
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&b,&n);
if(b&1)
{
if(n&1)ans^=1;
else ans^=0;
}
else
{
int t=n%(b+1);
if(t==b)ans^=2;
if(t&1)ans^=1;
else ans^=0;
}
}
printf("%d\n",ans?1:2);
}
return 0;
}