Poj 2975 Nim Nim game, winning scheme number

Source: Internet
Author: User

Nim
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5002 Accepted: 2313

Description

Nim is a 2-player game featuring several piles of stones. Players alternate turns, and on his/her turn, a player's move consists of removing  one or more stones  f Rom any single pile. Play ends when all the stones has been removed, at which point the last player to has moved is declared the winner. Given a position in Nim, your task was to determine how many winning moves there was in that position.

A position in Nim was called "losing" if the first player to move from that position would lose if both sides played per fectly. A "winning move," Then, was a move that leaves the game in a losing position. There is a famous theorem this classifies all losing positions. Suppose a Nim position contains  n  piles having  k 1,  k 2, ...,   kn  stones respectively; In such a position, there are  k 1 +  k 2 + ... +  kn  possible moves . We write each  ki  in binary (base 2). Then, the Nim position was losing if and only if, among all the  ki ' s, there was an even number of 1 's in each Digit position. In other words, the Nim position are losing if and only if the  xor  of the  ki 's is 0.

Consider the position with three piles given by k1 = 7, k2 = one, and k3 = 13. In binary, these values is as follows:

There is a odd number of 1 ' s among the rightmost digits, so this position was not losing. However, suppose K3 were changed to be 12. Then, there would is exactly 1 ' in each digit position, and thus, the Nim position would become losing. Since A winning move is an any move this leaves the game in a losing position, it follows this removing one stone from the th IRD Pile is a winning move when k1 = 7, k2 = one, and k3 = 13. In fact, there is exactly three winning moves from this position:namely removing one stone from any of the three piles.

Input

The input test file would contain multiple test cases, each of the which begins with a line indicating the number of piles, 1≤ n ≤1000. On the next line, there is n positive integers, 1≤ ki ≤ 1, $, $, indicating the number of stones in EA CH Pile. The End-of-file is marked by a test case with n = 0 and should isn't be processed.

Output

For each test case, write a single line with an integer indicating the number of winning moves from the given Nim position .

Sample Input

37 11 1321000000000 10000000000

Sample Output

30
Recommended Blog: http://blog.csdn.net/lionel_d/article/details/43939605
Code:
#include <stdio.h>typedef long ll, LL c[1010]; int main () {int n; while (~scanf ("%d", &n) && N) {A t i = 0; I < n; ++i) {scanf ("%i64d", &c[i]);} int ans = 0; for (int i = 0; i < n; ++i) {ll flag = 0, for (int j = 0; J < N; ++j) {if (J! = i) {flag ^= c[j];} if (C[i]>flag) {++ans;}} printf ("%d\n", ans);} return 0;}


With June

Poj 2975 Nim Nim game, winning scheme number

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.