zoukankan      html  css  js  c++  java
  • [容斥] Codeforces 449D Jzzhu and Numbers

    题目大意

    给定一个长为 \(n(1\leq n\leq 10^6)\) 的数组 \(\{a_n\}\)\(0\leq a_i\leq 10^6\),求这个数组中有多少个非空子序列按位与起来等于 \(0\),答案对 \(10^9+7\) 取模。

    题解

    正难则反,首先考虑求出有多少个子序列按位与起来不为 \(0\),然后再用子序列总数去减即为答案。

    \(S\) 是所有二进制位的集合,\(g(T)\) 表示按位与后至少在 \(T\) 中的二进制位上为 \(1\) 的子序列的个数,则有

    \[ans=(2^n-1)-\sum_{T\subseteq S\land T\neq\emptyset}(-1)^{|T|-1} g(T) \]

    要求出 \(g(T)\),可以先用高维前缀和求出 \(T\) 的超集大小 \(f(T)\),然后 \(g(T)=2^{f(T)}-1\)

    之后再容斥即可。

    题解

    #include <bits/stdc++.h>
    using namespace std;
    
    #define LL long long
    
    template<typename elemType>
    inline void Read(elemType& T) {
        elemType X = 0, w = 0; char ch = 0;
        while (!isdigit(ch)) { w |= ch == '-';ch = getchar(); }
        while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
        T = (w ? -X : X);
    }
    
    const LL MOD = 1e9 + 7;
    const int m = 20;
    int f[1 << 20], pow2[(1 << 20) + 1];
    int n;
    
    int main() {
        Read(n);
        for (int i = 1;i <= n;++i) { int x; Read(x); ++f[x]; }
        pow2[0] = 1;
        for (int i = 1;i <= (1 << m);++i) {
            pow2[i] = pow2[i - 1] << 1;
            if (pow2[i] > MOD) pow2[i] -= MOD;
        }
        for (int i = 0;i < m;++i)
            for (int j = 0;j < (1 << m);++j)
                if (!(j & (1 << i))) f[j] = (f[j] + f[j ^ (1 << i)]) % MOD;
        for (int i = 0;i < (1 << m);++i) {
            f[i] = (pow2[f[i]] - 1);
            if (f[i] < 0) f[i] += MOD;
        }
        for (int i = 0;i < m;++i) {
            for (int j = 0;j < (1 << m);++j) {
                if (j & (1 << i)) {
                    f[j] -= f[j ^ (1 << i)];
                    if (f[j] < 0) f[j] += MOD;
                }
            }
        }
        LL ans = pow2[n] - 1 + (f[(1 << m) - 1] - f[0]);
        ans = (ans % MOD + MOD) % MOD;
        printf("%I64d\n", ans);
    
        return 0;
    }
    
  • 相关阅读:
    pytest--重复执行用例 pytest-repeat
    python中查询mongo数据库
    pytest--将参数打到在报告中,ids参数
    pytest-html报告中,添加描述
    pytest-html报告
    pytest -fixture的3种用法(autouse=True)
    httprunner 创建run.py文件,执行套件或case,并生成测试报告
    pytest-使用自定义标记mark
    pytest 函数传参和fixture传参数request
    loadrunner-脚本设计
  • 原文地址:https://www.cnblogs.com/AEMShana/p/15793102.html
Copyright © 2011-2022 走看看