跳到正文

题解

P10501 Cutting Game 题解

_SLIDE_约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年6月4日
收录
2025年12月12日
更新
2026年10月13日
篇幅
约 2 分钟
获赞
6
题目
P10501

版本

  1. v1当前

直接 SG 函数,不会的可以看 OI Wiki。

设 SGw,hSG_{w,h} 表示在 w×hw\times h 的矩形纸张中先手的 SG 值,很显然 SG1,1=0SG_{1,1}=0(因为对方搞出了 1×11\times 1 纸张)。如果 ww 和 hh 中只有一个 11,那搞成 1×11\times 1 就赢了。因此当 w,h>1w,h>1 时绝对不能把纸张搞成 1×h1\times h 或 w×1w\times 1。

于是有

SGw,h={0w=1,h=1mex⁡({x∣x=SGi,h⊕SGw−i,h⋁x=SGw,j⊕SGw,h−j})w>1∨h>1SG_{w,h}=\begin{cases} 0 & w=1,h=1 \\ \operatorname{mex}(\{x|x=SG_{i,h}\oplus SG_{w-i,h}\bigvee x=SG_{w,j}\oplus SG_{w,h-j}\}) & w>1\vee h>1 \end{cases}

其中 i∈(1,w−1),j∈(1,h−1)i\in(1,w-1),j\in(1,h-1)。

代码 ↓

C++
#include <iostream>
#include <tuple>
#include <map>
using namespace std;
int dp[201][201];
int sg(const int w,const int h) {
	if(dp[w][h]>=0) return dp[w][h];
	if(w==1&&h==1) return dp[w][h]=0;
	map<int,bool> mp;
	for(int i=2;i<w-1;i++)
		mp[sg(i,h)^sg(w-i,h)]=1;
	for(int i=2;i<h-1;i++)
		mp[sg(w,i)^sg(w,h-i)]=1;
	for(dp[w][h]=0;;dp[w][h]++)
		if(!mp[dp[w][h]]) return dp[w][h];
}
int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	for(auto &i: dp)
		for(auto &j: i)
			j=-1;
	for(int w,h;cin>>w;) {
		cin>>h;
		cout<<(sg(w,h)?"WIN\n":"LOSE\n");
	}
	return 0;
}

禁止抄代码!!!

原文