HDU4901 The Romantic Hero 计数DP

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

2014 Multi-University Training Contest 4

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的理解不够深。这个只用两重循环,快得飞起来。具体实现看代码吧,还算比较简单易懂。

代码:

 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<string.h>
 4 #include<math.h>
 5
 6 #define ll __int64
 7 #define usint unsigned int
 8 #define mz(array) memset(array, 0, sizeof(array))
 9 #define RE  freopen("1.in","r",stdin)
10 #define WE  freopen("mask.txt","w",stdout)
11
12 #define maxa 1024
13 #define maxn 1000
14 #define C 1000000007
15
16 int f[maxn-1][maxa],g[maxn][maxa],h[maxn][maxa];
17
18 int main() {
19
20     int a[maxn],T,n;
21     short i,j,t;
22     int ans;
23     scanf("%d",&T);
24     while(T--) {
25         scanf("%d",&n);
26         for(i=0; i<n; i++)
27             scanf("%d",&a[i]);
28         mz(f);
29         mz(g);
30         mz(h);
31         f[0][a[0]]=1;
32         for(i=1; i<n-1; i++) {
33             f[i][a[i]]++;///单独一个元素的集合的情况
34             for(j=0; j<maxa; j++) {
35                 if(f[i-1][j]) {
36                     f[i][j]+=f[i-1][j];///继承之前算好的情况(就是 不包括当前元素的情况)
37                     f[i][j]%=C;
38                     t=j^a[i];
39                     f[i][t]+=f[i-1][j];///由前一次的情况异或当前元素得到的情况(包括当前元素的情况)
40                     f[i][t]%=C;
41                 }
42
43             }
44         }
45
46         g[n-1][a[n-1]]=1;
47         h[n-1][a[n-1]]=1;
48         for(i=n-2; i>0; i--) {
49             g[i][a[i]]++;
50             h[i][a[i]]++;
51             for(j=0; j<maxa; j++) {
52                 if(h[i+1][j]) {
53                     h[i][j]+=h[i+1][j];
54                     h[i][j]%=C;
55                     t=j&a[i];
56                     h[i][t]+=h[i+1][j];
57                     h[i][t]%=C;
58
59                     g[i][t]+=h[i+1][j];///包括当前元素的情况(g没有不包括当前元素的情况)
60                     g[i][t]%=C;
61                 }
62
63             }
64         }
65         ans=0;
66         for(i=0; i<n-1; i++)
67             for(j=0; j<maxa; j++) {
68                 if(f[i][j]&&g[i+1][j]) {
69                     ans+=(((ll)f[i][j])*(g[i+1][j])%C);
70                     ans%=C;
71                 }
72             }
73         printf("%d\n",ans);
74     }
75     return 0;
76 }

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

HDU4901 The Romantic Hero 计数DP,布布扣,bubuko.com

时间: 2024-08-03 23:53:45

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

hdu 4901 The Romantic Hero 计数dp,位计算

The Romantic Hero Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1128    Accepted Submission(s): 469 Problem Description There is an old country and the king fell in love with a devil. The d

HDU4901 The Romantic Hero DP

题意:给你n个数,问你将数分成两个数组,S,T ,T 中所有元素的需要都比S任意一个大,问你S中所有元素进行 XOR 操作和 T 中所有元素进行 &操作值相等的情况有多少种. 解题思路:两个二维DP,等于背包问题,dpy[i][j] 代表选 数组 S 前 i 个数 状态为 j 的 情况有多少种.(这个是从前往后dp的) 然后我们还需要知道 dpx[i][j] ,代表选 T 数组 i-n个数的时候状态为 j 的情况数, (从后往前dp) 答案就是中间过程中  dpx[i]][j], i 必选的那种

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

题目地址:HDU 4901 这题没想到最后居然能够做出来.... 这题用了两次DP,先从前往后求一次异或的,再从后往前求一次与运算的.分别是 1:求异或的时候,定义二维数组huo[1000][1024],前者指第几位,后者是哈希的思想,若huo[x][y]=2则表示最右边的数为第x位时,异或值为y的出现了两次,需要再定义一个hash数组,来保存前面出现的所有情况,再找有多少位的时候,用hash数组中出现的所有的值与当前的第x位的数字进行异或. 2:求与的时候,定义二维数组yu[1000][102

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

题目链接 题意 :给你一个数列,让你从中挑选一些数组成集合S,挑另外一些数组成集合T,要求是S中的每一个数在原序列中的下标要小于T中每一个数在原序列中下标.S中所有数按位异或后的值要与T中所有的数按位与的值相同,问能找出多少符合要求的组合. 思路 :比赛的时候有点没有头绪,后来二师兄想出了状态转移方程,YN又改了很多细节,最后才A的.总之是个别扭的DP..... 一开始是 _xor[i][j^a[i]] += _xor[i-1][j] :j 的下一个状态 就是异或上a[i],这个数组所代表的意思

HDU - 4901 The Romantic Hero(dp)

https://vjudge.net/problem/HDU-4901 题意 给n个数,构造两个集合,使第一个集合的异或和等于第二个集合的相与和,且要求第一个集合的元素下标都小于第二个集合的元素下标.问方案数 分析 dp来做.dp1[i][j]表示0~i的元素异或和为j的个数.dp2[i][j]表示i~n-1的元素相与和为j的个数.注意状态转移时要同时计算第i个数参与或不参与的情况,且dp1的第一维不能取到n-1,类似的,dp2的第一维不能取0.统计最终答案时需要合并,那么怎么才能防止重复呢?这

hdu 4901 The Romantic Hero(计数dp)2014多校训练第4场1005

The Romantic Hero                                                                               Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description There is an old country and the king fell in lov

HDU4901:The Romantic Hero(DP)

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 refus

HDU 4901 The Romantic Hero(DP)

HDU 4901 The Romantic Hero 题目链接 题意:给定一个序列,要求找一个分界点,然后左边选一些数异或和,和右边选一些数且和相等,问有几种方法 思路:dp,从左往右和从右往左dp,求出异或和且的个数,然后找一个分界点,使得一边必须在分界点上,一边随意,然后根据乘法原理和加法原理计算 代码: #include <cstdio> #include <cstring> typedef __int64 ll; const int N = 1024; const int

HDU 4901 The Romantic Hero(二维dp)

题目大意:给你n个数字,然后分成两份,前边的一份里面的元素进行异或,后面的一份里面的元素进行与.分的时候按照给的先后数序取数,后面的里面的所有的元素的下标一定比前面的大.问你有多上种放元素的方法可以使得前面异或的值和后面与的值相等. dp[x][y] 表示走到第x步,得到y这个数字一共有多少种方法. 但是需要注意这里得分一下,不能直接用dp数组存种数,你需要分一下从上一层过来的次数,和这一层自己可以到达的次数.然后取和的时候前后两个集合的种数进行乘法,注意边乘边取余. 顺便给一组数据: 4 3