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.
 

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

 

题意:给出一组数将其分成前后两组,问一共有多少组使得前一部分的异或值等于后一部分的与值。 

 开始想的差不多了前后各来一次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+40;
 9 const int MOD = 1e9+7;
10 LL dp1[MAX][MAX],dp2[MAX][MAX];
11 LL dp3[MAX][MAX],n;
12 LL a[MAX];
13 
14 void gao() {
15     dp1[1][a[1]]=1;
16     for(int i=2;i<=n;i++) {
17         dp1[i][a[i]]++;
18         for(int j=0;j<=1024;j++) {
19             dp1[i][j^a[i]]=(dp1[i][j^a[i]]+dp1[i-1][j])%MOD;
20             dp1[i][j]=(dp1[i][j]+dp1[i-1][j])%MOD;
21         }
22     }
23     dp2[n][a[n]]=dp3[n][a[n]]=1;
24     for(int i=n-1;i>=1;i--) {
25         dp2[i][a[i]]++; dp3[i][a[i]]++;
26         for(int j=0;j<=1024;j++) {
27             dp2[i][a[i]&j]=(dp2[i][a[i]&j]+dp3[i+1][j])%MOD;
28             dp3[i][a[i]&j]=(dp3[i][a[i]&j]+dp3[i+1][j])%MOD;
29             dp3[i][j]=(dp3[i][j]+dp3[i+1][j])%MOD;
30         }
31     }
32     return ;
33 }
34 
35 int main() {
36     int cas;
37     scanf("%d",&cas);
38     while(cas--) {
39         scanf("%I64d",&n);
40         for(int i=1;i<=n;i++) {
41             scanf("%I64d",&a[i]);
42         }
43         memset(dp1,0,sizeof(dp1));
44         memset(dp2,0,sizeof(dp2));
45         memset(dp3,0,sizeof(dp3));
46         gao();
47         LL res=0;
48         for(int i=1;i<n;i++) {
49             for(int j=0;j<1024;j++) {
50                 res=(res+dp1[i][j]*dp2[i+1][j])%MOD;
51             }
52         }
53         printf("%I64d\n",res);
54     }

55 } 

 

posted @ 2014-07-31 21:00  acvc  阅读(498)  评论(0编辑  收藏  举报