Jzzhu and Numbers

本文探讨了一种涉及位运算和组合计数的问题,通过容斥原理解决特定数学挑战。介绍了一个高效的算法来计算满足特定条件的整数组数量,并提供了详细的代码实现。
Jzzhu and Numbers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Jzzhu 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\n",ret);
    return 0;
}

转载于:https://www.cnblogs.com/dyzll/p/6536551.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值