zoukankan      html  css  js  c++  java
  • cf449D. Jzzhu and Numbers(容斥原理 高维前缀和)

    题意

    题目链接

    给出(n)个数,问任意选几个数,它们(&)起来等于(0)的方案数

    Sol

    正解居然是容斥原理Orz,然而本蒟蒻完全想不到。。

    考虑每一种方案

    答案=任意一种方案 - 至少有(1)位为(1)的方案 + 至少有两位为(1)的方案 - 至少有三位为(1)的方案

    至少有(i)位为(1)的方案可以dp算,设(f[x])表示满足(f[x] = a_i & x = x)(a_i)的个数

    最终答案$ = (-1)^{bit(i)} f[i]$

    (f)数组可以通过高维前缀和预处理

    #include<bits/stdc++.h>
    #define Pair pair<int, int>
    #define MP make_pair
    #define fi first
    #define se second 
    using namespace std;
    const int MAXN = 3e6 + 10, mod = 1e9 + 7, B = 20;
    inline int read() {
        char c = getchar(); int x = 0, f = 1;
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x * f;
    }
    int N, a[MAXN], bit[65537], f[MAXN];
    int add(int &x, int y) {
    	if(x + y < 0) x = x + y + mod;
    	else x = (x + y >= mod ? x + y - mod : x + y);
    }
    int mul(int x, int y) {
    	return 1ll * x * y % mod;
    }
    int fp(int a, int p) {
    	int base = 1;
    	while(p) {
    		if(p & 1) base = mul(base, a);
    		a = mul(a, a); p >>= 1;
    	}
    	return base;
    }
    int get1(int x) {
    //	return __builtin_popcount(x);
    	return bit[x & 65535] + bit[x >> 16];
    }
    int main() {
    	for(int i = 1; i <= 65536; i++) bit[i] = bit[i >> 1] + (i & 1);
    	N = read();
    	for(int i = 1; i <= N; i++) a[i] = read(), f[a[i]]++;
    	int Lim = (1 << B) - 1, ans = 0;
    	for(int i = 0; i <= 20; i++)
    		for(int sta = 0; sta <= Lim; sta++) 
    			if(!(sta & (1 << i))) add(f[sta], f[sta | (1 << i)]);
    	for(int sta = 0; sta <= Lim; sta++) {
    		int k = (get1(sta) & 1) ? -1 : 1;
    		add(ans, mul(k, fp(2, f[sta])));
    	}
    	cout << ans;
        return 0;
    }
    
  • 相关阅读:
    zeplin 登录效果实现
    instanceof 是如何工作的
    JavaScript版—贪吃蛇小组件
    C#+HtmlAgilityPack—糗事百科桌面版V2.0
    Google自定义搜索引擎
    百度网盘资源搜索器
    CSS元素类型
    CSS盒子模型
    Android调用Web服务
    无法将匿名方法转换为System.Delegate
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/9915979.html
Copyright © 2011-2022 走看看