题目链接

\(Description\)

给定\(n\)个正整数\(a_i\)。求有多少个子序列\(a_{i_1},a_{i_2},...,a_{i_k}\),满足\(a_{i_1},a_{i_2},...,a_{i_k}\) \(and\)起来为\(0\)。

\(n\leq10^6,\quad 0\leq a_i\leq10^6\)。

\(Solution\)

这个数据范围。。考虑按位容斥:

令\(g_x\)表示\(x\)的二进制表示中\(1\)的个数,\(f_x\)表示有多少个\(a_i\)满足\(a_i\&x=x\)。

想要让选出来的子序列最终\(and\)和为\(x\),那么只能从这\(f_x\)个数中选。

所以\(Ans=\sum_{x=0}^{lim}(-1)^{g_x}(2^{f_x}-1)\)。

那么如何求\(f_x\)?

\(a_i\&x=x\),即\(x\)是\(a_i\)的子集,所以对\(f_x\)枚举超集更新即可。复杂度\(O(2^nn)\)。

注意因为写法问题数组要开两倍。

又一不小心一个rank1...

//62ms	35500KB
#include <cstdio>
#include <cctype>
#include <algorithm>
#define MAXIN 500000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
#define mod 1000000007
#define lb(x) (x&-x)
#define Add(x,v) (x+=v)>=mod&&(x-=mod)
typedef long long LL;
const int N=3e6+5; int bit[N],pw[N],f[N];
char IN[MAXIN],*SS=IN,*TT=IN; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
} int main()
{
int n=read(),lim=0;
for(int t,i=1; i<=n; ++i) ++f[t=read()],lim=std::max(lim,t);
pw[0]=1;
for(int i=1; i<=n; ++i) pw[i]=pw[i-1]<<1, pw[i]>=mod&&(pw[i]-=mod);
for(int i=0; 1<<i<=lim; ++i)
for(int s=0; s<=lim; ++s)
if(!(s>>i&1)) Add(f[s],f[s|(1<<i)]);
LL ans=0;
for(int i=1; i<=lim; ++i) bit[i]=bit[i^lb(i)]^1;
for(int i=0; i<=lim; ++i) ans+=bit[i]?mod-pw[f[i]]+1:pw[f[i]]-1;
printf("%I64d\n",ans%mod); return 0;
}

Codeforces.449D.Jzzhu and Numbers(容斥 高维前缀和)的更多相关文章

  1. Codeforces 449D Jzzhu and Numbers(高维前缀和)

    [题目链接] http://codeforces.com/problemset/problem/449/D [题目大意] 给出一些数字,问其选出一些数字作or为0的方案数有多少 [题解] 题目等价于给 ...

  2. Codeforces 449D Jzzhu and Numbers

    http://codeforces.com/problemset/problem/449/D 题意:给n个数,求and起来最后为0的集合方案数有多少 思路:考虑容斥,ans=(-1)^k*num(k) ...

  3. BZOJ4036:按位或 (min_max容斥&高维前缀和)

    Description 刚开始你有一个数字0,每一秒钟你会随机选择一个[0,2^n-1]的数字,与你手上的数字进行或(c++,c的|,pascal 的or)操作.选择数字i的概率是p[i].保证0&l ...

  4. [luogu 3175] [HAOI2015]按位或(min-max容斥+高维前缀和)

    [luogu 3175] [HAOI2015]按位或 题面 刚开始你有一个数字0,每一秒钟你会随机选择一个[0,2^n-1]的数字,与你手上的数字进行按位或运算.问期望多少秒后,你手上的数字变成2^n ...

  5. luoguP3175 [HAOI2015]按位或 min-max容斥 + 高维前缀和

    考虑min-max容斥 \(E[max(S)] = \sum \limits_{T \subset S} min(T)\) \(min(T)\)是可以被表示出来 即所有与\(T\)有交集的数的概率的和 ...

  6. [Hdu-6053] TrickGCD[容斥,前缀和]

    Online Judge:Hdu6053 Label:容斥,前缀和 题面: 题目描述 给你一个长度为\(N\)的序列A,现在让你构造一个长度同样为\(N\)的序列B,并满足如下条件,问有多少种方案数? ...

  7. Codeforces 595B. Pasha and Phone 容斥

    B. Pasha and Phone time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. Codeforces Round #258 (Div. 2) 容斥+Lucas

    题目链接: http://codeforces.com/problemset/problem/451/E E. Devu and Flowers time limit per test4 second ...

  9. Relatively Prime Powers CodeForces - 1036F (莫比乌斯函数容斥)

    Relatively Prime Powers CodeForces - 1036F Consider some positive integer xx. Its prime factorizatio ...

随机推荐

  1. 常见的排序算法(直接插入&选择排序&二分查找排序)

    1.直接插入排序算法 源码: package com.DiYiZhang;/* 插入排序算法 * 如下进行的是插入,排序算法*/ public class InsertionSort {    pub ...

  2. Python交换a,b两个数值的三种方式

    # coding:utf-8 a = 1 b = 2 # 第一种方式 # t = a # 临时存放变量值 # a = b # b = t # 第二种方式 # a = a + b # a的值已经不是原始 ...

  3. C++ shut down a computer

    前阵子有朋友问我,怎么用C语言写一个小程序,控制电脑关机.这个我真的不懂,这几天闲着,就上网搜了搜,整理一下. IDE: Code::Blocks 16.01 操作系统:Windows 7 x64 # ...

  4. 正则re模块

    正则表达式的特殊字符: 语法: re.match(正则语法,字符串) # re.match() 为关键字 group(1) # 取出第一个匹配 括号中的值,1位第一个括号内的值 1. 特殊字符 1 . ...

  5. python:字符串转换成字节的三种方式及字符转码问题

    str='zifuchuang' 第一种 b'zifuchuang'第二种bytes('zifuchuang',encoding='utf-8')第三种('zifuchuang').encode('u ...

  6. Git基础(三) 跟踪文件

    检查当前文件状态 git status 跟踪新文件 git add README 状态简览 git status -s 或 git status --short 忽略文件 创建一个名为.gitigno ...

  7. 将Elasticsearch的快照备份到HDFS

    1.安装Elasticsearch插件repository-hdfs 下载地址:https://artifacts.elastic.co/downloads/elasticsearch-plugins ...

  8. 从oracle到mysql

    过去四年一直是使用oracle,现在要开始使用mysql了,对于使用中发现的不同之处,我在此记录 mysql在linux下表名区分大小写,windows下表名不区分大小写 mysql没有number类 ...

  9. [转] webpack3最新版本配置研究(五) devtool,webpack-dev-server,CommonsChunkPlugin

    devtool devtool是webpack中config自带的属性只要使用就可以了不用安装 webpack官网的解释如下 当 webpack 打包源代码时,可能会很难追踪到错误和警告在源代码中的原 ...

  10. string.format格式化字符串中转义大括号“{}”

    今天,用Java读取配置文件占位符,使用String.Format(string format,object arg0)方法.以前只知“{0}”为索引占位符(即格式项),与参数列表中的第一个对象相对应 ...