BashGame I
有一堆石子共有N个。A B两个人轮流拿,A先拿。每次最少拿1颗,最多拿K颗,拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N和K,问最后谁能赢得比赛。
例如N = 3,K = 2。无论A如何拿,B都可以拿到最后1颗石子。
#include<iostream>
#include<cstring>
using namespace std;
int N,K,T;
int main()
{
cin.sync_with_stdio(false);
cin>>T;
while(T--)
{
cin>>N>>K;
if (N<=K)
{
cout<<"A\n";
continue;
}
else
{
N%=(K+1);
if (N==0)
cout<<"B\n";
else
cout<<"A\n";
}
}
return 0;
}
BashGame II
有一堆石子共有N个。A B两个人轮流拿,A先拿。每次只能拿1,3,4颗,拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N,问最后谁能赢得比赛。
例如N = 2。A只能拿1颗,所以B可以拿到最后1颗石子。
#include<iostream>
#include<cstring>
using namespace std;
int N,T;
int main()
{
cin.sync_with_stdio(false);
cin>>T;
while(T--)
{
cin>>N;
N%=7;
if (N==0||N==2)
cout<<"B\n";
else
cout<<"A\n";
}
return 0;
}
BashGame III
有一堆石子共有N个。A B两个人轮流拿,A先拿。每次拿的数量只能是2的正整数次幂,比如(1,2,4,8,16....),拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N,问最后谁能赢得比赛。
例如N = 3。A只能拿1颗或2颗,所以B可以拿到最后1颗石子。(输入的N可能为大数)
#include<iostream>
#include<cstring>
using namespace std;
int T;
char N[1005];
int main()
{
cin.sync_with_stdio(false);
cin>>T;
while(T--)
{
cin>>N;
int sum=0,len=strlen(N);
for (int i=0;i<len;i++)
sum+=N[i]-'0';
if (sum%3==0)
cout<<"B\n";
else
cout<<"A\n";
}
return 0;
}
BashGame IV
有一堆石子共有N个。A B两个人轮流拿,A先拿。每次拿的数量最少1个,最多不超过对手上一次拿的数量的2倍(A第1次拿时要求不能全拿走)。拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N,问最后谁能赢得比赛。
例如N = 3。A只能拿1颗或2颗,所以B可以拿到最后1颗石子。
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=1e9+5;
int T,Cnt=1;
long long N,F[100];
int main()
{
cin.sync_with_stdio(false);
F[0]=F[1]=1;
for (int i=2;i<100&&F[i-1]<MAX;i++)
F[i]=F[i-1]+F[i-2],Cnt=i+1;
cin>>T;
while(T--)
{
cin>>N;
if (binary_search(F,F+Cnt,N))
cout<<"B\n";
else
cout<<"A\n";
}
return 0;
}
WythoffGame I
有2堆石子。A B两个人轮流拿,A先拿。每次可以从一堆中取任意个或从2堆中取相同数量的石子,但不可不取。拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出2堆石子的数量,问最后谁能赢得比赛。
例如:2堆石子分别为3颗和5颗。那么不论A怎样拿,B都有对应的方法拿到最后1颗。
//https://www.51nod.com/onlineJudge/questionCode.html
//证明:http://baike.baidu.com/item/%E5%A8%81%E4%BD%90%E5%A4%AB%E5%8D%9A%E5%BC%88/19858256?fr=aladdin&fromtitle=%E5%A8%81%E4%BD%90%E5%A4%AB%E5%8D%9A%E5%A5%95&fromid=7139745&type=syn
using namespace std;
int T,N,M;
/*
威佐夫博弈:
两个人如果都采用正确操作,那么面对非奇异局势,先拿者必胜;反之,则后拿者取胜.
局势(N,M)(N<M)满足N==(int)((1.0+sqrt(5.0))/2.0*(M-N))时,该局势为奇异局势,后拿者必胜.
*/
int main()
{
cin.sync_with_stdio(false);
cin>>T;
while(T--)
{
cin>>N>>M;
if (N>M) swap(N,M);
if (N==(int)((1.0+sqrt(5.0))/2.0*(M-N)))
cout<<"B\n";
else
cout<<"A\n";
}
return 0;
}
NimGame
有N堆石子。A B两个人轮流拿,A先拿。每次只能从一堆中取若干个,可将一堆全取走,但不可不取,拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N及每堆石子的数量,问最后谁能赢得比赛。
例如:3堆石子,每堆1颗。A拿1颗,B拿1颗,此时还剩1堆,所以A可以拿到最后1颗石子。
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int N,Ans,A;
int main()
{
cin.sync_with_stdio(false);
cin>>N;
for (int i=0;i<N;i++)
{
cin>>A;
if (!i)
Ans=A;
else
Ans^=A;
}
if (Ans)
cout<<"A\n";
else
cout<<"B\n";
return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
const int MAX=1e3+5;
int M,N,P,F[50],SG[MAX],S[MAX];
void GetSG(int n)
{
memset(SG,0,sizeof(SG));
for (int i=1;i<=n;i++)
{
memset(S,0,sizeof(S));
for (int j=0;F[j]<=i&&j<50;j++)
S[SG[i-F[j]]]=1;
for (int j=0;j<=n;j++)
if(!S[j])
SG[i]=j,j=n+123;
}
}
int main()
{
cin.sync_with_stdio(false);
F[0]=F[1]=1;
for (int i=2;i<50;i++)
F[i]=F[i-1]+F[i-2];
GetSG(MAX);
while(cin>>M>>N>>P&&(M||N||P))
{
if(SG[M]^SG[N]^SG[P])
cout<<"Fibo\n";
else
cout<<"Nacci\n";
}
return 0;
}