Cutting Game - POJ 2311 - Virtual Judge
SG函数深入理解orz:博弈论 SG函数_Strangedbly-CSDN博客_sg函数
AC代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) //#pragma GCC optimize("Ofast") #include <iostream> #include <queue> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> #include <cstring> #include <string> #include <cctype> #include <map> #include <vector> #include <deque> #include <set> #include <stack> #include <numeric> #include <iomanip> #include <functional> using namespace std; #define lowbit(x) ((x) & -(x)) #define IOS1 ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); #define IOS2 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); typedef vector<int> vi; typedef vector<long long> vll; typedef vector<char> vc; template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template<class T> T power(T a, int b) { T res = 1; for (; b; b >>= 1, a = a * a) { if (b & 1) { res = res * a; } } return res; } template <typename T> inline void read(T& x) { x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0', ch = getchar(); } x *= f; } /* Tips: 1.int? long long? 2.don't submit wrong answer 3.figure out logic first, then start writing please 4.know about the range 5.check if you have to input t or not 6.modulo of negative numbers is not a%b, it is a%b + abs(b) */ const int INF = 0x3f3f3f3f; const int mod = 1000000007; const double PI = acos(-1.0); vector<vector<int> > sg(210, vector<int>(210, -1)); int getsg(int n, int m) { if (sg[n][m] != -1) { return sg[n][m]; } vector<int> vis(1006, 0); for (int i = 2; i <= n - i; i++) { vis[getsg(i, m) ^ getsg(n - i, m)] = 1; } for (int i = 2; i <= m - i; i++) { vis[getsg(n, i) ^ getsg(n, m - i)] = 1; } for (int i = 0;; i++) { if (vis[i] == 0) { return sg[n][m] = i; } } } void solve() { int w, h; while (cin >> w >> h) { if (getsg(w, h)) { cout << "WIN" << endl; } else { cout << "LOSE" << endl; } } } int main() { //IOS1; IOS2; int __t = 1; //cin >> __t; for (int _t = 1; _t <= __t; _t++) { solve(); } return 0; } /* */