The funny stone game is coming. There are n piles of stones,numbered with0, 1, 2,..., n - 1. Two persons pick stones in turn.In every turn, each person selects three piles of stones numberedi,j, k (i <j, j k and at least one stone left in pilei). Then, the person gets one stone out of pilei, and put onestone into pile j and pile k respectively. (Note: ifj = k, itwill be the same as putting two stones into pilej). One will failif he can't pick stones according to the rule.
David is the player who first picks stones and he hopes to win thegame. Can you write a program to help him?
The number of piles, n, does not exceed 23. The number of stonesin each pile does not exceed 1000. Suppose the opponent player isvery smart and he will follow the optimized strategy to pick stones.
Input
Input contains several cases. Each case has two lines. The firstline contains a positive integer n ( 1![$ \leq$](https://i-blog.csdnimg.cn/blog_migrate/7ec2c334cce735e047ea9ea59adda4ff.png)
![$ \leq$](https://i-blog.csdnimg.cn/blog_migrate/7ec2c334cce735e047ea9ea59adda4ff.png)
![$ \leq$](https://i-blog.csdnimg.cn/blog_migrate/7ec2c334cce735e047ea9ea59adda4ff.png)
![$ \leq$](https://i-blog.csdnimg.cn/blog_migrate/7ec2c334cce735e047ea9ea59adda4ff.png)
The last case is followed by a line containing a zero.
Output
For each case, output a line in the format `` Game t:i j k". t isthe case number. i, j and k indicates which three piles Davidshall select at the first step if he wants to win. If there aremultiple groups of i, j and k, output the group with theminimized lexicographic order. If there are no strategies to win thegame, i, j and k are equal to -1.Sample Input
4 1 0 1 100 3 1 0 5 2 2 1 0
Sample Output
Game 1: 0 2 3 Game 2: 0 1 1 Game 3: -1 -1 -1
有N堆石子,每次可以选3堆,i<j<=k,且第i堆至少剩一个石子,把第i堆的石子拿走一个,k,j堆各放一个,谁不能操作谁输。
假设第i堆上有一个石子,其它堆都没有,往左移的话它的子状态就是sg[j]^sg[k],j<i,k<i。
这个游戏就相当于很多这样的单个石子游戏的NIM和了,如果某一堆偶数个的话异或为0可以忽略,奇数个异或起来也就相当于一个。
要输出的话,求完SG再暴力一遍,找到满足操作完之后的SG为0的最小字典序操作。
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define eps 1e-9
#define MAXN 30
#define MAXM 110
#define MAXNODE 100010*27
#define MOD 999983
typedef long long LL;
using namespace std;
int N,g[MAXN],a[MAXN];
void init(){
int vis[100];
g[0]=0;
for(int i=0;i<=23;i++){
memset(vis,0,sizeof(vis));
for(int j=0;j<i;j++)
for(int k=0;k<=j;k++) vis[g[j]^g[k]]=1;
for(int j=0;;j++) if(!vis[j]){
g[i]=j;
break;
}
}
}
int main(){
freopen("in.txt","r",stdin);
int cas=0;
init();
while(scanf("%d",&N)!=EOF&&N){
printf("Game %d: ",++cas);
int sg=0;
for(int i=0;i<N;i++){
scanf("%d",&a[i]);
if(a[i]%2) sg^=g[N-i-1];
}
if(!sg){
printf("-1 -1 -1\n");
continue;
}
int find=0;
for(int i=0;i<N;i++) if(a[i]){
for(int j=i+1;j<N;j++){
for(int k=j;k<N;k++){
if(sg==(g[N-i-1]^g[N-j-1]^g[N-k-1])){
printf("%d %d %d\n",i,j,k);
find=1;
break;
}
}
if(find) break;
}
if(find) break;
}
}
return 0;
}