1299: [LLH Invitational] chocolate bar time limit: ten Sec Memory Limit: 162 MB
Submit: 420 Solved: 231
[Submit] [Status] [Discuss] Description
TBL and X play games with chocolate bars. Each time a person can take a number of chocolate bars out of the box, or eat a chocolate bar out of a positive integer length. TBL Two people take turns, unable to operate the loser. They made a total of 10 rounds (one box at a time) with the best strategy. Can you predict the outcome?
Input
Input data total 20 rows. Line 2i-1 A positive integer, Ni, which represents the number of chocolate bars in the first round. The 2nd i row ni positive integer li,j, indicating the length of the first round chocolate bar.
Output
Output data total 10 rows. Each line outputs "YES" or "NO" to indicate whether the TBL will win. Output "NO" if it wins, otherwise output "YES"
Sample Input3
11 10 15
5
13 6 7) 15 3
2
15 12
3
9 7 4
2
15 12
4
15 12 11 15
3
2 14 15
3
3 16 6
4
1 4 10 3
5
8 7 7) 5 12
Sample OutputYES
NO
YES
YES
YES
NO
YES
YES
YES
NO
HINT
20% of the score, n<=5,l<=100.
40% of the score, n<=7. 50% of the score, l<=5,000.
100% of the score, n<=14,l<=1,000,000,000.
Source
[Submit] [Status] [Discuss]
Puzzle: Game problem. Need to use the Nim game, Nim game is to have n heap of stones, each can choose any stone from a heap of stones, the last person failed to take. For the Nim game there is a simple conclusion: a[1]^a[2]^a[3]. ^a[n]=0 will defeat at this time, anyway, the initiator win.
Then this problem can actually be regarded as a NIM game. We found that if the initiator can choose M heap from the stone, so that his XOR and 0, is the remaining n-m heap no matter how to choose the same or not 0, will be different or and 0 of the largest sequence selected out, then the choice of M-heap is equivalent to a new NIM game, and if the back of the game from this heap is the equivalent of the Then he will fail. If you also new Nim game, and then elected to the X heap, because the choice of the XOR and must not be 0, then the whole game of the initiator will face the winning state, just to turn this NIM game into a must-lose state, left to the back.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath>using namespace Std;int n,m,a[100];bool p;void dfs (int x,int num,int ans) {if (x==n+1) {if (!ans&&num) p= True Return }dfs (X+1,num,ans); if (p) Return;dfs (x+1,num+1,ans^a[x]); if (P) return;} int main () {for (int t=1;t<=10;t++) {scanf ("%d", &n), and for (int i=1;i<=n;i++) scanf ("%d", &a[i]); p= False DFS (1,0,0); if (p) printf ("no\n"); else printf ("yes\n"); }}
Bzoj 1299: [LLH Invitational] Chocolate bar (the application of the Nim game)