【bzoj3866】The Romantic Hero dp
来源:互联网 发布:网络爬虫用什么语言好 编辑:程序博客网 时间:2024/05/12 11:43
题目描述
给你n个数,从中选出两个不相交非空集合S和T,使得S中的每一个元素都在T集合的前面,并且S集合中的所有数的亦或等于T集合中的所有数的与,求方案数 mod 10^9+7。
输入
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.
输出
For each test case, output the result in one line.
样例输入
2
3
1 2 3
4
1 2 3 3
样例输出
1
4
题解:是一道dp常规题,思路很常规,但并不简单,特别是最后的去重,不细心的话很容易忽略就连样例都过不了。s【i】【j】表示前i位选出数^得到j的方案数,t【i】【j】表示后i位选数&得到j的方案数,最后通过乘法原理得出答案。,一定注意最后的ss【i】【0】--去重!!!
总结:dp很多题都是求前i个数……后i个数……,难点就是怎么讲前后连接,而求前i个数……、后i个数……通常都要枚举中间值k。
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#define N 30000#define mod 1000000007using namespace std;int n,date[N],T;int ss[1024][1024],st[1024][1024],fs[1024][1024],ft[1024][1024],ans;int main(){scanf("%d",&T);while(T--){scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d",&date[i]);memset(ss,0,sizeof(ss));memset(st,0,sizeof(st));memset(fs,0,sizeof(fs));memset(ft,0,sizeof(ft));ss[0][0]=1;st[n+1][1023]=1;for(int i=1;i<=n;i++){for(int j=0;j<1024;j++) fs[i][j^date[i]]=(fs[i][j^date[i]]+ss[i-1][j])%mod;for(int j=0;j<1024;j++) ss[i][j]=(ss[i-1][j]+fs[i][j])%mod;}for(int i=n;i;i--){for(int j=0;j<1024;j++) ft[i][j&date[i]]=(ft[i][j&date[i]]+st[i+1][j])%mod; for(int j=0;j<1024;j++) st[i][j]=(st[i+1][j]+ft[i][j])%mod;}ans=0;for(int i=1;i<n;i++){ss[i][0]--;//!!!for(int j=0;j<1024;j++) ans = ( ans + 1ll*ss[i][j]*ft[i+1][j] )%mod;}printf("%d\n",ans);}return 0;}/*231 2 341 2 3 3*/
阅读全文
0 0
- 【bzoj3866】The Romantic Hero dp
- HDU4901:The Romantic Hero(DP)
- HDU4901-The Romantic Hero(DP)
- 【BZOJ3866】The Romantic Hero “再不刷它就土了”系列
- HDU-4901 The Romantic Hero DP
- HDU 4901 The Romantic Hero(二维dp)
- HDU 4901 The Romantic Hero 简单DP
- HDU 4901 The Romantic Hero(DP)
- hdu 4901 The Romantic Hero(dp)
- HDU4901 The Romantic Hero 计数DP
- HDU 4901 The Romantic Hero 计数DP
- POJ 4901 The Romantic Hero DP
- HDOJ 4901 - The Romantic Hero 简单dp
- hdu 4901 The Romantic Hero (dp)
- 【hdu4901】The Romantic Hero 背包DP…?
- HDU 4901 The Romantic Hero DP(计数)
- hdu4901The Romantic Hero dp
- The Romantic Hero
- 嵌入式开发(ARM9)学习笔记(六)-Linux远程登录
- C语言中内存分布及程序运行中的BSS段、数据段、代码段、堆和栈
- Latex中文utf-8编码的三种方式
- maven学习
- JAVA_HOME
- 【bzoj3866】The Romantic Hero dp
- 顺序栈操作
- lucene学习
- java.lang.OutOfMemoryError: Java heap space解决方法
- lucene查询过程
- docker常用命令总结
- eclipse正则
- Jedis接口分类说明
- windows环境下运行测试redis