Nowcoder 挑战赛23 B 游戏 ( NIM博弈、SG函数打表 )

题目链接

题意 : 中文题、点链接

分析 :

前置技能是 SG 函数、NIM博弈变形

每次可取石子是约数的情况下、那么就要打出 SG 函数

才可以去通过异或操作判断一个局面的胜负

打 SG 函数的时候、由于 N 很大

所以不能使用递归的方式打表、会爆栈

还有要预处理每个数的约数

打出 SG 函数之后

暴力判断初始局面的每堆石子取走约数后是否对答案产生贡献

#include<bits/stdc++.h>
#define LL long long
#define ULL unsigned long long

#define scl(i) scanf("%lld", &i)
#define scll(i, j) scanf("%lld %lld", &i, &j)
#define sclll(i, j, k) scanf("%lld %lld %lld", &i, &j, &k)
#define scllll(i, j, k, l) scanf("%lld %lld %lld %lld", &i, &j, &k, &l)

#define scs(i) scanf("%s", i)
#define sci(i) scanf("%d", &i)
#define scd(i) scanf("%lf", &i)
#define scIl(i) scanf("%I64d", &i)
#define scii(i, j) scanf("%d %d", &i, &j)
#define scdd(i, j) scanf("%lf %lf", &i, &j)
#define scIll(i, j) scanf("%I64d %I64d", &i, &j)
#define sciii(i, j, k) scanf("%d %d %d", &i, &j, &k)
#define scddd(i, j, k) scanf("%lf %lf %lf", &i, &j, &k)
#define scIlll(i, j, k) scanf("%I64d %I64d %I64d", &i, &j, &k)
#define sciiii(i, j, k, l) scanf("%d %d %d %d", &i, &j, &k, &l)
#define scdddd(i, j, k, l) scanf("%lf %lf %lf %lf", &i, &j, &k, &l)
#define scIllll(i, j, k, l) scanf("%I64d %I64d %I64d %I64d", &i, &j, &k, &l)

#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define lowbit(i) (i & (-i))
#define mem(i, j) memset(i, j, sizeof(i))

#define fir first
#define sec second
#define VI vector<int>
#define ins(i) insert(i)
#define pb(i) push_back(i)
#define pii pair<int, int>
#define VL vector<long long>
#define mk(i, j) make_pair(i, j)
#define all(i) i.begin(), i.end()
#define pll pair<long long, long long>

#define _TIME 0
#define _INPUT 0
#define _OUTPUT 0
clock_t START, END;
void __stTIME();
void __enTIME();
void __IOPUT();
using namespace std;
const int maxn = 1e5 + 10;

int SG[maxn];
VI divisor[maxn];

inline void init()
{
    for(int i=1; i<maxn; i++)
        for(int j=i; j<maxn; j+=i)
            divisor[j].pb(i);

    SG[0] = 0;
    for(int i=1; i<maxn; i++){
        set<int> s;
        for(int j=0; j<(int)divisor[i].size(); j++) s.ins(SG[i-divisor[i][j]]);
        for(int j=0;;j++) if(!s.count(j)){ SG[i] = j; break; }
    }
}

int arr[maxn];
int main(void){__stTIME();__IOPUT();

    init();

    int n;
    sci(n);

    int xorSum = 0;
    for(int i=0; i<n; i++) sci(arr[i]), xorSum ^= SG[arr[i]];

    int ans = 0;
    for(int i=0; i<n; i++){
        for(int j=0; j<(int)divisor[arr[i]].size(); j++){
            if((xorSum ^
                SG[arr[i]] ^
                SG[arr[i] - divisor[arr[i]][j]]) == 0) ans++;
        }
    }

    printf("%d\n", ans);

__enTIME();return 0;}

void __stTIME()
{
    #if _TIME
        START = clock();
    #endif
}

void __enTIME()
{
    #if _TIME
        END = clock();
        cerr<<"execute time = "<<(double)(END-START)/CLOCKS_PER_SEC<<endl;
    #endif
}

void __IOPUT()
{
    #if _INPUT
        freopen("in.txt", "r", stdin);
    #endif
    #if _OUTPUT
        freopen("out.txt", "w", stdout);
    #endif
}

原文地址:https://www.cnblogs.com/Rubbishes/p/9606583.html

时间: 2024-08-05 13:47:11

Nowcoder 挑战赛23 B 游戏 ( NIM博弈、SG函数打表 )的相关文章

hdu 5795 A Simple Nim 博弈sg函数

A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) 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 an

51 nod1067 Bash游戏 V2(sg函数打表)

1067 Bash游戏 V2 1.0 秒 131,072.0 KB 5 分 1级题 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次只能拿1,3,4颗,拿到最后1颗石子的人获胜.假设A B都非常聪明,拿石子的过程中不会出现失误.给出N,问最后谁能赢得比赛. 例如N = 2.A只能拿1颗,所以B可以拿到最后1颗石子. 收起 输入 第1行:一个数T,表示后面用作输入测试的数的数量.(1 <= T <= 10000) 第2 - T + 1行:每行1个数N.(1 <= N <= 10^

[hdu-5795]A Simple Nim 博弈 尼姆博弈 SG函数打表找规律

[题目]题目链接 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).To make the game more int

51nod_1714:B君的游戏(博弈 sg打表)

题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1714 nim游戏的一个变形,需要打出sg函数的表 #include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn=70000000; int sg[65], maxs; int vis[maxn]; //yu控制递归层数,cur控制所分配最大值,next控

【转】博弈—SG函数

转自:http://chensmiles.blog.163.com/blog/static/12146399120104644141326/ http://blog.csdn.net/xiaofengcanyuexj/article/details/17119705 SG函数 “Sprague-Grundy函数” 我们将面对更多与Nim游戏有关的变种,还会看到Nim游戏的a1^a2^...^an这个值更广泛的意义. 上面的文章里我们仔细研究了Nim游戏,并且了解了找出必胜策略的方法.但如果把Ni

(转)博弈 SG函数

此文为以下博客做的摘要: https://blog.csdn.net/strangedbly/article/details/51137432 ---------------------------------------------------------------------------------------- 1.定义P-position和N-positon P表示Previous,N表示Next. 即上一个移动的人有必胜策略的局面是P-position,"先手必败"或&qu

UVA 10561 - Treblecross(博弈SG函数)

UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',可以在'.'的位置放X,谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每个串要是上面有一个X,周围的4个位置就是禁区了(放下去必败),所以可以以X分为几个子游戏去求SG函数的异或和进行判断,至于求策略,就是枚举每个位置就可以了 代码: #include <stdio.h> #include <string.h> #include <algorithm> usi

UVA 11534 - Say Goodbye to Tic-Tac-Toe(博弈sg函数)

UVA 11534 - Say Goodbye to Tic-Tac-Toe 题目链接 题意:给定一个序列,轮流放XO,要求不能有连续的XX或OO,最后一个放的人赢,问谁赢 思路:sg函数,每一段...看成一个子游戏,利用记忆化求sg值,记忆化的状态要记录下左边和右边是X还是O即可 代码: #include <stdio.h> #include <string.h> const int N = 105; int t, sg[3][3][N]; char str[N]; int ge

Nim or not Nim? hdu3032 SG值打表找规律

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