UVALive 5059 Playing With Stones(求sg函数)

题意和nim游戏差不多,就是取石子的时候最多只能拿原来的一半,比如一堆5个石子最多拿两个。

先用打表的方式看出前面一部分的sg值,然后找规律来做。

打表求sg值的程序才是最重要的。

#include<cstdio>
#include<cstring>
#define ll long long
int main() {
    int vis[50];
    int sg[50];
    sg[1] = 0;
    for(int i = 2; i <= 30; i++) {
        memset(vis, 0, sizeof(vis));
        for(int j = 1; j * 2 <= i; j++) vis[sg[i-j]] = 1;
        for(int j = 0; ; j++) if(!vis[j]) {
            sg[i] = j;
            break;
        }
        printf("%d ", sg[i]);
    }
}
#include<cstdio>
#include<cstring>
const int maxn = 110;
#define ll long long
ll sg(ll x) {
    return x % 2 == 0 ? x / 2 : sg(x / 2);
}

int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        ll ans = 0;
        int n;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) {
            ll tmp;
            scanf("%lld", &tmp);
            ans ^= sg(tmp);
        }
        if(!ans) puts("NO");
        else puts("YES");
    }
}
时间: 2024-10-11 13:20:11

UVALive 5059 Playing With Stones(求sg函数)的相关文章

UVALive 5059 C - Playing With Stones 博弈论Sg函数

C - Playing With Stones Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 5059 Description You and your friend are playing a game in which you and your friend take turns removing stones from piles.

【LA5059】Playing With Stones (SG函数)

题意:有n堆石子,分别有a[i]个.两个游戏者轮流操作,每次可以选一堆,拿走至少一个石子,但不能拿走超过一半的石子. 谁不能拿石子就算输,问先手胜负情况 n<=100,1<=a[i]<=2e18 思路:打表找SG函数的规律 当n为偶数时,SG(n)=n/2 当n为奇数时,SG(n)=SG(n/2) 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algo

HDU 1536 S-Nim 求SG函数

题意:给你n个数Nnum[ i ],表示每次只能取Nnum[ i ]个数. m个问题:每次给你 l 堆石子,每堆有num个石子,问先手是否会赢. Sample Input 2 2 5 3 2 5 12 3 2 4 7 4 2 3 7 12 5 1 2 3 4 5 3 2 5 12 3 2 4 7 4 2 3 7 12 0 Sample Output LWW WWL 经典Nim游戏,找出SG就可以了. 至于如何找SG,这里有详细的 点我 #include<cstdio> #include<

HDU 3032 Nim or not Nim? (博弈之求SG函数)

题意:经典Nim博弈游戏变换,给你n堆石子pi,每堆有pi个石子, Alice和Bob轮流取石子,每次可以从任意一堆中拿走任意个石子,也可以将某一堆石子分成两个小堆 (每堆石子个数必须不能为0),先拿完者获胜 思路:求SG函数后找规律: SG函数定义及求法:点击打开链接 #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #in

Gym 101246D Fire in the Country(dfs求SG函数)

http://codeforces.com/gym/101246/problem/D 题意: 给定一个无向有环图,大火从1点开始,每个时间点与它相邻的点也将会着火,现在有两个人轮流操作机器人,机器人从1点出发,每个人每次选择一个点走,谁最后被火烧了谁就输了. 思路: 博弈题. 我们先预处理求出每个点着火的时间点,然后根据时间点重建新图,也就是重新建一个有向无环图,原来图中相连的并且时间点相差1的相连,由时间低的连向时间高的. 接下来我们在新图上求每个点的SG值,SG值为0的点就是叶子结点,这样父

HDU 5795 A Simple Nim 打表求SG函数的规律

A Simple Nim Problem Description Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).T

hdu 1760 A New Tetris Game 博弈~~搜索求sg函数

A New Tetris Game Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1176    Accepted Submission(s): 557 Problem Description 曾经,Lele和他姐姐最喜欢,玩得最久的游戏就是俄罗斯方块(Tetris)了. 渐渐得,Lele发觉,玩这个游戏只需要手快而已,几乎不用经过大

LA 5059 (找规律 SG函数) Playing With Stones

题意: 有n堆石子,两个人轮流取,每次只能取一堆的至少一个至多一半石子,直到不能取为止. 判断先手是否必胜. 分析: 本题的关键就是求SG函数,可是直接分析又不太好分析,于是乎找规律. 经过一番“巧妙”的分析,有这样一个规律: 如果n是偶数,SG(n) = n / 2; 如果n是奇数,SG(n) = SG(n / 2); 这道题的意义不在于规律是什么,而是要自己能够写出求SG函数值的代码.顺便再体会一下mex(S)的含义. 1 #include <cstring> 2 3 const int

Marbles(博弈SG函数)

Marbles Gym - 101908B Using marbles as a currency didn't go so well in Cubic?nia. In an attempt to make it up to his friends after stealing their marbles, the Emperor decided to invite them to a game night in his palace. Of course, the game uses marb