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;
}
留着给自己看,奥利给!!!