Previous Examination Questions Monk wits
Time limit: 1.0s memory limit: 256.0MB
Problem description
Ancient funeral activities often asked the monks to practice things. At the end of the ceremony, there are sometimes "monk wits" fun programs to relieve the oppressive atmosphere.
The program is in the following steps: First use grain (usually rice) on the ground "painting" a number of steps (indicating the N-level pagoda). There are also a number of small monks randomly "stand" on a certain step. The highest level of stairs must stand people, others arbitrarily. (as shown in 1)
The two masters of the game commanded a small monk to go up the steps of any level, but will be standing on the high steps of the little monk block, can not be crossed. Neither of the two young monks can stand on the same steps, nor move to the lower steps.
The two wizards take turns issuing instructions, and eventually all the young monks will be squeezed in high steps, and can no longer move upward. When it was the master's turn to be unable to move on, the master conceded that the game was over.
For the known number of steps and the location of the small monk, please calculate how the first-order mage should make the decision to win.
Input format
The input data is a row of n integers separated by spaces, indicating the location of the little monk. The step number is counted from 1, so the last little monk's position is the total number of steps. (n<100, number of steps <1000)
Output format
The output is a line of two integers separated by a space: a B, which means to move the small monk in position a to position B. If there is more than one solution, the output of a small solution of a value, if no solution is output-1.
Sample input
1 5 9
Sample output
1 4
Sample input
1 5 8 10
Sample output
1 3
This problem uses the NIM to take the child game deformation!
These are the information of the NIM game:
Nimbo (Nimm game): There are three piles of various items, two people take turns from a heap of any number of items, the provisions of at least one at a time, more than unlimited, the last person to win the light. This is the most interesting situation, it is closely related to the binary, we use (A,B,C) to represent a situation, first (0,0,0Is obviously a strange situation, no matter who face the strange situation, will inevitably fail. The second singular situation is (0, N,n), as long as you take away as many items as your opponent will eventually cause (0,0,0)。 Careful analysis, (1,2,3) is also a singular situation, no matter how the opponent takes, the next can be changed (0, N,n). There is a computer algorithm called a bitwise mode2Add, also called XOR operation, we use the symbol (+) to denote this operation. The difference between this operation and the general addition is that1+1=0。 Look First (1,2,3), the bitwise mode2The result of the addition:1= Binary on2= BinaryTen3= Binary One(+) ———————0= Binaryxx(Note not carry) for singular situations (0, N,n), as well as the result0。 Any singular situation (a,b,c) has a (+) b (+) C =0。 If we are faced with a non-singular situation (a,b,c), how to become a singular situation? Suppose a < b< C, we just change C to a (+) B, because we have the following results: A (+) b (+) (A (+) b) = (A (+) a) (+) (b (+) b) =0+0=0。 To change C to a (+) b, simply subtract C-(a (+) b). Cases1。 ( -, +, the), -+ += -, the- -= A, so from theTaken from Aobjects to a singular situation ( -, +, -)。 Cases2。 ( -,Bayi,121), -+Bayi=102,121-102= +, so from121Taken from +A singular situation was created by an item ( -,Bayi,102)。 Cases3。 ( in, $, -), in+ $= -, -- -=TenFrom -Taken fromTenOne, which becomes ( in,45, -)。 Cases4。 Let's actually do a game to see: A: (7,8,9)(1,8,9) Singular situation B: (1,8,9)(1,8,4Methyl (1,8,4)(1,5,4) Singular situation B: (1,5,4)(1,4,4Methyl (1,4,4)(0,4,4) Singular situation B: (0,4,4)(0,4,2Methyl (0.4,2)(0,2,2) Singular situation B: (0,2,2)(0,2,1Methyl (0,2,1)(0,1,1) Singular situation B: (0,1,1)(0,1,0Methyl (0,1,0)(0,0,0Singular situation a wins.
In the face of strange situation will be defeated!
The hardest part of this problem is to construct the model of the sub-game.
For 1 5 8 10 This example, we divided 1 5 into a group, 8 10 divided into a group, we found that no matter 5 how to move forward, the back of the 1 followed up on it!
However, 8 how to move, 5 to move, and then 1 to move along, so say:
**
It doesn't make sense for you to move 8!
**
So we just need to analyze how the second of each group moves!
about how to get the smallest solution, that is, direct enumeration, from the smallest case, if the Nim answer is 0, then you move this step, the other side in the face of strange situation, will be defeated!
The output!
#include <cstdio>#include <cstring>Const intmaxn=1005;intA[MAXN];intB[MAXN];intC[MAXN];intd[ the];Charstr[maxn<<1];BOOLSolveintN) {memset(b,0,sizeof(b));intcoun=0; for(intI=1; i<=n;++i) {if(A[i]) d[coun++]=i;//printf ("%d", d[coun-1]);}//Putchar (ten);d[coun]=d[coun-1]+1;intans=0; for(intI=1; i<=coun;i+=2) {ans^= (d[i]-d[i-1]-1); }//Putchar (ten); returnans==0;}intMain () {gets (str);intlen=strlen(str);intcoun=0; for(intI=0;i<len;) { while(str[i]<' 0 '|| Str[i]>' 9 ') {++i; }intt=0; for(; i<len;++i) {if(str[i]>=' 0 '&&str[i]<=' 9 ') {t=t*Ten+str[i]-' 0 '; }Else Break; } a[coun++]=t; }intn=a[coun-1]; a[coun]=a[coun-1]+1;intans=0; for(intI=1; i<=coun;i+=2) {b[i]=a[i]-a[i-1]-1; Ans^=b[i]; }//printf ("ans=%d\n", ans); if(!ans) {printf(" -1\n"); }Else{memsetC0,sizeof(c)); for(intI=0; i<coun;++i) {c[a[i]]=1; }//printf ("n=%d\n", n); BOOLans=true; for(intI=1; i<=n&&ans;++i) {memcpy(A,c,sizeof(c)); for(intj=i+1; j<=n&&ans;++j) {if(!a[j]) {//printf ("i=%d j=%d\n", i,j);a[i]=0; a[j]=1;if(Solve (n)) {printf("%d%d\n", i,j); ans=false; Break; } a[i]=1; a[j]=0; }Else Break; } } }return 0;}
Blue Bridge Cup of the previous Test monk wits