POJ2975|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
Source
Stanford Local 2005

分析:这题就很水了啊……水到我提交都很忐忑……题面直接告诉你异或为0就是必输局面了。那不就好办了,只要有一个ki大于其余所有石子的异或值,就能通过移动这一堆造成后手必输局面。

#include<iostream>
#include<cstdio>
using namespace std;

int a[1001];

int main()
{
    int n;
    while (scanf("%d",&n))
    {
          if (n==0) break;
          int ans=0;
          for (int i=1; i<=n; i++) scanf("%d",&a[i]);
          for (int i=1; i<=n; i++)
          {
              int now=0;
              for (int j=1; j<=n; j++)
                  if (i!=j) now^=a[j];
              if (a[i]>now) ans++;
          }
          cout << ans << endl;
    }
    return 0;
}
时间: 2024-11-05 22:21:38

POJ2975|Nim|博弈论的相关文章

POJ2975 Nim 博弈论 尼姆博弈

http://poj.org/problem?id=2975 题目始终是ac的最大阻碍. 问只取一堆有多少方案可以使当前局面为先手必败. 显然由尼姆博弈的性质可以知道需要取石子使所有堆石子数异或和为0,那么将某一堆a个石子变为a^异或和即可. a1^a2^a3^...^an=y; a1^a2^a3^...^an^y=0; 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<

poj2975 Nim(经典博弈)

Nim Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5866   Accepted: 2777 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 mor

[poj2975]Nim_博弈论

Nim poj-2975 题目大意:给定n堆石子,问:多少堆石子满足操作之后先手必胜. 注释:$1\le n\le 10^3$. 想法: 我们设M=sg(x1)^sg(x2)^...^sg(xn).其中,xi是第i堆石子个数. 如果sg(xi)^M<sg(xi),显然这堆石子满足题意. 最后,附上丑陋的代码... ... #include <iostream> #include <cstdio> #include <cstring> #include <al

POJ 1704 Georgia and Bob(nim博弈论)

题目地址:POJ 1704 这个题实在巧妙..居然这样就可以转化成了经典的nim模型. 这题可以从左往右两两配对,如果是奇数个的话,就让最左边的与0配对.然后每当对方移动某一对的前一个,你总可以移动该对的后一个来移动回来.所以这是没有影响的.有影响的只是每一对中间的空格数.这就转化成了((n+1)/2)堆石子的游戏,每一堆的石子个数是每一对点之间的空格数.然后用异或求解. 代码如下: #include <iostream> #include <cstdio> #include &l

POJ2975 Nim

Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u 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 fr

hdu 3032 Nim or not Nim? 博弈论,,,网上搜的题解让我大开眼界,原来还可以这样A题

Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1076    Accepted Submission(s): 532 Problem Description Nim is a two-player mathematic game of strategy in which players take tur

poj2975 Nim 博弈

      自从省赛结束了,好久都做过博弈题了,感觉都快忘了.今天找了几题练练手,在做过程中,感觉这道题挺有意思的.题目的意思是说,在Nim游戏中,先手有几种方式让 Nim 和变为0.(不知道Nim游戏的,请参考:这里) 其实我觉得这道题就是披着博弈的外衣,然后来考查你异或运算符(^)的使用的.在做题之前,我们想要了解异或运算符(^)的一个重要的性质: 现在我们有三个整数a, b, c: 我们假设c = a ^ b, 那么我们可以得到 b = c ^ a, a = b ^ c(交换律对异或运

poj2975(nim游戏取法)

求处于必胜状态有多少种走法. if( (g[i]^ans) <= g[i]) num++; //这步判断很巧妙 // // main.cpp // poj2975 // // Created by New_Life on 16/8/12. // Copyright © 2016年 chenhuan001. All rights reserved. // #include <iostream> #include <stdio.h> #include <string.h&g

BZOJ 4147 AMPPZ2014 Euclidean Nim 博弈论+数论

题目大意:给定n个石子,两人轮流操作,规则如下: 轮到先手操作时:若石子数<p,那么只能添加p个石子,否则可以拿走p的倍数个石子 轮到后手操作时:若石子数<q,那么只能添加q个石子,否则可以拿走q的倍数个石子 拿走所有石子的人胜利,问先手是否必胜,或输出游戏会永远进行下去 令d=gcd(p,q),那么若d不能整除n,游戏将会永远进行下去 否则将p/=d,q/=d,n/=d,显然不影响结果 然后我们讨论: 状态1.若p=q,先手必胜 不用解释吧= = 状态2.若p>q,n<p,那么先