HDU 4906 (dp胡乱搞)
The Romantic Her
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.
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.
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.
2 3 1 2 3 4 1 2 3 3
1 4
题意:给出一组数将其分成前后两组,问一共有多少组使得前一部分的异或值等于后一部分的与值。
开始想的差不多了前后各来一次dp,还有20分钟没细想,少了一个状态,就是当前的状态可以不变的。
然后需要注意的是可能集合为空,这是不允许的。
1 // by caonima
2 // hehe
3 #include <cstdio>
4 #include <cstring>
5 #include <algorithm>
6 using namespace std;
7 typedef long long LL;
8 const int MAX = 1e3+;
9 const int MOD = 1e9+;
LL dp1[MAX][MAX],dp2[MAX][MAX];
LL dp3[MAX][MAX],n;
LL a[MAX];
void gao() {
dp1[][a[]]=;
for(int i=;i<=n;i++) {
dp1[i][a[i]]++;
for(int j=;j<=;j++) {
dp1[i][j^a[i]]=(dp1[i][j^a[i]]+dp1[i-][j])%MOD;
dp1[i][j]=(dp1[i][j]+dp1[i-][j])%MOD;
}
}
dp2[n][a[n]]=dp3[n][a[n]]=;
for(int i=n-;i>=;i--) {
dp2[i][a[i]]++; dp3[i][a[i]]++;
for(int j=;j<=;j++) {
dp2[i][a[i]&j]=(dp2[i][a[i]&j]+dp3[i+][j])%MOD;
dp3[i][a[i]&j]=(dp3[i][a[i]&j]+dp3[i+][j])%MOD;
dp3[i][j]=(dp3[i][j]+dp3[i+][j])%MOD;
}
}
return ;
}
int main() {
int cas;
scanf("%d",&cas);
while(cas--) {
scanf("%I64d",&n);
for(int i=;i<=n;i++) {
scanf("%I64d",&a[i]);
}
memset(dp1,,sizeof(dp1));
memset(dp2,,sizeof(dp2));
memset(dp3,,sizeof(dp3));
gao();
LL res=;
for(int i=;i<n;i++) {
for(int j=;j<;j++) {
res=(res+dp1[i][j]*dp2[i+][j])%MOD;
}
}
printf("%I64d\n",res);
}
55 }
HDU 4906 (dp胡乱搞)的更多相关文章
- HDU 4906 Our happy ending (状压DP)
HDU 4906 Our happy ending pid=4906" style="">题目链接 题意:给定n个数字,每一个数字能够是0-l,要选当中一些数字.然 ...
- HDU 5928 DP 凸包graham
给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也 ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU 4906 状态压缩dp
Our happy ending Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- HDU 1069 dp最长递增子序列
B - Monkey and Banana Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- HDU 1160 DP最长子序列
G - FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- hdu 4826(dp + 记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4826 思路:dp[x][y][d]表示从方向到达点(x,y)所能得到的最大值,然后就是记忆化了. #i ...
- HDU 2861 (DP+打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2861 题目大意:n个位置,m个人,分成k段,统计分法.S(n)=∑nk=0CknFibonacci(k ...
- HDU 2838 (DP+树状数组维护带权排序)
Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...
随机推荐
- 洛谷P4158 [SCOI2009]粉刷匠
传送门 设$dp[i][j][k][0/1]$表示在涂点$(i,j)$,涂了$k$次,当前点的颜色是否对,最多能刷对多少个格子 首先换行的时候肯定得多刷一次 然后是如果和前一个格子颜色相同,那么当前点 ...
- (快排)51NOD 1018 排序
给出N个整数,对着N个整数进行排序 Input 第1行:整数的数量N(1 <= N <= 50000) 第2 - N + 1行:待排序的整数(-10^9 <= A[i] < ...
- 为什么前后端分离不利于seo
搜索引擎的基础爬虫的原理就是抓取你的url,然后获取你的html源代码并解析. 而你的页面通常用了vue等js的数据绑定机制来展示页面数据,爬虫获取到的html是你的模型页面而不是最终数据的渲染页面, ...
- c++病毒函数
FreeConsole(); 屏蔽输出. BlockInput(); 阻止键盘和鼠标的工作. 所需头文件: #include <windows.h> #include <Winabl ...
- php 编译时 报错 configure: error: libXpm.(a|so) not found.
编译环境 centos7 php 5.4.26 $ yum install libXpm-devel 显示已安装 百度得知 ubuntu虚拟机安装lamp遇到的问题 configure: error: ...
- http2及server push
本文主要研究下java9+springboot2+undertow2启用http2及server push maven <parent> <groupId>org.spri ...
- spring-redis-data的一个坑
事故原因: 运维报告redis内存直线上升,然后查询发现都是setrange操作,review代码,没法发现setrange操作 代码如下: redisTemplate.opsForValue().s ...
- [ SPOJ PT07J ] Query on a tree III
\(\\\) Description 其实这题才是正版的 Qtree3...... 给定 \(n\) 个点,以 \(1\) 号节点为根的树,点有点权. \(m\) 次询问 以 \(x\) 为根的子树内 ...
- 2105. [NOIP2015] 信息传递
★☆ 输入文件:2015message.in 输出文件:2015message.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] 有n个同学(编号为1到n)正在 ...
- Python学习日记之文件读取操作
Python内置了文件读写的函数open,read 用法示例: open('/home/root/files.txt ') 在打开文件后,操作完成后可以使用close()关闭文件,但比较好的文件读写方 ...