POJ - 2975 Nim

我们之前的\(Nim\)游戏都已经知道当\(Nim\)和为\(0\)时是必败的。

那我们就刻意制造这种情况。因为一次只能改一堆石头,不能制造几个数异或消去\(sum\)的情况,所以只用考虑将\(a[i]\)替换为\(a[i]?sum\)的情况就行了。

注意要保证\(a[i]?sum<a[i]\)。

#include<cstdio>
#include<cstdlib>

int n, a[1002], sum, ans;

int read() {
    int x = 0, f = 1; char s;
    while((s = getchar()) > '9' || s < '0') {
        if(s == '-') f = -1;
        if(s == EOF) exit(0);
    }
    while(s >= '0' && s <= '9') {
        x = (x << 1) + (x << 3) + (s ^ 48);
        s = getchar();
    }
    return x * f;
}

int main() {
    while(n = read(), n) {
        sum = ans = 0;
        for(int i = 1; i <= n; ++ i) a[i] = read(), sum ^= a[i];
        for(int i = 1; i <= n; ++ i) if((sum ^ a[i]) < a[i]) ++ ans;
        printf("%d\n", ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/AWhiteWall/p/12334900.html

时间: 2024-12-07 18:35:56

POJ - 2975 Nim的相关文章

[原博客] POJ 2975 Nim 统计必胜走法个数

题目链接题意介绍了一遍Nim取石子游戏,可以看上一篇文章详细介绍.问当前状态的必胜走法个数,也就是走到必败状态的方法数. 我们设sg为所有个数的Xor值.首先如果sg==0,它不可能有必胜走法,输出0. 对于任意一堆有a[i]个石子,若sg Xor a[i] <= a[i] ,那么我们就可以在a[i]里面取出sg Xor a[i]个石子,使得剩下石子Xor和为0,于是ans++.然后输出ans. 注意C/C++语言中^操作比<操作优先级低. #include<iostream> #

poj 2975 Nim(博弈)

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

poj 2975 Nim 尼姆博弈,求取胜方案数

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 mor

POJ 2975 Nim(普通nim)

题目链接 #include<iostream> #include<cstdio> using namespace std; int main() { int n,k[1005]; int sum,cnt; while(scanf("%d",&n)&&n) { sum=0,cnt=0; for(int i=1;i<=n;i++) { scanf("%d",&k[i]); sum^=k[i]; } if(su

POJ 2975 Nim 尼姆博弈

题目大意:尼姆博弈,求先手必胜的情况数 题目思路:判断 ans=(a[1]^a[2]--^a[n]),求ans^a[i] < a[i]的个数. #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<stdio.h> #include<stdlib.h> #include<queue> #include<

POJ 2975

题面:http://poj.org/problem?id=2975 本题就是nim游戏的一个变试,因为我们知道nim游戏中每次会取ai xor x<ai(x=a1 xor a2 xor ... xor aN的那堆石子,然后就能造成必胜局,所以只要统计有多少个满足ai xor x<ai的i即可. Code: #include<iostream> #include<cstdio> #include<cmath> #include<cstdio> #i

HDU 3404&amp;POJ 3533 Nim积(二维&amp;三维)

(Nim积相关资料来自论文曹钦翔<从"k倍动态减法游戏"出发探究一类组合游戏问题>) 关于Nim积计算的两个函数流程: 代码实现如下: int m[2][2]={0,0,0,1}; int Nim_Multi_Power(int x,int y) { if(x<2) return m[x][y]; int a=0; for(;;a++) if(x>=(1<<(1<<a))&&x<(1<<(1<<

POJ 2068 Nim

链接: http://poj.org/problem?id=2068 题意: 传统的Nim游戏由两名玩家进行,在一堆石头中,双方轮流取走任意合法数量块石头,取走最后一块石头的玩家落败. 多人Nim游戏将参赛人数拓展至两个队伍,每支队伍有n名队员交错入座,单次分别能最多取走Mi块石头,取走S块石头中的最后一块的队伍失败, 求第一支队伍是否有必胜策略? 题解: dp[i][j]表示第i个人取,还有j块石头 . 当j为0的时候,没有石头,这时候是胜,为1. 后继中有必败态的为必胜态. 代码: 31 i

POJ 2068 Nim#双人dp博弈

http://poj.org/problem?id=2068 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int dp[25][(1<<13)+5];//dp[i][j]表示轮到第i个人取时,剩j个石头 int n,s,m[25]; int DFS(int pos,int remain) { if(dp