Codeforces.449D.Jzzhu and Numbers(容斥 高维前缀和)
\(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(容斥 高维前缀和)的更多相关文章
- Codeforces 449D Jzzhu and Numbers(高维前缀和)
[题目链接] http://codeforces.com/problemset/problem/449/D [题目大意] 给出一些数字,问其选出一些数字作or为0的方案数有多少 [题解] 题目等价于给 ...
- Codeforces 449D Jzzhu and Numbers
http://codeforces.com/problemset/problem/449/D 题意:给n个数,求and起来最后为0的集合方案数有多少 思路:考虑容斥,ans=(-1)^k*num(k) ...
- BZOJ4036:按位或 (min_max容斥&高维前缀和)
Description 刚开始你有一个数字0,每一秒钟你会随机选择一个[0,2^n-1]的数字,与你手上的数字进行或(c++,c的|,pascal 的or)操作.选择数字i的概率是p[i].保证0&l ...
- [luogu 3175] [HAOI2015]按位或(min-max容斥+高维前缀和)
[luogu 3175] [HAOI2015]按位或 题面 刚开始你有一个数字0,每一秒钟你会随机选择一个[0,2^n-1]的数字,与你手上的数字进行按位或运算.问期望多少秒后,你手上的数字变成2^n ...
- luoguP3175 [HAOI2015]按位或 min-max容斥 + 高维前缀和
考虑min-max容斥 \(E[max(S)] = \sum \limits_{T \subset S} min(T)\) \(min(T)\)是可以被表示出来 即所有与\(T\)有交集的数的概率的和 ...
- [Hdu-6053] TrickGCD[容斥,前缀和]
Online Judge:Hdu6053 Label:容斥,前缀和 题面: 题目描述 给你一个长度为\(N\)的序列A,现在让你构造一个长度同样为\(N\)的序列B,并满足如下条件,问有多少种方案数? ...
- Codeforces 595B. Pasha and Phone 容斥
B. Pasha and Phone time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #258 (Div. 2) 容斥+Lucas
题目链接: http://codeforces.com/problemset/problem/451/E E. Devu and Flowers time limit per test4 second ...
- Relatively Prime Powers CodeForces - 1036F (莫比乌斯函数容斥)
Relatively Prime Powers CodeForces - 1036F Consider some positive integer xx. Its prime factorizatio ...
随机推荐
- django----图书管理
待完成 from django.db import models # Create your models here. class Book(models.Model): nid = models.A ...
- OrCAD Capture CIS 16.6 修改原理图的页面大小
OrCAD Capture CIS 16.6 打开需要修改的原理图. 菜单:Options > Schematic Page Properties... 看图操作...
- C++ Primer 笔记——拷贝控制
1.如果构造函数的第一个参数是自身类类型的引用,且任何额外参数都有默认值,则此构造函数是拷贝构造函数.拷贝构造函数的第一个参数必须是引用类型(否则会无限循环的调用拷贝构造函数). 2.如果没有为一个类 ...
- 论文阅读笔记二十三:Learning to Segment Instances in Videos with Spatial Propagation Network(CVPR2017)
论文源址:https://arxiv.org/abs/1709.04609 摘要 该文提出了基于深度学习的实例分割框架,主要分为三步,(1)训练一个基于ResNet-101的通用模型,用于分割图像中的 ...
- vs问题解决:an operation is not legal in the current state
debug时弹出提示框:内容有:an operation is not legal in the current state 解决方案: Go to Tools > Options > D ...
- httpstatus类的状态有哪些
HTTP Status Code 常见的状态码: HTTP: Status 200 – 服务器成功返回网页HTTP: Status 404 – 请求的网页不存在HTTP: Status 503 – 服 ...
- python练习册 0002随机生成验证
这个题需要用到random库的方法,不用就会忘,暂把random库的常用方法贴出来 import random import string # 随机整数 # randint(a, b),生成a~b之间 ...
- Git推送错误Remote: User permission denied错误解决方法
用了别的同事的电脑,推送代码,报错. 解决方法: 修改别人的密码,改成自己的账号和密码就可以了.
- 小程序:最难点For的wx:key
转自:http://www.wxappclub.com/topic/536 A:数据改变,导致重新渲染的两种情况: 1:有wx:key的情况(不重新创建,仅改变顺序) 添加元素或改变元素顺序导致数据改 ...
- 深入理解JSCore
https://blog.csdn.net/MeituanTech/article/details/82108667