CF850E Random Elections 题解

· · 题解

简要题意:

给定一个长度为 2^n 的 01 序列 f_0,f_1,\dots,f_{2^n-1},并随机 n 个长度为 3 的排列 p_0,p_1,\dots,p_{n-1}。

考虑三个初始为 0 的整数 a,b,c,并对于每个小于 n 的非负整数 i 进行下面的操作:

  • 若 p_{i,1}>p_{i,2},则 a \leftarrow a+2^i。
  • 若 p_{i,2}>p_{i,3},则 b \leftarrow b+2^i。
  • 若 p_{i,3}>p_{i,1},则 c \leftarrow c+2^i。

求操作结束后不满足 f_a=f_b=f_c 的概率与 6^n 的乘积。答案对 10^9+7 取模。

转化一下即可得到,我们需要求出所有随机方案中,不满足 f_a=f_b=f_c 的方案数。

考虑排列实际上带来的限制是什么:不能同时满足 p_{i,1}>p_{i,2} 和 p_{i,2}>p_{i,3} 和 p_{i,3}>p_{i,1},也不能同时满足 p_{i,1}<p_{i,2} 和 p_{i,2}<p_{i,3} 和 p_{i,3}<p_{i,1};也就是说,对于每个二进制位,a,b,c 不能全为 0 或全为 1;所以,a,b,c 需要满足 a \lor b \lor c=2^n-1 且 a \land b\land c=0。

那么现在问题变成了,求有多少个 0 \le a,b,c \lt 2^n,满足 a \lor b \lor c=2^n-1 和 a \land b\land c=0,并且不满足 f_a=f_b=f_c。

显然,(f_a,f_b,f_c) 的值可以是 (1,0,1),(1,0,0),(0,1,1),(0,1,0),(1,1,0),(0,0,1)。由于 a,b,c 是等价的,不妨钦定 f_a=1 且 f_b=0,此时计算出的方案包含 (f_a,f_b,f_c)=(1,0,0) 和 (f_a,f_b,f_c)=(1,0,1) 的情况,再乘上 3 即可得到包含所有情况的答案。

于是,现在相当于需要从所有满足 f_x=1 的 x 中选一个当 a,从所有满足 f_y=0 的 y 中选一个当 b,而 c 只需要满足每一位上不是三个 0 或三个 1 即可。

仍然分别考虑每一位:如果 a 和 b 在这一位上的值相等,则 c 在这一位上只有一种选择方案;而如果 a 和 b 在这一位上的值不同,则 c 在这一位上有两种选择方案。于是可以得到,当 a 和 b 已经确定时,c 的选择方案有 2^{\text{popcount}(a \oplus b)} 种。

那么现在只需要对于每一个 x \in [0,2^n) 求出,有多少对 (a,b) 满足 f_a=1 且 f_b=0 且 a\oplus b=x。设 p_i=[f_i=1],q_i=[f_i=0],对 p 和 q 做一个异或卷积即可。记得给此时的答案乘上 3。

时间复杂度 \mathcal O(2^nn)。

const int N=(1<<20)+5,mod=1e9+7;
int n,V,p[N],q[N],g[N],ans;
string s;
int ksm(int a,int b){
    int res=1;
    while(b){
        if(b&1) res=1ll*res*a%mod;
        a=1ll*a*a%mod,b>>=1;
    }
    return res;
}
int ad(int a,int b){
    a+=b;
    if(a>=mod) a-=mod;
    return a;
}
void add(int &a,int b){
    a+=b;
    if(a>=mod) a-=mod;
}
int popcount(int x){
    int res=0;
    while(x){
        if(x&1) res++;
        x>>=1;
    }
    return res;
}
void Xor(int V,int *f,int c){
    for(int k=1;k<V;k<<=1){
        for(int i=0;i<V;i++){
            if(i&k) continue;
            int x=f[i],y=f[i^k];
            f[i]=ad(x,y),f[i^k]=ad(x,mod-y);
        }
    }
    for(int i=0;i<V;i++) f[i]=1ll*f[i]*c%mod;
}
void solve(){
    cin>>n>>s,V=1<<n;
    for(int i=0;i<V;i++) p[i]=(s[i]=='1'),q[i]=(s[i]=='0');
    Xor(V,p,1),Xor(V,q,1);
    for(int i=0;i<V;i++) g[i]=1ll*p[i]*q[i]%mod;
    Xor(V,g,ksm(V,mod-2));
    for(int i=0;i<V;i++) add(ans,1ll*g[i]*ksm(2,popcount(i))%mod);
    cout<<3ll*ans%mod<<endl;
}