跳到正文

个人记录

Bitwise Operations

SpeedStar约 6 分钟
原文

这是 2025年9月3日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 6 分钟

版本

  1. v2当前
  2. v1

SOS Bit Problem

给定一个包含 nn 个整数的数组,你的任务是计算每个元素 xx 的:

  1. 有多少元素 yy 满足 x∣y=xx∣y = x
  2. 有多少元素 yy 满足 x&y=xx \& y = x
  3. 有多少元素 yy 满足 x&y≠0x \& y \neq 0

限制:

  • 1⩽n⩽2×1051 \leqslant n \leqslant 2 \times 10^5
  • 1⩽ai⩽1061 \leqslant a_i \leqslant 10^6

算法分析

  1. xx 的子集数量
  2. xx 的超集数量
  3. n−n- 满足 x&y=0x \& y = 0 的 yy 的个数,对于后者,其实就是 xx 的补集的子集数量

代码实现

C++
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

int main() {
    cin.tie(nullptr) -> sync_with_stdio(false);
    
    int n;
    cin >> n;
    
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    
    int m = 20;
    int m2 = 1<<m;
    vector<int> cnt(m2);
    rep(i, n) cnt[a[i]]++;
    
    vector<int> f = cnt;
    rep(i, m)rep(j, m2) if (j>>i&1) f[j] += f[j^1<<i];
    
    vector<int> g = cnt;
    rep(i, m)rep(j, m2) if (j>>i&1) g[j^1<<i] += g[j];
    
    for (int x : a) {
        cout << f[x] << ' ' << g[x] << ' ' << n-f[~x&m2-1] << '\n';
    }
    
    return 0;
}

And Subset Count

给你一个包含 nn 个整数的数组。你的任务是,对于每个 k=0,1,…,nk=0,1,…,n,计算数组中所有非空子集的个数,这些子集的元素按位与运算结果恰好等于 kk 。

限制:

  • 1⩽n⩽2×1051 \leqslant n \leqslant 2 \times 10^5
  • 0⩽ai⩽n0 \leqslant a_i \leqslant n

算法分析

fwt\mathrm{fwt}

代码实现

C++
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;

//const int mod = 998244353;
const int mod = 1000000007;
struct mint {
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod) {}
    mint operator-() const {
        return mint(-x);
    }
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        return mint(*this) += a;
    }
    mint operator-(const mint a) const {
        return mint(*this) -= a;
    }
    mint operator*(const mint a) const {
        return mint(*this) *= a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return *this *= a.inv();
    }
    mint operator/(const mint a) const {
        return mint(*this) /= a;
    }
};
istream& operator>>(istream& is, mint& a) {
    return is >> a.x;
}
ostream& operator<<(ostream& os, const mint& a) {
    return os << a.x;
}

int main() {
    cin.tie(nullptr) -> sync_with_stdio(false);
    
    int n;
    cin >> n;
    
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    
    int m = 18;
    int m2 = 1<<m;
    vector<int> d(m2);
    rep(i, n) d[a[i]]++;
    
    rep(i, m)rep(j, m2) if (j>>i&1) d[j^1<<i] += d[j];
    
    vector<mint> two(n+1, 1);
    rep(i, n) two[i+1] = two[i]*2;
    
    vector<mint> dp(m2);
    rep(i, m2) dp[i] = two[d[i]]-1;
    
    rep(i, m)rep(j, m2) if (j>>i&1) dp[j^1<<i] -= dp[j];
    
    rep(i, n+1) cout << dp[i] << " \n"[i == n];
    
    return 0;
}