P10501 Cutting Game 题解
直接 SG 函数,不会的可以看 OI Wiki。
设
于是有
其中
代码 ↓
#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;
}
禁止抄代码!!!