Puzzle Report: Hdu 1850 Being A good boy in Spring Festival (NIM game)

Source: Internet
Author: User
Tags bitwise

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1850

Problem description A year outside parents time
Spring Festival Home Can you do a few days good boy?
Try to do the following things in winter vacation
Spend time with mom on the vegetable
Quietly buy a little gift for dad
Take the initiative to ask for a bowl wash once and for all
One day get up early and give mom and dad a heart for breakfast.
You can talk to Mom and dad if you want.
Let's play a little game. ACM Class to School ~
Here is a two-person mini-game: There are m stacks of poker on the table, and the number of cards per pile is ni (i=1 ... M); The two take turns, and each step can be arbitrarily selected and taken away from any of the cards; the poker on the table is all out, then the game is over; the last person to pick the card is the winner.
Now we do not want to study whether to win or negative, I just want to ask you:
——“If you want to win, there are several choices in the first step.It? The input data contains multiple test cases, each of which occupies 2 rows, and the first line contains an integer m (1<m<=100), which represents the number of stacks of poker, followed by a row containing M integers Ni (1<=ni<=1000000,i=1 ... m), representing the number of M-Heap poker, respectively. An M of 0 indicates the end of the input data. Output if the initiator can win, please export his first feasible number of scenarios, otherwise please output 0, the output of each instance takes up one row. Sample Input35 7 90Sample Output1 problem-solving ideas: reference to Baidu Encyclopedia: Nim Game The typical NIM game, the model is: there are three piles (or M-heap) of several 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 to win the light. This is the most interesting situation, it is closely related to the binary system, we use (a,b,c) to denote a situation, first (0,0,0) is clearly a singular situation, no matter who face the singular situation, will inevitably fail. The second singular situation is (0,n,n), as long as the opponent takes away as many items as the final result (0,0,0). In a careful analysis, it is also a singular situation, no matter how you take it, and then the opponent can turn it into a (0,n,n) situation. there is a computer algorithm called the bitwise modulo 2 Plus, also known as the XOR operation, we use the symbol (+) to denote this operation, first look at (three-way) bitwise modulo 2 plus results: 1 = binary 2 = binary ten 3 = binary one (+) ——————— 0 = binary 00 (note no rounding) As for the singular situation (0,n,n), the result is also 0. 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) (the focus of the problem ) .
Example 1: (14,21,39), 14 (+) 21=27,39-27=12, so a singular situation (14,21,27) can be achieved by taking 12 objects from 39.
Example 2: (55,81,121), 55 (+) 81=102,121-102=19, so take 19 items from 121 to form a singular situation (55,81,102).
Example 3: (29,45,58), 29 (+) 45=48,58-48=10, removed 10 from 58, changed to (29,45,48).
Example 4: Let's actually take a game to see:
Krabi 7,8,9 (1,8,9) Strange Situation
b 1,8,9) (1,8,4)
Krabi 1,8,4 (1,5,4) Strange Situation
b 1,5,4) (1,4,4)
Krabi 1,4,4 (0,4,4) Strange Situation
b 0,4,4) (0,4,2)
Krabi 0.4,2 (0,2,2) Strange Situation
b 0,2,2) (0,2,1)
Krabi 0,2,1 (0,1,1) Strange Situation
b 0,1,1) (0,1,0)
Krabi 0,1,0 (0,0,0) Strange Situation
Jia Sheng. Conclusion: ①a1^a2^......^an==0, the hand will win, ② if a1^a2^...^an!=0, there must be a legitimate move, the AI changed into AI ' A1^a2^...^ai ' ^...^an=0. If S=a1^a2^...^an, there must be an AI, so that (s^ai<ai) must be set up, then we can change the AI to Ai ' =ai^k, then a1^a2^ ^ai ' ^...^an=a1^a2^...^an^s=0, the situation into ① state, then the initiator will win.

AC Code:
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN = the;4 intM,ANS,CNT,A[MAXN];5 intMain ()6 {7      while(Cin>>m &&m) {8Ans=cnt=0;9          for(intI=0; i<m;++i) {TenCin>>A[i]; Oneans^=A[i]; A}//all the numbers are different or up, there is ans inside -          for(intI=0; i<m;++i) { -             if((Ans^a[i]) <A[i]) thecnt++; -}//here the ANS and a[i] xor, you can get out of a[i] all the number of different or results. If this result is less than a[i], then as long as a certain value in the a[i], you can form a singular situation, the initiator will win -cout<<cnt<<Endl; -     } +     return 0; -}

Puzzle Report: Hdu 1850 Being A good boy in Spring Festival (NIM game)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.