题目大意
定义一个 01 串集合 是好的:
- 所有元素长度在 之间。
- 对于所有 , 的所有前缀不属于 。
给定初始值 ,Alice 和 Bob 轮流往 中插入一个字符串,谁不能保持 是好的就输了,求赢家。
数据范围:。
思路分析
先建立 Trie 树,那么 Trie 上的点和所有叶子的子树不能选。
那么剩下来的就是一些一度点的空子树,显然这些子树之间的博弈是互不影响的,求出每个子树的 SG 值后异或起来即可。
我们只要求 表示深度为 的空 Trie 上的博弈的 SG 函数,枚举插入字符串深度得到:
打表发现 ,可以归纳法证明,直接求值即可。
时间复杂度 。
代码呈现
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN=1e5+5;
int n,tr[MAXN][2],tot=1; ll m,ans=0;
ll lowbit(ll x) { return x&-x; }
void dfs(int u,int d) {
if(d==m) return ;
for(int k:{0,1}) {
if(tr[u][k]) dfs(tr[u][k],d+1);
else ans^=lowbit(m-d);
}
}
signed main() {
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=1;i<=n;++i) {
string str; cin>>str; int p=1;
for(char c:str) {
if(!tr[p][c-'0']) tr[p][c-'0']=++tot;
p=tr[p][c-'0'];
}
}
dfs(1,0);
cout<<(ans?"Alice":"Bob")<<"\n";
return 0;
}