uva 1482 - Playing With Stones(Nim游戏)

题目链接: uva 1482 - Playing With Stones

题目大意:n堆石子,给定每堆石子的个数,两个人分别从操作,每次可以从一堆中取走至少一个石子,但是不能超过一半。如果不能操作则视为失败。

解题思路:对于每一堆式子来说,可以看作一个Nim游戏,但是SG(x)并不等于x,因为每次取石子不能超过一半,所以对于偶数SG(x)=x/2,对于奇数SG(x)=SG(x/2).

所以去Nim和即可。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;

ll SG (ll x) {
    return x&1 ? SG(x>>1) : x>>1;
}

int main () {
    int cas, n;

    scanf("%d", &cas);
    while (cas--) {
        ll m, v = 0;
        scanf("%d", &n);

        for (int i = 0; i < n; i++) {
            scanf("%lld", &m);
            v ^= SG(m);
        }
        printf("%s\n", v ? "YES" : "NO");
    }
    return 0;
}

uva 1482 - Playing With Stones(Nim游戏),布布扣,bubuko.com

时间: 2024-10-12 23:47:29

uva 1482 - Playing With Stones(Nim游戏)的相关文章

UVA - 1482 Playing With Stones

Description You and your friend are playing a game in which you and your friend take turns removing stones from piles. Initially there are N piles with a1, a2, a3,..., aN number of stones. On each turn, a player must remove at least one stone from on

UVA 1482 - Playing With Stones(SG打表规律)

UVA 1482 - Playing With Stones 题目链接 题意:给定n堆石头,每次选一堆取至少一个,不超过一半的石子,最后不能取的输,问是否先手必胜 思路:数值很大,无法直接递推sg函数,打出前30项的sg函数找规律 代码: #include <stdio.h> #include <string.h> int t, n; long long num; long long SG(long long x) { return x % 2 == 0 ? x : SG(x /

ZOJ 3964 Yet Another Game of Stones Nim游戏变种

ZOJ3964 解题思路 此题的题意比较容易理解,可以简单的看着 Nim 博弈的变种.但问题在于 Alice 对第 i 堆石子的取法必须根据 bi 确定.所以如果这个问题能够归结到正常的 Nim 博弈(取石子问题),则很容易解决. 考虑特判存在 bi=1 或 bi=2 的情况: 如果存在第 i 堆石子,其 ai 为奇数且 bi=2 ,则 Bob 必胜(Alice 在最优策略下无法取完该堆,但 Bob 可以). 如果存在 2 个及以上 bi=2 或 bi=1 且 ai>1 的情况,则 Bob 必胜

LeetCode 292 Nim Game(Nim游戏)

翻译 你正在和你的朋友们玩以下这个Nim游戏:桌子上有一堆石头.每次你从中去掉1-3个.谁消除掉最后一个石头即为赢家.你在取出石头的第一轮. 你们中的每个人都有着聪明的头脑和绝佳的策略.写一个函数来确定对于给定的数字是否你能够赢得这场比赛. 比如,假设堆中有4个石头,那么你永远也无法赢得比赛:不管你移除了1.2或3个石头,最后一个石头都会被你的朋友所移除. 原文 You are playing the following Nim Game with your friend: There is a

[Swift]LeetCode292. Nim游戏 | Nim Game

You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove th

LeetCode OJ:Nim Game(Nim游戏)

You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove th

HDU_1907_基础博弈nim游戏

John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4440    Accepted Submission(s): 2541 Problem Description Little John is playing very funny game with his younger brother. There is one big bo

uva 11927 - Games Are Important(组合游戏+记忆化)

题目链接:uva 11927 - Games Are Important 题目大意:给出一张无环有向图,并给出每个节点上的石子数,每次操作可以选择一个石子,向下一个节点移动.两人轮流操作,直到不能操作为失败者. 解题思路:有了图之后,用记忆化的方式处理出每个节点的SG值,取所有石子数为奇数的节点的Nim和. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; con

10165 - Stone Game(Nim游戏)

UVA 10165 - Stone Game 题目链接 题意:给定n堆石子,每次能在一堆取1到多个,取到最后一个赢,问谁赢 思路:就裸的的Nim游戏,利用定理求解 代码: #include <stdio.h> #include <string.h> int n, num; int main() { while (~scanf("%d", &n) && n) { int sum = 0; for (int i = 0; i < n;