codeforces 449D DP+容斥

Jzzhu and Numbers

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status

Appoint description: 
System Crawler  (2014-07-20)

Description

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).

Sample Input

Input

32 3 3

Output

0

Input

40 1 2 3

Output

10

Input

65 2 0 5 2 1

Output

53
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5
 6 typedef __int64 LL;
 7 const LL mod=1000000007;
 8 const int maxn=1<<20;
 9 int dp[20][1<<20];
10 int a[1<<20];
11
12 LL pow_mod(LL a,LL b)
13 {
14     LL ret=1;a%=mod;
15     while(b)
16     {
17         if(b&1) ret=ret*a%mod;
18         a=a*a%mod;
19         b>>=1;
20     }
21     return ret;
22 }
23
24 int main()
25 {
26     int n,i,j;
27     int ans,t,tt;
28     scanf("%d",&n);
29     for(i=1;i<=n;i++) scanf("%d",&a[i]);
30     memset(dp,0,sizeof(dp));
31     for(i=1;i<=n;i++)
32     {
33         if(a[i]&1)
34         {
35             dp[0][ a[i] ]++;dp[0][ a[i]^1 ]++;
36         }
37         else dp[0][ a[i] ]++;
38     }
39     for(i=0;i<19;i++)
40         for(j=0;j<maxn;j++)
41         {
42             t=1<<(i+1);
43             if(j&t)
44             {
45                 dp[i+1][j]+=dp[i][j];dp[i+1][ j-t ]+=dp[i][j];
46             }
47             else dp[i+1][j]+=dp[i][j];
48         }
49     ans=0;
50     for(j=0;j<maxn;j++)
51     {
52         t=1;
53         for(i=0;i<20;i++)
54             if((1<<i)&j)
55                 t=-t;
56         tt=pow_mod(2,dp[19][j]);
57         tt--;
58         ans=((ans+t*tt)%mod+mod)%mod;
59     }
60     printf("%d\n",ans);
61     return 0;
62 }

codeforces 449D DP+容斥

时间: 2024-08-07 07:31:46

codeforces 449D DP+容斥的相关文章

【BZOJ 4665】 4665: 小w的喜糖 (DP+容斥)

4665: 小w的喜糖 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 94  Solved: 53 Description 废话不多说,反正小w要发喜糖啦!! 小w一共买了n块喜糖,发给了n个人,每个喜糖有一个种类.这时,小w突发奇想,如果这n个人相互交换手中的糖,那会有多少种方案使得每个人手中的糖的种类都与原来不同. 两个方案不同当且仅当,存在一个人,他手中的糖的种类在两个方案中不一样. Input 第一行,一个整数n 接下来n行,每行一个整数

bzoj 3622 DP + 容斥

LINK 题意:给出n,k,有a,b两种值,a和b间互相配对,求$a>b$的配对组数-b>a的配对组数恰好等于k的情况有多少种. 思路:粗看会想这是道容斥组合题,但关键在于如何得到每个a[i]大于b的组数. 不妨从整体去考虑,使用$f[n][j]$代表前n个中有j组$a[i]>b[i]$,很容易得到转移式$f[n][j]=f[n-1][j]+f[n-1][j-1]*(cnt[n]-(j-1))$,其中$cnt[i]$为比a[i]小的b[]个数 但是仔细思考该式子含义会发现,$f[n][j

[Luogu P1450] [HAOI2008]硬币购物 背包DP+容斥

题面 传送门:https://www.luogu.org/problemnew/show/P1450 Solution 这是一道很有意思的在背包里面做容斥的题目. 首先,我们可以很轻松地想到暴力做背包的做法. 就是对于每一次询问,我们都做一次背包. 复杂度O(tot*s*log(di)) (使用二进制背包优化) 显然会T得起飞. 接下来,我们可以换一种角度来思考这个问题. 首先,我们可以假设没有每个物品的数量的限制,那么这样就会变成一个很简单的完全背包问题. 至于完全背包怎么写,我们在这里就不做

Codeforces 611C New Year and Domino DP+容斥

"#"代表不能放骨牌的地方,"."是可以放 500*500的矩阵,q次询问 开两个dp数组,a,b,a统计横着放的方案数,b表示竖着放,然后询问时O(1)的,容斥一下, 复杂度O(n^2+q) #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<cstdlib> #include<cmat

codeforces 342D Xenia and Dominoes(状压dp+容斥)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Xenia and Dominoes Xenia likes puzzles very much. She is especially fond of the puzzles that consist of domino pieces. Look at the picture that shows one of such puzzles. A puzzle is a 3 ×

[BZOJ 1042] [HAOI2008] 硬币购物 【DP + 容斥】

题目链接:BZOJ - 1042 题目分析 首先 Orz Hzwer ,代码题解都是看的他的 blog. 这道题首先使用DP预处理,先求出,在不考虑每种硬币个数的限制的情况下,每个钱数有多少种拼凑方案. 为了避免重复的方案被转移,所以我们以硬币种类为第一层循环,这样阶段性的增加硬币. 一定要注意这个第一层循环要是硬币种类,并且初始 f[0] = 1. f[0] = 1; for (int i = 1; i <= 4; ++i) { for (int j = B[i]; j <= MaxS; +

HDU 4632 Palindrome subsequence (区间dp 容斥定理)

Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/Others) Total Submission(s): 2610    Accepted Submission(s): 1050 Problem Description In mathematics, a subsequence is a sequence that can be derived

HDU 5838 (状压DP+容斥)

Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的点.   n<=5 , m<=5. 解题分析 考虑从1~n*m,从小到大依次填数,则如果某个位置编号为X且该位置还未填数,那么其周围的点均不能填数. 令dp[i][j]表示填到第i个数,状态为j . 令X的个数为cnt,那么 j ∈[ 0 , 1<<cnt). 一种情况为第i个数填在

CF285E Positions in Permutations(dp+容斥)

题意,给定n,k,求有多少排列是的 | p[i]-i |=1 的数量为k. Solution 直接dp会有很大的后效性. 所以我们考虑固定k个数字使得它们是合法的,所以我们设dp[i][j][0/1][0/1]表示前i个数,填了j个数,当前位置有没有被选,下一位有没有被选,这样做的话,转移会比较简单. 那么除去这j个数,剩下的数随便填,乘上全排列就好了. 但这样会多算. 然后这种问题有一个容斥模型,直接套上就好了. #include<iostream> #include<cstdio&g