经典的NIM-poj-2975-Nim

简介: poj-2975-Nim 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 from an

poj-2975-Nim

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 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 n piles having k1, k2, …, kn stones respectively; in such a position, there are k1 + k2 + … + kn possible moves. We write each ki in binary (base 2). Then, the Nim position is losing if and only if, among all the ki’s, there are an even number of 1’s in each digit position. In other words, the Nim position is losing if and only if the xor of the ki’s is 0.

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

  111
1011
1101

There are an odd number of 1’s among the rightmost digits, so this position is not losing. However, suppose k3 were changed to be 12. Then, there would be exactly two 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 k1 = 7, k2 = 11, and k3 = 13. In fact, there are exactly three winning moves from this position: namely removing one stone from any of the three piles.

Input

The input test file will contain multiple test cases, each of which begins with a line indicating the number of piles, 1 ≤ n ≤ 1000. On the next line, there are n positive integers, 1 ≤ ki ≤ 1, 000, 000, 000, indicating the number of stones in each pile. The end-of-file is marked by a test case with n = 0 and should not 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

3

7 11 13

2

1000000000 1000000000

0

 Sample Output

3

0

大意:有n堆石头,甲乙两个人轮流拿,每次从某堆石头中拿出至少一个。若轮到某人时无石可拿,此人输。

当甲存在必赢策略时,并不意味着他从任意一堆里选石头都能赢。求出必赢时他有多少堆石头可以考虑。

目录
相关文章
|
4月前
【力扣】292. Nim 游戏
【力扣】292. Nim 游戏
LeetCode 292. Nim Game
你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头。 拿掉最后一块石头的人就是获胜者。你作为先手。 你们是聪明人,每一步都是最优解。 编写一个函数,来判断你是否可以在给定石头数量的情况下赢得游戏。
66 0
LeetCode 292. Nim Game
|
算法 Java C#
LeetCode刷题292-简单-Nim游戏
LeetCode刷题292-简单-Nim游戏
214 0
LeetCode刷题292-简单-Nim游戏
|
决策智能
LeetCode之Nim Game
LeetCode之Nim Game
115 0
[LeetCode] Nim Game
Haha, an interesting problem. Just try to let your opponent start with a number that is an integer multiple of 4.
861 0
|
索引
Nim教程【十】
openarray类型 注意:openarray类型只能用于参数 固定大小的数组虽然性能不错,但过于呆板,使用取来不是很方便 对于一个方法来说,传入参数如果是一个数组,最好是不要限制数组的长度 也就是说,方法应该能够处理不同大小的数组 openarray类型就是为了满足这样的要求而设计...
1103 0
|
索引
Nim教程【九】
向关注这个系列的朋友们,道一声:久违了! 它并没有被我阉掉,他一定会得善终的,请各位不要灰心 Set集合类型 为了在特殊场景下提高程序的性能设置了Set类型,同时也是为了保证性能,所以Set只能容纳有序类型, Set类型可以被一个大括号实例化: var x = {},x就是一个空的set类型 还...
907 0
|
机器学习/深度学习 数据格式
Nim教程【七】
这是国内第一个关于Nim的系列教程 先说废话 很开心,在今天凌晨快一点多的时候拿到了 nim-lang.com;nim-lang.cn;nim-lang.net 这三个域名,到不是为了投资,准备用nim-lang.
1095 0
|
索引 C# Rust
Nim教程【六】
目前看来这是国内第一个关于Nim的系列教程 先说废话         Rust1.0已经发布了,         国内有一个人为这个事情写了一篇非常长的博客,         这篇文章我前几天草草的看了一下,只记得这位朋友追Rust的艰辛,其他内容都已经记不清楚了         我觉得,...
1032 0