3895: Take stone time limit:1 Sec Memory limit:512 MB
submit:263 solved:127
[Submit] [Status] [Discuss] Descriptionalice and Bob two good friends and began to play with stones. When the game starts, there are n heaps of stones in a row, and then they take turns (Alice Initiator), and each action is selected from one of the following rules: • Remove one of the stones from a heap • A person who merges any two piles cannot be manipulated to lose. Alice wants to know if she can win the strategy. Input the first line of T, indicating the number of data groups. For each set of test data, the first line reads N. The next n positive integers a1,a2...an, representing the number of stones per heap. Output one row for each set of test data. Output yes indicates Alice has a winning strategy, output no indicates Alice has no winning strategy. Sample Input2
3
1 1 2
2
3 4
3
2 3 5Sample OutputYES
NO
NOhint100% data Meet t<=100, n<=50. Ai<=1000sourcesolution
Conclusion + Memory Search
First of all, hand play or feel, very well found: the total number of operations S=$\sum a_{i}+n-1 .... (x>1) $
This further finds that if S is an odd number, it is clear that the initiator is winning
Prove:
Difficult to prove that s is an odd number of wins, consider proof S is even the initiator will fail;
First if only a bunch, for odd, obviously the initiator win;
1. If the initiator chooses to merge two piles of stones, then the number of stones per heap is still greater than 1,x becomes odd.
2. If the initiator chooses to take a stone from a heap of stones greater than 2, the number of stones above each heap is still greater than 1,x to odd.
3. If the initiator chooses to take a stone from a heap of stones equal to 2, then the next can merge the remaining 1 stones into any heap. So the parity of X is constant, the number of stones per heap is still greater than
So you can get the situation for all x>1, then consider the existence of x=1
Can be divided into 4 scenarios:
1. Take a pebble from a heap with a size of 1.
2. Merge a heap with a size of 1 and a heap with a size not 1
3. Merge two heaps of size 1.
4. Combine or take a pebble operation on a heap with a size not 1.
Then we use OK[I][J] to represent the state, indicating that there are only 1 stones with the I heap, the total number of gravel heap is s
In this case, it is obvious that memory search can be used to transfer
Consider that the maximum value of the data is not large, so all data can be passed.
Code
#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>using namespaceStd;inlineintRead () {intw=0;CharCh=GetChar (); while(ch<'0'|| Ch>'9') ch=GetChar (); while(ch>='0'&& ch<='9') w=w*Ten+ch-'0', ch=GetChar (); returnW;}#defineMAXN 100#defineint Register intBOOLok[ -][50060],visit[ -][50060];inlineBOOLDFS (intXinty) { if(!x)returny&1; if(y==1)returnDFS (x+1,0); if(Visit[x][y])returnOk[x][y]; Visit[x][y]=1; if(x>=2&& y &&! DFS (X-2, y+3)) {ok[x][y]=1;return 1;} if(x>=2&&!y &&! DFS (X-2, y+2)) {ok[x][y]=1;return 1;} if(x && y &&!) DFS (X-1, y+1)) {ok[x][y]=1;return 1;} if(Y &&!) DFS (x,y-1)) {ok[x][y]=1;return 1;} if(x &&!) DFS (X-1, y)) {ok[x][y]=1;return 1;} Ok[x][y]=0; return 0;} Main () {intn,a[maxn],t,sum; T=read (); while(t--) {N=read (); BOOLflag=1; Sum=0; for(intI=1; i<=n; i++) {A[i]=read (), sum+=A[i]; if(a[i]==1) flag=0; } if(flag)if((sum+n-1) &1) puts ("YES");ElsePuts"NO"); Else { intx=0, y=0; for(intI=1; i<=n; i++) if(a[i]==1) X+=a[i];Elsey+=a[i]+1; if(DFS (X,max (Y-1,0))) puts ("YES");ElsePuts"NO"); } }//return 0;}
Sunshine Seniors day3t2 ....
Test, the hands played several groups, found the conclusion, so casually and Dcrusher uncle said
Be Dcrusher an education
Uncle Education I said, a random search on the out, and then rolled away with 10 minutes to write a note search.
The hand is wrong to write a place, the card became 80 .... Beiyu Uncle Mark card into 50 ...
Dcrusher Strength abuse Field Orz
"BZOJ-3895" Take the stone memory search + game