2014多校4的1005

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4901

The Romantic Hero

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 393    Accepted Submission(s): 150

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
WJMZBMR
 
Source
 
Recommend
We have carefully selected several similar problems for you:  4906 4905 4904 4903 4902

题意:

给一列数,让你选两个集合,A集合所有元素下标小于B集合所有元素下标,A集合的所有元素异或等于B集合所有元素AND,两个集合都非空,求集合元素有多少种选法,MOD 10^9+7。0<=元素大小<1024,最多1000个元素。

题解:

DP!(虽然我一开始就想到DP,不过我DP功力太差,比赛中实在做不出,结束后看了http://www.cnblogs.com/avema/p/3881466.html的超快题解(虽然没写题解只有代码)才学会的)

官方题解居然是这样的“水题,大家都会做吧?”我都怕了!虽然很多人过了,好像的确很水的样子……

言归正传,我来讲一下这个DP。

f[i][j]:由0~i的元素异或得到j的种类数。

h[i][j]:由i~n-1的元素AND得到j的种类数。

g[i][j]:由i~n-1的元素,且一定包含i,AND得到j的种类数。

求出这些,最后把f[i][j]*g[i+1][j]求和就得到答案了!

这里用g而不用h,防止重复计数,非常高端。我写的时候也一直在考虑防止重复计数,结果写出来4重循环,我自己都怕,看来是我对DP的理解不够深。这个只用两重循环,快得飞起来。具体实现看代码吧,还算比较简单易懂。

代码:

 #include<stdio.h>
#include<math.h>
#include<string.h>
#include<math.h> #define ll __int64
#define usint unsigned int
#define mz(array) memset(array, 0, sizeof(array))
#define RE freopen("1.in","r",stdin)
#define WE freopen("mask.txt","w",stdout) #define maxa 1024
#define maxn 1000
#define C 1000000007 int f[maxn-][maxa],g[maxn][maxa],h[maxn][maxa]; int main() { int a[maxn],T,n;
short i,j,t;
int ans;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
for(i=; i<n; i++)
scanf("%d",&a[i]);
mz(f);
mz(g);
mz(h);
f[][a[]]=;
for(i=; i<n-; i++) {
f[i][a[i]]++;///单独一个元素的集合的情况
for(j=; j<maxa; j++) {
if(f[i-][j]) {
f[i][j]+=f[i-][j];///继承之前算好的情况(就是 不包括当前元素的情况)
f[i][j]%=C;
t=j^a[i];
f[i][t]+=f[i-][j];///由前一次的情况异或当前元素得到的情况(包括当前元素的情况)
f[i][t]%=C;
} }
} g[n-][a[n-]]=;
h[n-][a[n-]]=;
for(i=n-; i>; i--) {
g[i][a[i]]++;
h[i][a[i]]++;
for(j=; j<maxa; j++) {
if(h[i+][j]) {
h[i][j]+=h[i+][j];
h[i][j]%=C;
t=j&a[i];
h[i][t]+=h[i+][j];
h[i][t]%=C; g[i][t]+=h[i+][j];///包括当前元素的情况(g没有不包括当前元素的情况)
g[i][t]%=C;
} }
}
ans=;
for(i=; i<n-; i++)
for(j=; j<maxa; j++) {
if(f[i][j]&&g[i+][j]) {
ans+=(((ll)f[i][j])*(g[i+][j])%C);
ans%=C;
}
}
printf("%d\n",ans);
}
return ;
}

这次来个C的代码,酷不酷炫(其实没有类都可以换成C的吧

HDU4901 The Romantic Hero 计数DP的更多相关文章

  1. HDU - 4901 The Romantic Hero(dp)

    https://vjudge.net/problem/HDU-4901 题意 给n个数,构造两个集合,使第一个集合的异或和等于第二个集合的相与和,且要求第一个集合的元素下标都小于第二个集合的元素下标. ...

  2. 2014多校第四场1005 || HDU 4901 The Romantic Hero (DP)

    题目链接 题意 :给你一个数列,让你从中挑选一些数组成集合S,挑另外一些数组成集合T,要求是S中的每一个数在原序列中的下标要小于T中每一个数在原序列中下标.S中所有数按位异或后的值要与T中所有的数按位 ...

  3. HDU 4901(杭电多校训练#3 1005题)The Romantic Hero(DP)

    题目地址:HDU 4901 这题没想到最后竟然可以做出来.. .. 这题用了两次DP,先从前往后求一次异或的.再从后往前求一次与运算的. 各自是 1:求异或的时候,定义二维数组huo[1000][10 ...

  4. HDU 4901 The Romantic Hero (计数DP)

    The Romantic Hero 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/E Description There is ...

  5. HDU 4901 The Romantic Hero(二维dp)

    题目大意:给你n个数字,然后分成两份,前边的一份里面的元素进行异或,后面的一份里面的元素进行与.分的时候依照给的先后数序取数,后面的里面的全部的元素的下标一定比前面的大.问你有多上种放元素的方法能够使 ...

  6. HDU 4901 The Romantic Hero 题解——S.B.S.

    The Romantic Hero Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  7. HDOJ 4901 The Romantic Hero

    DP....扫两次合并 The Romantic Hero Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  8. HDU 4901 The Romantic Hero

    The Romantic Hero Time Limit: 3000MS   Memory Limit: 131072KB   64bit IO Format: %I64d & %I64u D ...

  9. HDU5800 To My Girlfriend 背包计数dp

    分析:首先定义状态dp[i][j][s1][s2]代表前i个物品中,选若干个物品,总价值为j 其中s1个物品时必选,s2物品必不选的方案数 那么转移的时候可以考虑,第i个物品是可选可可不选的 dp[i ...

随机推荐

  1. 看牙与IT项目

    周末为了一颗牙第五次去牙科诊所,得到的消息是:还需要观察至少2周才能做牙冠,同时发现了较深的牙结石需要做刮治,刮治疗需要2次.因此至少要再去医院3次.从去年的六月体检发现这颗牙的问题,目前最乐观估计也 ...

  2. Jenkins进阶系列之——10Publish Over SSH插件

    说明:这个插件可以通过ssh连接其他Linux机器 官方说明:Publish Over SSH 安装步骤: 系统管理→管理插件→可选插件→Artifact Uploaders→Publish Over ...

  3. 封印术:shadow dom

    置顶文章:<纯CSS打造银色MacBook Air(完整版)> 上一篇:<鼠标滚动插件smoovejs和wowjs> 作者主页:myvin 博主QQ:851399101(点击Q ...

  4. Linux 常用工具贴

    1. nmon for Linux  用于监控Linux CPU.IO.网络等,可以生产excel格式的报表  http://nmon.sourceforge.net/pmwiki.php?n=Sit ...

  5. 微信小程序剖析【下】:运行机制

    在上一篇<微信小程序「官方示例代码」浅析[上]>中,我们只是简单的罗列了一下代码,这一篇,让我们来玩点刺激的——就是看看IDE的代码,了解它是怎么运行的. 还好微信的开发团队在软件工程的实 ...

  6. [USACO2006][poj3182]The Grove(巧妙的BFS)

    题目:http://poj.org/problem?id=3182 题意:一个棋盘中间有一个联通块,给你一个起点让你从起点开始绕联通块外围一圈并回到起点,求最小步数. 分析: 首先根据数据的范围比较小 ...

  7. Linux下SVN安装配置

      第一章 安装 1. 采用源文件编译安装.源文件共两个,为:subversion-1.6.1.tar.gz (subversion 源文件)subversion-deps-1.6.1.tar.gz ...

  8. iOS彩票项目--第七天,初次读取json数据、KVC转模型技巧、运行时字典转模型以及初步对显示网页的操作并且跟踪标签

    一.初次读取json数据 二.KVC转模型技巧,这里的技巧主要解决的是字典中的key 与 模型中有的属性对应不起来的时候 的解决办法 <方法1> <方法2>运行时字典转模型,运 ...

  9. HashMap 和 HashTable区别

    HashMap 非线程安全的 HashTable线程安全的 package Collections.Map; import java.util.HashMap; public class HashMa ...

  10. Java基础-final变量和普通变量的区别

    当用final作用于类的成员变量时,成员变量(注意是类的成员变量,局部变量只需要保证在使用之前被初始化赋值即可)必须在定义时或者构造器中进行初始化赋值,而且final变量一旦被初始化赋值之后,就不能再 ...