Jzzhu and Numbers
time limit per test
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputJzzhu have n non-negative integers a1, a2, ..., an. We will call a sequence of indexes i1, i2, ..., ik (1 ≤ i1 < i2 < ... < ik ≤ n) a group of size k.
Jzzhu wonders, how many groups exists such that ai1 & ai2 & ... & aik = 0 (1 ≤ k ≤ n)? Help him and print this number modulo1000000007 (109 + 7). Operation x & y denotes bitwise AND operation of two numbers.
Input
The first line contains a single integer n (1 ≤ n ≤ 106). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 106).
Output
Output a single integer representing the number of required groups modulo 1000000007 (109 + 7).
Examples
input
3
2 3 3
output
0
input
4
0 1 2 3
output
10
input
6
5 2 0 5 2 1
output
53
分析:暴力肯定不行了,考虑容斥;
答案为ans=Σ(-1)f(x)*pow(2,num[x]),f(x)代表x中二进制1的个数,num[x]代表a[k]&x=x的个数;
求num[x]=Σcnt[k](a[k]&x=x),即为高维前缀和,与spoj tle类似;
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <bitset> #include <map> #include <queue> #include <stack> #include <vector> #define rep(i,m,n) for(i=m;i<=n;i++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define sys system("pause") const int maxn=1e6+10; const int N=2e2+10; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} int n,m,k,t,num[1<<20]; ll dp[1<<20],p[maxn]; int main() { int i,j; p[0]=1; rep(i,1,maxn-10)p[i]=p[i-1]*2%mod; scanf("%d",&n); rep(i,1,n)scanf("%d",&j),dp[j]++; rep(i,0,19) { rep(j,0,(1<<20)-1) { if((~j)&(1<<i))(dp[j]+=dp[j^(1<<i)])%=mod; if(j&(1<<i))num[j]++; } } ll ret=0; rep(i,0,(1<<20)-1) { if(num[i]&1)ret=(ret-p[dp[i]]+mod)%mod; else ret=(ret+p[dp[i]])%mod; } printf("%lld ",ret); return 0; }