题解:P12494 [集训队互测 2024] 数位 DP

· · 题解

结论:每次能得到的 m 一定是一段前缀 [0, x]。

归纳证明:

  1. 如果当前的操作是 \texttt{and},x\gets \min(x, c)。
  2. 如果当前的操作是 \texttt{or} 或 \texttt{xor},x\gets x \operatorname{or} c \operatorname{or} (2^{\operatorname{highbit}{(x\operatorname{and} c})}-1)。容易发现 \texttt{or} 和 \texttt{xor} 是等价的。

倒着考虑,我们已知操作完后的 x 的最小值,求操作之前的 x' 至少是多少。

  1. > 1. $c_p=0$,保持原状。 > 2. $x_p=1, c_p=1$,把 $x_p$ 修改成 $0$。 > 3. $x_p=0, c_p=1$,把 $x_p$ 以下的位全部修改成 $0$,结束判断。

所以我们可以枚举 p 满足 x_p=0,这个 p 是第一个满足 case 3 的位置,则 >p 的位置可以选择变成 0 或不变,<p 的位置全部变成 0,且这样有贡献系数 2_p。另外还有不存在 p 的情况,可以视作 p=-1,贡献系数为 1。

由此可以得到一个 O(2^{P}nqP) 的做法,其中 P=\operatorname{popcount}(m)。

考虑利用模数 2^{32} 性质,我们把 \texttt{and} 操作中 2^k-x 拆成若干个 2 的幂次之和,相当于每次可以选择 2^k,或者钦定 x 中第 p 位是 1 并乘上 -2^p 的系数。

考虑第 i 个位置的 \texttt{or} 操作,我们发现其选则的位置 p 必须 < 1\dots i-1 操作钦定最小的 x 为 1 的位 d。由于我们是倒着做的,在 i 之前我们都不需要考虑 [0, d-1] 的位置的值,所以这样选对于 [0, d-1] 的位来说贡献就是 2^d。

我们还需要计算目前 >d 的位的贡献系数。设这个位置 p,在 y 的位置第一次被钦定为必须选 1,在 x 的位置第一次大于目前的 d,那么在 [x, y] 的范围内,x_p 可以选择在任意一个 \texttt{or} 操作时变为 0,或者在 [x, y] 的范围内始终保持 1,那么贡献系数就是 cnt+1。

理论部分完毕,接下来考虑具体操作。

枚举每一个操作,维护一个状态 a_i。

我们发现对于一个状态其贡献系数一定时 2^{\sum ia_i} 的倍数,那么我们令 a_0\gets \min(a_0, 1),有值的 a 就一定是 31 的一个拆分数了,状态数不会很多。

在转移时,如果是 \operatorname{or} 操作,将系数乘 2^d 并令 a_d\gets a_d+1;如果是 \operatorname{and} 操作,可以选择系数 2^k,或者选择钦定一个位置 p 并带有 -2^p 的系数。接下来分三类讨论:

最后对于每个询问 m,对于一个钦定了 msk_i 内元素的状态 i,只要 msk_i \subseteq m 即可把状态 i 统计到答案里面。

这样这道题就做完了,复杂度 O(S(k)k(n+q))。

代码参考了 @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';
    }
}