AT_abc278_G
类型:巴巴博弈 博弈题、SG 函数。
题意
有 n 个物品排成一排,每次可以删除一个长度在 l 到 r 之间的连续区间,两个人轮流删除,问谁会赢并构造必胜方案。
思路
首先我们注意到 以此我们考虑直接计算 SG 函数,是 的,使用一些优化 (奇技淫巧) 也许可以通过。
然后我们考虑分类讨论:
- 当 时先手必胜,先手可以通过取走中间 个(
x的奇偶性与n相同)构造两个一样长的区间,然后对称的模仿后手即可; - 有可能不能构造,此时我们再用
SG函数就可以了,因为此时我们发现每一个长度的SG函数值都相等,于是考虑根据定义暴力计算SG函数的值:
如果 则先手必胜,反之则后手必胜。
然后我们找出一段区间 ,裂成 和 。为保证能够必胜,则当前所有区间的 SG 函数值的 mex 记作 mx 要能够满足:
直接用 set 维护连续段即可喵。(为什么这么像某个叫ODT的数据结构呢)
Code:
// I often find myself reminiscing about the past.
// Moments of life freeze in my mind.
// I cut, fold, and curl the time behind me, kneading it into the white clouds drifting across the sky.
// Even clouds have their distinctions: cumulus clouds are heavy, while cirrus clouds are ethereal.
// The moments in life that strike me with awe pass through my thoughts and remain unforgettable forever, while the more ordinary, everyday memories leave behind only fragments under the erosion of time.
// Reminiscence is like entering a dream—too clear, and it cannot delight my imagination; too vague, and it sinks into nothingness.
// Only the mist-shrouded mountains and rivers, the veiled woman—that perfect haziness—can satisfy my longing for beauty.
// Reminiscence always wraps me, unbidden, in the yellowed pages of the past.
// Friends parted and reunited, streets demolished and rebuilt—each clue helps me set out from a specific moment and sail upstream against the river of time.
// The days gone by cannot be relived; I am merely a passerby.
// Yet I still long to leave myself moments of leisure on every journey into memory—to pause before a scene, to gaze across the haze of years at my former self, and to savor as much sweetness as I can.
// If beautiful times once flowed through my body, I am already content.
// The past has solidified.
// I carry my memories forward, but I often neglect to tend to them, and so the memories themselves keep changing their forms.
// This brings a certain challenge to my journeys of recollection.
// 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;
}
// 阿乌拉,投降吧
// ......
// 阿乌拉,站在你面前的是活了上千年的魔法使总结
(这题看着是紫题喵,其实思维难度就是紫的喵。)
