原题链接
多堆的NIM问题就需要sg函数了,下面先给出打表程序
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn=100000 + 10;
int sg[500];//sg[i]是i不能转移到的状态的最小值
bool vis[500];
void deal(){
memset(sg,0,sizeof(sg));
for(int i=3;i<=500;i++){
memset(vis,0,sizeof(vis));
for(int j=1;j<=i;j*=2){
vis[sg[i-j]]=1;
}
for(int t=0;;t++){
if(!vis[t]){
sg[i]=t;
break;
}
}
}
}
int main(){
deal();
for(int i=0;i<500;i++)
cout << i << " " <<sg[i] <<endl;
}
首先根据sg函数的定义,sg[i]是i不能转移到的状态的最小值,所以sg[i-j]就是i-j不能转移到的状态的最小值,所以vis[sg[i-j]]=说明就代表i-j不能转移到的状态的最小值(即i能转移到的状态的最小值)都访问过了。所以对于后面的循环
for(int t=0;;t++)
if(!vis[t]) sg[i]=t;
这也就是说在剩下的不能到达的状态中找到那个最小值即可。
而对于每一个人面对m局面时,他总要进行操作,所以m局面就是他永远都无法到达的,所以这样不断地推下去就可以用到达不了的最小局面来代替了
所以还是先进行打表,同样也是发现了规律,所以最后把这些sg值异或起来即可
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
//const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
//const DB EPS = 1e-9;
//const DB OO = 1e20;
//const DB PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=100000 + 10;
int main(){
int n;
while(scanf("%d",&n)==1){
int x=0,t;
for(int i=0;i<n;i++){
scanf("%d",&t);
if(t%3==0) x^=0;
else if(t%3==1) x^=1;
else if(t%3==2) x^=2;
}
if(x==0) printf("No\n");
else printf("Yes\n");
}
return 0;
}