取b^x个石子博弈

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_44316314/article/details/101862033

游戏规则:
一共有G个子游戏,一个子游戏有Bi, Ni两个数字。两名玩家开始玩游戏,每名玩家从N中减去B的任意幂次的数,直到不能操作判定为输。问谁最终能赢。

证明:b^x = k(b+1)+1或k(b+1)+b
b x = ( b + 1 1 ) x = i = 0 n C ( x , i ) ( b + 1 ) i ( 1 ) x = i = 0 n C ( n , i ) ( b + 1 ) i 1 ( 1 ) x ( b + 1 ) b^x = (b+1-1)^x = \sum_{i=0}^nC(x,i)(b+1)^i(-1)^x = \sum_{i=0}^nC(n,i)(b+1)^{i-1}(-1)^x(b+1)
到最后i=0时,剩下-1或1。

/*
b^x = k(b+1)+1或k(b+1)+b
*/ 
#include <iostream>
using namespace std;

int main()
{
	freopen("powers.in","r",stdin);
	int t;
	cin >> t;
	while( t-- )
	{
		int res = 0;
		int m;
		cin >> m;
		for (int i = 1; i <= m; i++)
		{
			int b,n;
			cin >> b >> n;
			n %= (b+1);
			if( n == b ) 
			{
				if( n % 2 == 0 ) res ^= 2; //此时的必胜态也可以选择失败,所以多组时为2,视为石子数为2的尼姆博弈; 
				else res ^= 1;
			}else if( n % 2 != 0 ) res ^= 1;
		}
		if( res ) cout << "1" << endl;
		else cout << "2" << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44316314/article/details/101862033