https://atcoder.jp/contests/abc297/tasks/abc297_g
No matter what game it is, it has game states. In this game, the number of stones of each piles are considered states.
- P-position: the current player lose
- N-position: the current player win
Obviously, state 0 is an P-position, since the current player cannot make any moves.
Further, states $[l, r]$ are N-position, since the current player can remove all stones and the opponent enters a P-position(state 0).
The question here is: whether each state is either N-position or P-position? Can a state neither?
The answer is No, the reason is very simple. Consider the state $s$, and we can enter states $s_0,s_1,…,$ in one move, these states are either N-position or P-position, therefore we only have two cases:
- All states $s_i$ are N-position, that is no matter how we move, the opponent secures a win. Then the current player must lose, state $s$ is a P-position.
- There exists a state $s_i$ such that $s_i$ is a P-position, then we move to that state, the opponent must lose, that is the current player secures a win, state $s$ is a N-position.
$$sg(x)=mex(x_i)$$
$$sg(X,Y,Z)=sg(X)\oplus sg(Y)\oplus sg(Z)$$
// compile: g++ -o data data.cpp -O3 -std=gnu++20 -Wall -Wextra -Wshadow -D_GLIBCXX_ASSERTIONS -ggdb3 -fmax-errors=2 -DLOCAL
// run: ./data < data.in
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifdef LOCAL
#include <debug/codeforces.h>
#define debug(x...) {_variables(#x);_print(x);}
#else
#define debug(x...)
#endif
template<typename...Args> void print_(Args...args){((cout<<args<<" "),...)<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);++i
#define sz(v) ((int)(v).size())
#define print(...) print_(__VA_ARGS__);
#define INTINF (int)(9223372036854775807)
#define int long long
#define MAXN 200010
int n, l, r;
int mex(vector<int> v) {
sort(v.begin(), v.end());
if (!v.size() || v[0] > 0) return 0;
for (int i = 0; i < sz(v)-1; ++i) {
if (v[i + 1] - v[i] > 1) return v[i] + 1;
}
return v[sz(v)-1] + 1;
}
int s[MAXN];
int sg(int x) {
if (~s[x]) return s[x];
vector<int> v;
for (int i = x - r; i <= x - l; ++i) if (i) v.push_back(sg(i));
return s[x] = mex(v);
}
int get(int x) {
x %= (l + r);
return x / l;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
memset(s, -1, sizeof(s));
cin >> n >> l >> r;
int ans = 0;
for (int i = 0; i < n; ++i) {
int x; cin >> x;
ans ^= get(x);
}
cout << (ans ? "First" : "Second") << endl;
return 0;
}
Drew some examples to find the pattern when solving game theory problems.