AGC010-D Decrementing を解いた

問題リンク

atcoder.jp

問題

2人が数列  A_1, ... A_N に交互に次の操作をする。

  1.  A_1, ... A_N のうち  2 以上の要素を選んで  1 を引く
  2.  A_1, ... A_N A_1, ... A_N のgcdで割る

操作できなくなった方が負けのとき、勝つのは先手と後手のどちらか。

思考

操作2がないとすると、答えは  \sum (A_i-1) の偶奇で決まる。

操作2を入れて考えると、 \sum (A_i-1) の偶奇が変わるのは偶数で割るときだけ。
 A_i の偶奇に注目すればいい!

解答

N≧3のとき

操作2がなければ先手が勝てるとき

先手の番で偶数を奇数にすれば、奇数が常に1個以上ある = 操作2によって総和の偶奇が変化しない状態にできる。
→ 先手必勝

操作2がなければ後手が勝てるとき

奇数が2個以上のとき、後手の番で偶数を奇数にすれば、奇数が常に1個以上ある = 操作2によって(以下略)な状態にできる。
→ 奇数が2個以上のときは後手必勝

奇数が1個のときは先手はその1個を偶数にするしかない。
→ 奇数が1個のときは再帰的に解く

N=2のとき

{奇, 奇} のとき、先手は奇数を増やせないので話が少しややこしそう。

よくわからないので実験したところ、綺麗な市松模様になった。
→ 答えは操作2がないときと同じ

N=1のとき

 A_1=1 確定なので cout << "Second" << endl;

提出コード

//#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
constexpr lint mod = 1e9 + 7;
#define all(x) (x).begin(), (x).end()
#define bitcount(n) __builtin_popcountll((lint)(n))
#define fcout cout << fixed << setprecision(15)
#define highest(x) (63 - __builtin_clzl(x))
#define rep(i, n) for(int i = 0; i < int(n); i++)
#define rep2(i, l, r) for(int i = int(l); i < int(r); i++)
#define repr(i, n) for(int i = int(n) - 1; i >= 0; i--)
#define repr2(i, l, r) for(int i = int(r) - 1; i >= int(l); i--)
constexpr int inf9 = 1e9; constexpr lint inf18 = 1e18;
inline void Yes(bool condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
lint power(lint base, lint exponent, lint module){ if(exponent % 2){ return power(base, exponent - 1, module) * base % module; }else if(exponent){ lint root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; }else{ return 1; }}
struct position{ int x, y; }; position mv[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; double euclidean(position first, position second){ return sqrt((second.x - first.x) * (second.x - first.x) + (second.y - first.y) * (second.y - first.y)); }
template<class itr> void array_output(itr start, itr goal){ string ans; for(auto i = start; i != goal; i++) cout << (i == start ? "" : " ") << (*i); if(!ans.empty()) ans.pop_back(); cout << ans << endl; }
template<class itr> void cins(itr first, itr last){ for(auto i = first; i != last; i++){ cin >> (*i); } }
struct combination{ vector<lint> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1){ fact[0] = 1; for(int i = 1; i <= sz; i++){ fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2, mod); for(int i = sz - 1; i >= 0; i--){ inv[i] = inv[i + 1] * (i + 1) % mod; } } lint P(int n, int r){ if(r < 0 || n < r) return 0; return (fact[n] * inv[n - r] % mod); } lint C(int p, int q){ if(q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } };
template<class itr> bool next_sequence(itr first, itr last, int max_bound){ itr now = last; while(now != first){ now--; (*now)++; if((*now) == max_bound){ (*now) = 0; }else{ return true; } } return false; }
template<class itr, class itr2> bool next_sequence2(itr first, itr last, itr2 first2, itr2 last2){ itr now = last; itr2 now2 = last2; while(now != first){ now--, now2--; (*now)++; if((*now) == (*now2)){ (*now) = 0; }else{ return true; } } return false; }
template<class T> bool chmax(T &a, const T &b){ if(a < b){ a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b){ if(b < a){ a = b; return 1; } return 0; }
inline int at(lint i, int j){ return (i >> j) & 1; }
random_device rnd;
bool is_in_board(lint y, lint x, lint H, lint W){ return (0 <= y && y < H && 0 <= x && x < W); }
lint inv2 = power(2, mod - 2, mod);

struct io_init {
    io_init() {
      cin.tie(nullptr); cout.tie(nullptr);
      std::ios::sync_with_stdio(false);
    }
} io_init;

#define int lint

bool solve3(int n, vector<int> a){
    int s = 0;
    for(int x: a) s += x - 1;
    if(s % 2 == 1){
        return true;
    }
    
    int odd = 0;
    for(int x: a) odd += x % 2;
    if(odd >= 2){
        return false;
    }
    
    int g = 0;
    rep(i, n){
        if(a[i] == 1) return false;
        if(a[i] % 2) a[i]--;
        g = gcd(g, a[i]);
    }
    rep(i, n){
        a[i] /= g;
    }
    return !solve3(n, a);
}

signed main(){
    int n;
    cin >> n;
    vector<int> a(n);
    cins(all(a));
    if(n == 1) cout << "Second" << endl;
    else if(n == 2) cout << ((a[0] + a[1]) % 2 ? "First" : "Second") << endl;
    else cout << (solve3(n, a) ? "First" : "Second") << endl;
}

(やばい、オーバーフローにハマってたのがバレる...)

Submission #21519251 - AtCoder Grand Contest 010