【AGC010 D】Decrementing (博弈)

Description

有一个正整数数列,两人轮流操作,每次选择一个大于1的数,将其减1,然后将所有数除以所有数的 gcd ,无法操作的一方负,问先手必胜还是后手必胜。


Solution

考虑对于1,1,…,1,2是必胜的,对于其它的 k , k , . . . , k , k + 1 ,对方都可以通过操作来避免。而1,1,…,1,2有奇数个偶数。

一个数列如果有奇数个偶数,先手必胜。
如果有偶数个偶数,奇数个数不等于1,后手必胜。
如果有偶数个偶数,奇数个数等于1,先手肯定会将奇数减一,然后全部除2,这个可以递归处理。


Code

/************************************************
 * Au: Hany01
 * Date: Jul 23rd, 2018
 * Prob: AGC 010 D
 * Email: [email protected]
 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
    static int _, __; static char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 1e5 + 5;

int n, a[maxn], cnt[2];

inline int sp() {
    register int ans = 0;
    For(i, 1, n) (ans += a[i] - 1) &= 1;
    return ans;
}

int solve()
{
    cnt[0] = cnt[1] = 0;
    For(i, 1, n) {
        if (a[i] == 1) return sp();
        ++ cnt[a[i] & 1];
    }
    if (cnt[0] & 1) return 1;
    if (cnt[0] & 1 ^ 1 && cnt[1] != 1) return 0;
    int t = 0;
    For(i, 1, n) a[i] >>= 1, t = __gcd(t, a[i]);
    For(i, 1, n) a[i] /= t;
    return solve() ^ 1;
}

int main()
{
#ifdef hany01
    File("agc010d");
#endif

    n = read();
    For(i, 1, n) a[i] = read();

    puts(solve() ? "First" : "Second");

    return 0;
}
//黄师塔前江水东,春光懒困倚微风。
//    -- 杜甫《江畔独步寻花·其五》

猜你喜欢

转载自blog.csdn.net/hhaannyyii/article/details/81170635
今日推荐