[高维前缀和(SOS)][容斥 ]CF449D Jzzhu and Numbers

· · 个人记录

link

考虑容斥。

设 f[i][s] 为按位与和的值为 s ,且前 i 位的元素与 s 一样的集合的超集数量。

分类讨论第 i 位的取值:

当 s 的第 i 位为 1 时,则没有选择余地,只能从 f[i - 1][s] 这里转移。

如果 s 的第 i 位为 0 时,可以选择这位的取值,显然为 f[i - 1][s] + f[i - 1][s ^ (1 << i)]

本质上就是一个高维前缀和。

那么最后的答案就是至少偶位为 1 - 至少奇位为 1 的 。

code:

#include<bits/stdc++.h>
#define int long long 
using namespace std;

const int N = 1e6 + 10, M = 22, MAX = (1 << M), mod = 1e9 + 7;

int n, a[N], f[MAX];

int popcnt(int x){
    if(x == 0) return 0;
    return popcnt(x >> 1) + (x & 1);
}

int fast_pow(int x, int y){
    int ret = 1, base = x;
    while(y){
        if(y & 1) ret = (ret * base) % mod;
        base = (base * base) % mod;
        y >>= 1;
    }
    return ret;
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0); 
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i], f[a[i]]++;
    for(int i = 0; i < M; i++){
        for(int s = 0; s < MAX; s++){
            if((s & (1 << i)) == 0) f[s] = (f[s] + f[s ^ (1 << i)]) % mod;
        }
    }
    int ans = 0;
    for(int s = 0; s < MAX; s++){
        int cnt = popcnt(s);
        ans = (ans + (((cnt & 1) ? (mod - 1) : 1) * (fast_pow(2, f[s]) - 1 + mod)) % mod) % mod;
    }
    cout << ans;

    return 0;
}