题解:AT_abc278_g [ABC278G] Generalized Subtraction Game

· · 题解

AT_abc278_G

题意

有 n 个物品排成一排,每次可以删除一个长度在 l 到 r 之间的连续区间,两个人轮流删除,问谁会赢并构造必胜方案。

思路

首先我们注意到 n,l,r \leq 2000 以此我们考虑直接计算 SG 函数,是 O(n^{2 (r - l)}) 的,使用一些优化 (奇技淫巧) 也许可以通过。 然后我们考虑分类讨论:

  1. 当 l \ne r 时先手必胜,先手可以通过取走中间 x 个(x 的奇偶性与 n 相同)构造两个一样长的区间,然后对称的模仿后手即可;

如果 sg_n \ne 0 则先手必胜,反之则后手必胜。

然后我们找出一段区间 [L, R] ,裂成 [L, i) 和 [i + l, R]。为保证能够必胜,则当前所有区间的 SG 函数值的 mex 记作 mx 要能够满足:

mx \oplus sg_{R - L + 1} \oplus sg_{L - i} \oplus sg_{R - i - l - 1} = 0

直接用 set 维护连续段即可喵。(为什么这么像某个叫 ODT 的数据结构呢) 。

Code:

// Where should I linger? I ask myself.

#include <bits/stdc++.h>
#define FILE
#define IOS() std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0)
#define re register
// #define int long long
using pii = std::pair <int, int>;
using ll = long long;
using std::endl;

constexpr int N = 2005;
int n, l, r, sg[N], cnt[N];
std::vector <int> s;
std::set <std::pair <int, int> > set;
int cur;

void solve(int p){
    std::cout << "First" << endl;
    std::cout << n / 2 - p / 2 + 1 << ' ' << p << endl;
    int l = n / 2 - p / 2 + 1, r = l + p - 1;
    int x, y;
    while(std::cin >> x >> y, x > 0) if(x < l) std::cout << x + r << ' ' << y << endl; else std::cout << x - r << ' ' << y << endl;
}
bool check(int l, int r){
    auto it = set.upper_bound({l, n});
    if(it == set.begin()) return 0;
    --it;
    if(l < it->first || it->second < r) return 0;
    return (cur ^ sg[it->second - it->first + 1] ^ sg[l - it->first] ^ sg[it->second - r]) == 0;
}
void split(int l, int r){
    auto it = set.upper_bound({l, n});
    --it;
    int x = it->first, y = it->second;
    set.erase(it);
    if(r < y) set.emplace(r + 1, y);
    if(x < l) set.emplace(x, l - 1);
}
void solve(){
    cur = 0;
    for(auto it = set.begin(); it != set.end(); ++it) cur ^= sg[it->second - it->first + 1];
    for(int i = 1; i <= n - l + 1; ++i) if(check(i, i + l - 1)){
        std::cout << i << ' ' << l << endl;
        split(i, i + l - 1);
        return ;
    }
}

int main(){
    #ifndef FILE
        freopen(".in", "r", stdin);
        freopen(".out", "w", stdout);
    #endif
    IOS();
    std::cin >> n >> l >> r;
    if(l != r){
        solve(l + (l % 2 != n % 2));
        return 0;
    }
    if(l % 2 == n % 2){
        solve(l);
        return 0;
    }
    for(int i = l; i <= n; ++i){
        for(int j = 0; j <= i - l; ++j) s.emplace_back(sg[j] ^ sg[i - j - l]);
        for(auto j : s) cnt[j]++;
        while(cnt[sg[i]]) ++sg[i];
        for(auto j : s) cnt[j]--;
        s.clear();
    }
    set.emplace(1, n);
    if(sg[n]) std::cout << "First" << endl, solve();
    else std::cout << "Second" << endl;
    int x, y;
    while(std::cin >> x >> y, x > 0) split(x, x + y - 1), solve();
    return 0;
}

// 阿乌拉,投降吧
// ......
// 阿乌拉,站在你面前的是活了上千年的魔法使