David plays a stone game. In the game, there are n piles of stones numbered 0 .. n-1. The two players take stones in turn. In each round of game, each player selects three piles of stones I, J, K (I <j, j <= K, and at least one stone is in the I heap stone ), take a stone from I and place it into J and K (if J = K, put 2 stones into K ). The first person to lose the stone. The number of stone heaps cannot exceed 23, and each pile of stones cannot exceed 1000.
Solution: It seems that all stones are moved to the right until all stones reach the n-1 heap. First, consider that each heap of stones is actually an independent sub-game with no mutual influence between the heap and the heap. Then, if the number is an even number, it will not affect the winning and losing State. The winning state cannot be reversed by moving the even number of stones, because the winning state only requires symmetric operations. Therefore, each heap of stones becomes the 01 state. The SG value is only related to the position. Pre-process the SG value at each position. When calculating the first feasible step, you can determine ijk brute force.
Code:
/******************************************************* @author:xiefubao*******************************************************/#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#include <vector>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <string.h>//freopen ("in.txt" , "r" , stdin);using namespace std;#define eps 1e-8#define zero(_) (_<=eps)const double pi=acos(-1.0);typedef long long LL;const int Max=100010;const LL INF=0x3FFFFFFF;int sg[100];bool rem[100];int n;void init(){ sg[0]=0; for(int i=1; i<=25; i++) { memset(rem,0,sizeof rem); for(int j=i-1; j>=0; j--) for(int k=j; k>=0; k--) { rem[sg[j]^sg[k]]=1; } int t=0; while(rem[t]) t++; sg[i]=t; }}bool help[100];//0 1 2 4 7 8 11 13 14 16 19 21 22 25 26 28 31 32 35 37 38 41 42int main(){ init(); int kk=1; while(cin>>n&&n) { memset(help,0,sizeof help); int ans=0; for(int i=0; i<n; i++) { int a; scanf("%d",&a); if(a)help[i]=1; if(a&1) { ans^=sg[n-1-i]; } } printf("Game %d: ",kk++); if(ans) { for(int i=0; i<n-1; i++) { if(help[i]) { for(int j=i+1; j<n; j++) for(int k=j; k<n; k++) { if((ans^sg[n-1-i]^sg[n-1-j]^sg[n-1-k])==0) { printf("%d %d %d\n",i,j,k); goto end; } } } }end: ; } else puts("-1 -1 -1"); } return 0;}
The game of a 1378-a funny stone game SG