eolymp
bolt
Try our new interface for solving problems

Nim

Nim is a \textbf{2}-player game featuring several piles of stones. Players alternate turns, and on his/her turn, a player’s move consists of removing \textit{one or more stones} from any single pile. Play ends when all the stones have been removed, at which point the last player to have moved is declared the winner. Given a position in Nim, your task is to determine how many winning moves there are in that position. A position in Nim is called "losing" if the first player to move from that position would lose if both sides played perfectly. A "winning move", then, is a move that leaves the game in a losing position. There is a famous theorem that classifies all losing positions. Suppose a Nim position contains \textbf{n} piles having \textbf{k_1}, \textbf{k_2}, …, \textbf{k_n} stones respectively; in such a position, there are \textbf{k_1} + \textbf{k_2} + … + \textbf{k_n} possible moves. We write each \textbf{k_i} in binary (base \textbf{2}). Then, the Nim position is losing if and only if, among all the \textbf{k_i}’s, there are an even number of \textbf{1}’s in each digit position. In other words, the Nim position is losing if and only if the \textbf{xor} of the \textbf{k_i}’s is \textbf{0}. Consider the position with three piles given by \textbf{k_1} = \textbf{7}, \textbf{k_2} = \textbf{11}, and \textbf{k_3} = \textbf{13}. In binary, these values are as follows: \begin{verbatim} 11110111101\end{verbatim}There are an odd number of \textbf{1}’s among the rightmost digits, so this position is not losing. However, suppose \textbf{k_3} were changed to be \textbf{12}. Then, there would be exactly two \textbf{1}’s in each digit position, and thus, the Nim position would become losing. Since a winning move is any move that leaves the game in a losing position, it follows that removing one stone from the third pile is a winning move when \textbf{k_1} = \textbf{7}, \textbf{k_2} = \textbf{11}, and \textbf{k_3} = \textbf{13}. In fact, there are exactly three winning moves from this position: namely removing one stone from any of the three piles. \InputFile The input test file will contain multiple test cases, each of which begins with a line indicating the number of piles, \textbf{1} ≤ \textbf{n} ≤ \textbf{1000}. On the next line, there are \textbf{n} positive integers, \textbf{1} ≤ \textbf{k_i} ≤ \textbf{1, 000, 000, 000}, indicating the number of stones in each pile. The end-of-file is marked by a test case with \textbf{n} = \textbf{0} and should not be processed. \OutputFile For each test case, write a single line with an integer indicating the number of winning moves from the given Nim position.
Time limit 1 second
Memory limit 64 MiB
Input example #1
3
7 11 13
2
1000000000 1000000000
0
Output example #1
3
0