0x3A Cutting Game (博弈论,SG函数)

本文深入探讨了SG函数在博弈论中的应用,特别是在有向图游戏中。通过定义mex和SG函数,解释了如何确定游戏状态的胜负,并提供了一个具体的纸张剪切问题实例,展示了如何使用动态规划和记忆化搜索求解SG函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是一道经典的SGSGSG函数题目

存在SG函数的博弈问题都是有向图游戏

首先定义 mex(S)mex(S)mex(S) 表示不属于集合 SSS 的最小非负整数

SG(x)SG(x)SG(x) 的定义为 mex{SG(y1),SG(y2),SG(y3),....SG(yk)}mex\{SG(y_1),SG(y_2),SG(y_3),....SG(y_k)\}mex{SG(y1),SG(y2),SG(y3),....SG(yk)},其中 y1...yky_1...y_ky1...yk 表示 xxx 能够转移到的状态

有向图游戏的和等于给个游戏函数值的异或和,即SG(G)=SG(G1) xor SG(G2) xor... xor SG(Gk)SG(G)=SG(G_1) \ xor \ SG(G_2) \ xor ... \ xor \ SG(G_k)SG(G)=SG(G1) xor SG(G2) xor... xor SG(Gk)
当起点的 SGSGSG 函数大于0则必胜,否则必败

这个题目就是每次给某张纸剪一下使他分成两块,最终剪出 1∗11*111 的玩家获胜

首先 1∗11*111 肯定是一个必胜局面

那么显然 2∗2,2∗3,3∗22*2,2*3,3*222,23,32 都是必败局面

那么能够通过 SGSGSG 函数解决此题

定义 SG(i,j)SG(i,j)SG(i,j) 表示纸张大小为 i∗ji*jij 时的 SGSGSG 函数

那么能够轻松地知道

SG(n,m)=mex({SG(i,m) xor SG(n−i,m)}∪{SG(n,i) xor SG(n,m−i)})SG(n,m)=mex(\{SG(i,m) \ xor \ SG(n-i,m)\} \cup \{SG(n,i) \ xor \ SG(n,m-i)\})SG(n,m)=mex({SG(i,m) xor SG(ni,m)}{SG(n,i) xor SG(n,mi)})

通过上述方程转移SG函数,通过记忆化搜索即可解决本问题

#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(s) freopen(s".in", "r", stdin), freopen(s."out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 210 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }

int n, m ;
int dp[N][N] ;

int sg(int n, int m) {
	if (dp[n][m] != -1) return dp[n][m] ;
	bool g[N] = {0} ;
	rep(i, 2, m / 2) g[sg(n, i) ^ sg(n, m - i)] = 1 ;
	rep(i, 2, n / 2) g[sg(i, m) ^ sg(n - i, m)] = 1 ;
	for (int i = 0;; i++) if (!g[i]) return dp[n][m] = i ;
}

signed main(){
	ass(dp, -1) ;
	while (scanf("%d%d", &n, &m) != EOF) {
		if (sg(n, m) > 0) puts("WIN") ;
		else puts("LOSE") ;
	}
	return 0 ;
}

/*
写代码时请注意:
	1.ll?数组大小,边界?数据范围?
	2.精度?
	3.特判?
	4.至少做一些
思考提醒:
	1.最大值最小->二分?
	2.可以贪心么?不行dp可以么
	3.可以优化么
	4.维护区间用什么数据结构?
	5.统计方案是用dp?模了么?
	6.逆向思维?
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值