【hdu4901】The Romantic Hero 背包DP…?
2016-03-11 20:55
330 查看
Problem Description
There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.You may wonder why this country has such an interesting tradition? It has a very long story, but I won’t tell you :).
Let us continue, the party princess’s knight win the algorithm contest. When the devil hears about that, she decided to take some action.
But before that, there is another party arose recently, the ‘MengMengDa’ party, everyone in this party feel everything is ‘MengMengDa’ and acts like a ‘MengMengDa’ guy.
While they are very pleased about that, it brings many people in this kingdom troubles. So they decided to stop them.
Our hero z*p come again, actually he is very good at Algorithm contest, so he invites the leader of the ‘MengMengda’ party xiaod*o to compete in an algorithm contest.
As z*p is both handsome and talkative, he has many girl friends to deal with, on the contest day, he find he has 3 dating to complete and have no time to compete, so he let you to solve the problems for him.
And the easiest problem in this contest is like that:
There is n number a_1,a_2,…,a_n on the line. You can choose two set S(a_s1,a_s2,..,a_sk) and T(a_t1,a_t2,…,a_tm). Each element in S should be at the left of every element in T.(si < tj for all i,j). S and T shouldn’t be empty.
And what we want is the bitwise XOR of each element in S is equal to the bitwise AND of each element in T.
How many ways are there to choose such two sets? You should output the result modulo 10^9+7.
Input
The first line contains an integer T, denoting the number of the test cases.For each test case, the first line contains a integers n.
The next line contains n integers a_1,a_2,…,a_n which are separated by a single space.
n<=10^3, 0 <= a_i <1024, T<=20.
Output
For each test case, output the result in one line.Sample Input
2 3 1 2 3 4 1 2 3 3
Sample Output
1 4
Author
WJMZBMRSource
2014 Multi-University Training Contest 4Recommend
一个序列,取俩堆数a和b,使得a集合中所有元素在b集合的左边,并且a集合的异或值等于b集合的and值,求满足条件的集合数量。你跟我说这是背包…
异或运算好搞,因为两次异或等于没异或。设dp1[i][j]为前i个数异或得j的方案数,则状态转移方程:
dp1[i][j]=dp[i−1][j]+dp[i−1][j ^a[i]]
同样的思路。设dp2[i][j]是序列后i个数and得j的方案数。但没异或那么方便了…
dp2[i][a[i]]=dp2[i][a[i]]+1dp2[i][j]=dp2[i+1][j]dp2[i][j&a[i]]=dp[i][j&a[i]]+dp[i+1][j]
//还真tm是背包
然后在计算的时候…如果枚举dp1[i][j]∗dp2[i+1][j]的话…会重复…
所以要枚举一个中间值,因为异或方便所以把这个中间值放异或的集合里。
ans+=dp1[i−1][j&a[i]]∗dp2[i+1][j]
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; typedef long long LL; const int mod = 1000000007; const int SZ = 2000; int dp1[SZ][SZ],dp2[SZ][SZ]; int a[SZ]; void init() { memset(dp1,0,sizeof(dp1)); memset(dp2,0,sizeof(dp2)); } int main() { int T; scanf("%d",&T); while(T --) { init(); int n; scanf("%d",&n); for(int i = 1;i <= n;i ++) scanf("%d",&a[i]); dp1[0][0] = 1; for(int i = 1;i <= n;i ++) for(int j = 0;j < 1024;j ++) dp1[i][j] = (dp1[i - 1][j] + dp1[i - 1][j ^ a[i]]) % mod; for(int i = n;i >= 1;i --) { for(int j = 0;j < 1024;j ++) dp2[i][j] = dp2[i + 1][j]; for(int j = 0;j < 1024;j ++) dp2[i][j & a[i]] = (dp2[i][j & a[i]] + dp2[i + 1][j]) % mod; dp2[i][a[i]] ++; } int ans = 0; for(int i = 1;i <= n;i ++) for(int j = 0;j < 1024;j ++) ans = (ans + (LL)dp1[i - 1][j ^ a[i]] * dp2[i + 1][j]) % mod; printf("%d\n",ans); } return 0; }
相关文章推荐
- FZU 1686 神龙的难题(重复覆盖问题)
- 分布式配置管理系统 - Diamond
- 链表的回文结构
- 对于IOS布局的问题
- bzoj2565: 最长双回文串
- /etc/init.d/mysql: /usr/bin/my_print_defaults: /lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录
- [RxJS] Handling a Complete Stream with Reduce
- 《JAVA课程设计》实训第四天——《猜猜看》游戏
- Atitit. Atiposter 发帖机 新特性 poster new feature v7 q39
- 关于“诗词大闯关”的调查表与调查结果分析
- Set,Map,包装类工具类
- Atitit. Atiposter 发帖机 新特性 poster new feature v7 q39
- leetcode 63. Unique Paths II | Java最短代码实现
- Atitit. Atiposter 发帖机 新特性 poster new feature v7 q39
- p235习题3
- DOM children childNodes nodetype
- 单一入口机制
- 1008_第几天
- Byte、bit、bps、Bps详解
- C++中的指针和动态分配二维数组问题