博弈取石子总结

1.一堆石子共有n个,A先手拿,B后手,要求每次最少拿1,最多拿k,拿到最后一个的获胜
如果n <= k,A胜!
如果n%(k+1)==0,B胜!
否则A胜

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n,k;
	cin >> n >> k;
	if(n <= k)
	{
	 cout << "A" << endl;
	 continue;
	}
	if(n % (k+1) == 0) cout << "B" << endl;
	else cout << "A" << endl;
	return 0;
}

2.一堆石子共有n个,A先手,B后手,要求每一次只能拿1.3.或者4个,拿到最后一个的获胜
如果n%7等于0或2,A胜!
否则B胜!

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	if(n % 7 == 0 || n % 7 == 2)
	cout << "A" << endl;
	else cout << "B" << endl;
	return 0;
}

3.尼姆博弈!!!
n堆石子,A先手,每次在一堆里面取任意,不可不取,拿到最后一个获胜

所有石子堆的石子个数的异或和:a[0] xor a[1] xor … a[n],异或和为0,则B胜
异或和不为0,A胜

#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 50;
int a[maxn];
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	int ans = 0;
	for(int i = 0; i < n; i++)
	{
		cin >> a[i];
		ans ^= a[i];
	}
	if(ans == 0) cout << "B" << endl;
	else
	cout << "A" << endl;
	return 0;
}

4.威佐夫博弈!!!
有两堆石子,A先手,B后手,每次在两堆石子取相同个数或者只在一堆取,不可不取,拿到最后一个获胜

两堆个数为a,b,交换使得a小于等于b
计算 int t = (int)((1.0+sqrt(5.0)) / 2.0 * (b-a))
如果t等于a,B胜
如果t等于b,A胜

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int a,b;
	cin >> a >> b;
	int t  = (int)((1.0 + sqrt(5.0)) / 2.0 * (b-a));
	if(t == a) cout << "B" << endl;
	else cout << "A" << endl;
	return 0;
}

5.斐波那锲博弈!!!
一堆石子有n个,A先手,B后手,先手第一次可以随便拿(但是不可不拿,不可全拿),之后每一次拿最少拿1个,最多不超过上一次拿的2倍,拿到最后一个石子获胜

如果n为斐波那锲数,那么先手必败,B获胜
否则A胜

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 1e5 + 50;
int a[maxn];
int vis[maxn] = {0};
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	a[0] = 1;
	a[1] = 1;
	vis[1] = 1;
	for(int i = 2; i < maxn; i++)
	{
		a[i] = a[i-1] + a[i+1];
		vis[a[i]] = 1;
	}
	if(vis[n]) cout << "B" << endl;
	else cout << "A" << endl;
	return 0;
}

留着给自己看,奥利给!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值