题解:P12494 [集训队互测 2024] 数位 DP
结论:每次能得到的
归纳证明:
- 如果当前的操作是
\texttt{and} ,x\gets \min(x, c) 。 - 如果当前的操作是
\texttt{or} 或\texttt{xor} ,x\gets x \operatorname{or} c \operatorname{or} (2^{\operatorname{highbit}{(x\operatorname{and} c})}-1) 。容易发现\texttt{or} 和\texttt{xor} 是等价的。
倒着考虑,我们已知操作完后的
-
-
> 1. $c_p=0$,保持原状。 > 2. $x_p=1, c_p=1$,把 $x_p$ 修改成 $0$。 > 3. $x_p=0, c_p=1$,把 $x_p$ 以下的位全部修改成 $0$,结束判断。
所以我们可以枚举
由此可以得到一个
考虑利用模数
考虑第
我们还需要计算目前
理论部分完毕,接下来考虑具体操作。
枚举每一个操作,维护一个状态
我们发现对于一个状态其贡献系数一定时
在转移时,如果是
最后对于每个询问
这样这道题就做完了,复杂度
代码参考了 @DaiRuiChen007:
#include<bits/stdc++.h>
#define rep(i, j, k) for(int i=(j); i<=(k); ++i)
#define per(i, j, k) for(int i=(j); i>=(k); --i)
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
#define debug(a...) _debug(#a, a)
using namespace std;
template<class T>ostream&operator<<(ostream&c,vector<T>v){c<<'['; for(auto x:v) c<<x<<", "; return c<<']';}
template<class T> void _debug(const char *s, T x){cout<<s<<'='<<x<<endl;}
template<class T, class... Nxt> void _debug(const char *s, T x, Nxt... nxt){
int d=0;
while(d||*s!=',') d+=*s=='(', d-=*s==')', cout<<*s++;
cout<<'='<<x<<',';
_debug(s+1, nxt...);
}
using ui=unsigned int;
// using ui=int;
using ull=unsigned long long;
using info=array<int, 31>;
mt19937_64 rnd(38478324);
const int N=1003, M=1e5+5;
int n, k, q, op[N], tot, lo[M], vl[M][32], to[M][2], msk[M];
info a[M];
ull w[N], hs[M];
ui f[M], g[M], pr[M][2];
vector<pair<int, int>> G[M];
unordered_map<ull, int> mp;
void dfs(int i, info x, int s){
if(i>k){
x[k]++, a[++tot]=x;
rep(i, 0, k) hs[tot]+=x[i]*w[i];
mp[hs[tot]]=tot;
return;
}
for(x[i]=0; (i||x[i]<=1) && s>=x[i]*i; x[i]++) dfs(i+1, x, s-x[i]*i);
}
int qry(ull x){return mp.count(x)?mp[x]:0;}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin>>n>>k>>q;
rep(i, 2, n){char c; cin>>c; op[i]=c!='A';}
rep(i, 0, k) w[i]=rnd();
dfs(0, {}, 31);
rep(i, 1, tot){
int &p=lo[i]=0;
while(!a[i][p]) p++;
to[i][1]=p?qry(hs[i]+w[p]):i, pr[i][1]=1u<<p;
to[i][0]=i, pr[i][0]=1u<<k;
rep(j, 0, k-1)
if(a[i][j]) msk[i]|=1<<j, pr[i][0]-=1u<<j;
else G[i].emplace_back(qry(hs[i]+w[j]), j);
per(j, k, 0)
vl[i][j]=vl[i][j+1]+max(0, a[i][j]-1);
}
f[qry(w[k])]=1;
int c=0;
rep(z, 1, n){
int o=op[z];
c+=o;
memcpy(g, f, sizeof(f)), memset(f, 0, sizeof(f));
rep(i, 1, tot) if(g[i]){
f[to[i][o]]+=g[i]*pr[i][o];
if(!o){
for(auto [v, j]:G[i]){
if(j<lo[i]) f[v]-=g[i]<<j;
else f[v]-=g[i]*(c-vl[i][j]+1)<<j;
}
}
}
}
while(q--){
int x; cin>>x;
ui ans=0;
rep(i, 1, tot) if((x|msk[i])==x && f[i]){
ui t=f[i];
rep(j, 0, k-1) if((x>>j&1) && !a[i][j]) t*=c-vl[i][j]+1;
ans+=t;
}
cout<<ans<<'\n';
}
}