xxxxxxxxxx
/*
a = (長さ) mod 2
b = (1 の個数) mod 2
c = (1 を除く最小の数の個数) mod 2
とし,局面の状態を [abc] で表す.
命題:
先手必勝局面は [001], [010], [011], [100], [101], [110]
後手必勝局面は [000], [111]
証明:
1 を除く最小の数を x とおく.
また 1 と x を除く最小の数を y とおく.
まず先手必勝局面から後手必勝局面に遷移可能であることを示す.
[001] からは,
1 があれば 1→0 として [111] に遷移できる.
1 がなければ y→x として [000] に遷移できる(偶奇性より y は必ず存在する.また 1 が無いので操作は合法).
[010] からは x→0 として [111] に遷移できる(偶奇性より x は必ず存在する).
[011] からは
y があれば y→0 として [111] に遷移できる.
y がなければ x→1 として [000] に遷移できる(x が 1 個だったとしても大丈夫).
[100] からは y→0 として [000] に遷移できる(偶奇性より y は必ず存在する).
[101] からは
x が 1 個かつ y が奇数個ならば x→1 として [111] に遷移できる.
さもなくば x→0 として [000] に遷移できる.
[110] からは 1→0 として [000] に遷移できる.
次に後手必勝局面からは先手必勝局面にしか遷移できないことを示す.
[000]→[111],[111]→[000] の遷移が不可能であることを示す.
長さの偶奇を変える必要があるので,操作は *→0 の形である必要がある.
さらに 1 の個数の偶奇を変える必要があるので,操作は 1→0 の形である必要がある.
しかしこれでは x の個数の偶奇を変えることができない.
[000]→[000],[111]→[111] の遷移が不可能であることを示す.
長さの偶奇を変えてはならないので,*→0 の形の操作は禁止される.
もし 1 があれば可能な操作は *→1 の形しかないが,これでは 1 の個数の偶奇が変わってしまう.
残る可能性は 1 が無いときの [000]→[000] の遷移であるが,
*→x の形の操作は x の個数の偶奇を変えてしまい,
*→1 の形の操作は 1 の個数の偶奇を変えてしまい,
*→x'(1<x'<x) の形の操作は 1 を除く最小の数の個数を奇数にしてしまうのでいずれも行えない.
*/
// 折りたたみ用
// 警告の抑制
// ライブラリの読み込み
using namespace std;
// 型名の短縮
using ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)
using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;
using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;
// 定数の定義
const double PI = acos(-1);
const vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
const vi DY = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003104004004LL; // (int)INFL = 1010931620;
// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;
// 汎用マクロの定義
// 0 から n-1 まで昇順
// s から t まで昇順
// s から t まで降順
// a の全要素(変更不可能)
// a の全要素(変更可能)
// d ビット全探索(昇順)
// set の全要素(昇順)
// a の順列全て(昇順)
// 非負mod
// 重複除去
// 強制終了
// 半開矩形内判定
// 汎用関数の定義
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
template <class T> inline T get(T set, int i) { return (set >> i) & T(1); }
// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }
// 折りたたみ用
using namespace atcoder;
//using mint = modint1000000007;
using mint = modint998244353;
//using mint = modint; // mint::set_mod(m);
namespace atcoder {
inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
// 手元環境(Visual Studio)
// 提出用(gcc)
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
//【局面のニム値】O(?)(遅いので実験用)
/*
* 初期局面 p から遷移可能な局面とそのニム値のリストを返す.
* nxt(p, nps) を呼ぶと,p から遷移可能な局面のリストを nps に格納するものとする.
*/
map<vl, int> nim; // これをグローバル変数にすれば再利用可能
template <class T>
map<T, int> calc_nimber(const T& p, function<void(const T&, vector<T>&)>& nxt) {
function<int(const T&)> calc_nimber = [&](const T& p) {
if (nim.count(p)) return nim[p];
vector<T> nps;
nxt(p, nps);
vi next_nimbers;
repe(np, nps) {
next_nimbers.push_back(calc_nimber(np));
}
uniq(next_nimbers);
int i = 0;
while (i < sz(next_nimbers) && next_nimbers[i] == i) i++;
nim[p] = i;
return nim[p];
};
calc_nimber(p);
return nim;
/* nxt の定義の雛形
using T = vi;
function<void(const T&, vector<T>&)> nxt = [&](const T& p, vector<T>& nps) {
};
*/
}
bool naive(int n, vl a) {
using T = vl;
function<void(const T&, vector<T>&)> nxt = [&](const T& p, vector<T>& nps) {
if (p.empty()) return;
ll x = p.back();
rep(i, sz(p)) {
repi(k, 0, min(x, p[i] - 1)) {
auto np(p);
np[i] = k;
sort(all(np), greater<ll>());
if (np.back() == 0) np.pop_back();
nps.push_back(np);
}
}
};
sort(all(a), greater<ll>());
auto res = calc_nimber(a, nxt);
//for (auto [p, g] : res) {
// if (g > 0) continue;
// vl b(p);
// uniq(b);
// reverse(all(b));
// if (p == b) {
// dump(p);
// }
//}
//dump("----- 0 -----");
//for (auto [p, g] : res) {
// if (g > 0) continue;
// dump(p);
//}
//dump("----- 1 -----");
//for (auto [p, g] : res) {
// if (g == 0) continue;
// dump(p);
//}
return res[a] > 0;
}
bool solve(int n, vl a) {
int cnt1 = 0; ll a_min2 = INFL; int cnt2 = 0;
rep(i, n) {
if (a[i] == 1) {
cnt1++;
}
else {
if (a_min2 > a[i]) {
a_min2 = a[i];
cnt2 = 1;
}
else if (a_min2 == a[i]) {
cnt2++;
}
}
}
if ((n & 1) && (cnt1 & 1) && (cnt2 & 1)) return 0;
if ((~n & 1) && (~cnt1 & 1) && (~cnt2 & 1)) return 0;
return 1;
}
void bug_find() {
// 合わない入力例を見つける.
mute_dump = true;
mt19937_64 mt;
mt.seed((int)time(NULL));
uniform_int_distribution<ll> rnd(0LL, 1LL << 60);
rep(hoge, 100000) {
int n = rnd(mt) % 10 + 1;
vl a(n);
rep(i, n) a[i] = rnd(mt) % 6 + 1;
auto res_naive = naive(n, a);
auto res_solve = solve(n, a);
if (res_naive != res_solve) {
cout << "----------error!----------" << endl;
cout << "input:" << endl;
cout << n << endl;
cout << a << endl;
cout << "results:" << endl;
cout << res_naive << endl;
cout << res_solve << endl;
cout << "--------------------------" << endl;
}
}
mute_dump = false;
exit(0);
}
//【広義単調増加列の列挙】O(bin(n+m-1, n))(の改変)
/*
* 0 ≦ a[0] ≦ a[1] ≦ ... ≦ a[n-1] < m なる列 a[0..n) を格納したリストを返す.
*/
vvl enumerate_weakly_increase_sequences(int n, int m) {
vl a(n);
vvl seqs;
// len : 列の長さ
function<void(int)> rf = [&](int len) {
// 列の長さが n の場合
if (len == n) {
// 完成しているので記録
seqs.push_back(a);
return;
}
// i0 : 直前の数の大きさ
int i0 = (int)(len > 0 ? a[len - 1] : 1);
// 直前の数 i0 以上の数が選べる.
repi(i, i0, m - 1) {
a[len++] = i;
rf(len);
len--;
}
};
rf(0);
return seqs;
}
// 徹底的に探す.
void bug_find2() {
// 合わない入力例を見つける.
mute_dump = true;
mt19937_64 mt;
mt.seed((int)time(NULL));
uniform_int_distribution<ll> rnd(0LL, 1LL << 60);
repi(n, 1, 6) {
auto as = enumerate_weakly_increase_sequences(n, 15);
repe(a, as) {
auto res_naive = naive(n, a);
auto res_solve = solve(n, a);
if (res_naive != res_solve) {
cout << "----------error!----------" << endl;
cout << "input:" << endl;
cout << n << endl;
cout << a << endl;
cout << "results:" << endl;
cout << res_naive << endl;
cout << res_solve << endl;
cout << "--------------------------" << endl;
}
}
}
mute_dump = false;
exit(0);
}
// 見つからない・・・オーバーフローなわけもないし・・・うーん・・・
// どーしても分からなかったので,ずるして tatyam さんの提出を見てしまった.
bool cheat(int n, vl a) {
if (n % 2 == 0) {
ll a_min = *min_element(all(a));
int cnt = 0;
rep(i, n) cnt += a[i] == a_min;
return cnt % 2 == 1;
}
else {
return solve(n, a);
}
}
/*
* この判定方法だと [3, 2, 1, 1] は後手必勝ということになるが本当?
*
* 先手 後手 先手 後手 先手 後手 先手
* 3211 → 321 → 32 → 22 → 21 → 11 → 1 → {}
* → 2 → {}
* → 311 → 11 → 1 → {}
* → 31 → 11
* → 211 → 11
* → 21 → 11
*
* やっぱり先手に必勝戦略があるように見える.
* あやしいのは 321 → 32 の遷移で 3 → 2 という手が復活する部分だが,何か解釈を間違えてる・・・?
*/
//【最善手】O(?)(遅いので実験用)
/*
* 初期局面 p から遷移可能な各局面 x について,
* 組 (x, x のニム値, x から最善手を選んで遷移できる局面のリスト) のリストを返す.
* nxt(p, nps) を呼ぶと,p から遷移可能な局面のリストを nps に格納するものとする.
*/
template <class T>
vector<tuple<T, int, vector<T>>> best_move(const T& p, function<void(const T&, vector<T>&)>& nxt) {
map<T, int> nim; // これをグローバル変数にすれば再利用可能
vector<tuple<T, int, vector<T>>> best;
function<int(const T&)> calc_nimber = [&](const T& p) {
if (nim.count(p)) return nim[p];
vector<T> nps;
nxt(p, nps);
uniq(nps);
vector<T> win;
vi next_nimbers;
repe(np, nps) {
int nimber = calc_nimber(np);
if (nimber == 0) win.push_back(np);
next_nimbers.push_back(nimber);
}
uniq(next_nimbers);
uniq(win);
int i = 0;
while (i < sz(next_nimbers) && next_nimbers[i] == i) i++;
nim[p] = i;
if (win.empty()) best.emplace_back(p, 0, move(nps));
else best.emplace_back(p, i, move(win));
return i;
};
calc_nimber(p);
return best;
/* nxt の定義の雛形
using T = vi;
function<void(const T&, vector<T>&)> nxt = [&](const T& p, vector<T>& nps) {
};
*/
}
//【二部グラフ判定(連結)】O(n + m)
/*
* 連結無向グラフが二部グラフかどうか判定する.
* 二部グラフならその彩色例を col に格納する(色は 0, 1 で表す)
*/
template <class G>
bool bipartite_graphQ(const G& g, vi& col) {
// verify : https://atcoder.jp/contests/code-festival-2017-qualb/tasks/code_festival_2017_qualb_c
int n = sz(g);
// 頂点の色(0,1 は色を,-1 は未探索を表す)
col = vi(n, -1);
// 再帰用の関数
function<bool(int)> dfs = [&](int s) {
repe(t, g[s]) {
// 未彩色の頂点の場合
if (col[t] == -1) {
// s と異なる色で t を彩色する.
col[t] = 1 - col[s];
// t から先を彩色しにいき,二部グラフでないならすぐに帰る.
if (!dfs(t)) return false;
}
// 彩色済の頂点の場合
else {
// s と t が同色だったら二部グラフではないのですぐに帰る.
if (col[t] == col[s]) return false;
}
}
// ここまで来たなら見た範囲は二部グラフである.
return true;
};
// 0 を始点として再帰関数を呼び出す.
col[0] = 0;
return dfs(0);
}
void zikken() {
vl a = { 10, 10, 10, 10, 10, 10, 10, 10 };
using T = vl;
function<void(const T&, vector<T>&)> nxt = [&](const T& p, vector<T>& nps) {
if (p.empty()) return;
ll x = p.back();
rep(i, sz(p)) {
repi(k, 0, min(x, p[i] - 1)) {
auto np(p);
np[i] = k;
sort(all(np), greater<ll>());
if (np.back() == 0) np.pop_back();
nps.push_back(np);
}
}
};
sort(all(a), greater<ll>());
auto res = best_move(a, nxt);
// 局面から勝敗判定に必要そうな情報だけを抜き出す.
auto to_code = [&](T p) {
int n = sz(p);
int cnt1 = 0; ll a_min2 = INFL; int cnt2 = 0;
rep(i, n) {
if (p[i] == 1) {
cnt1++;
}
else {
if (a_min2 > p[i]) {
a_min2 = p[i];
cnt2 = 1;
}
else if (a_min2 == p[i]) {
cnt2++;
}
}
}
int len = (n & 1);
int c1 = cnt1 & 1;
int c2 = cnt2 & 1;
return len * 4 + c1 * 2 + c2;
};
Graph g(12); Graph gd(12);
for (auto [p, nimber, nps] : res) {
int s = to_code(p);
repe(np, nps) {
int t = to_code(np);
g[s].push_back(t);
g[t].push_back(s);
gd[s].push_back(t);
}
}
rep(s, 12) {
uniq(g[s]);
uniq(gd[s]);
}
// 勝敗判定に十分な情報を抜き出せていれば,局面間の遷移グラフは 2 部グラフになる.
vi col;
dump(bipartite_graphQ(g, col));
rep(s, 12) {
int len_s = s / 4;
int c1_s = (s / 2) % 2;
int c2_s = s % 2;
repe(t, gd[s]) {
int len_t = t / 4;
int c1_t = (t / 2) % 2;
int c2_t = t % 2;
dump("(", len_s, c1_s, c2_s, ") -> (", len_t, c1_t, c2_t, ")");
}
}
exit(0);
}
/*
1
( 0 0 0 ) -> ( 0 0 1 )
( 0 0 0 ) -> ( 0 1 0 )
( 0 0 0 ) -> ( 0 1 1 )
( 0 0 0 ) -> ( 1 0 0 )
( 0 0 0 ) -> ( 1 0 1 )
( 0 0 0 ) -> ( 1 1 0 )
( 0 0 0 ) -> ( 2 0 1 )
( 0 0 0 ) -> ( 2 1 0 )
( 0 0 1 ) -> ( 0 0 0 )
( 0 0 1 ) -> ( 1 1 1 )
( 0 1 0 ) -> ( 0 0 0 )
( 0 1 0 ) -> ( 1 1 1 )
( 0 1 1 ) -> ( 0 0 0 )
( 0 1 1 ) -> ( 1 1 1 )
( 1 0 0 ) -> ( 0 0 0 )
( 1 0 0 ) -> ( 1 1 1 )
( 1 0 1 ) -> ( 0 0 0 )
( 1 0 1 ) -> ( 1 1 1 )
( 1 1 0 ) -> ( 0 0 0 )
( 1 1 1 ) -> ( 0 0 1 )
( 1 1 1 ) -> ( 0 1 0 )
( 1 1 1 ) -> ( 0 1 1 )
( 1 1 1 ) -> ( 1 0 0 )
( 1 1 1 ) -> ( 1 0 1 )
*/
void Main() {
int n;
cin >> n;
vl a(n);
cin >> a;
if (n <= 6 && *max_element(all(a)) <= 10) {
cout << (naive(n, a) ? "First" : "Second") << endl;
}
else {
cout << (solve(n, a) ? "First" : "Second") << endl;
}
}
int main() {
// input_from_file("input.txt");
// output_to_file("output.txt");
// zikken();
// bug_find2();
int t = 1;
cin >> t; // マルチテストケースの場合
rep (hoge, t) {
dump("------------------------------");
Main();
}
}
提出日時 | |
ユーザー | ![]() |
言語 | C++ (GCC 12.3) |
結果 | AC |
実行時間 | 1450 ms |
メモリ | 32976 kb |
テストケース名 | 結果 | 実行時間 | メモリ |
---|---|---|---|
01.txt | AC | 1450 ms | 32976 kb |
02.txt | AC | 12 ms | 32976 kb |
sample-01.txt | AC | 4 ms | 32976 kb |