hdu4901 The Romantic Hero 计数dp

The Romantic Hero

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


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,最近貌似见过好多道了,一直不会,今天好好看了一下

题意

将数列分为两部分,左边取一些数,右边取一些数,左边取出来的数进行异或,右边取出来的数进行按位和,看有多少种取法

4个dp数组

a1[i][j]前i个数,第i个数必须取,异或和为j有多少种情况

a0[i][j]前i个数,第i个数不取,异或和为j有多少种情况

b1[i][j]从后往前数到i,第i个数必须取,按位和为j有多少种情况

b0[i][j]从后往前数到i,第i个数不取,按位和为j有多少种情况

答案是 ans+=a1[i][j]*b0[i][j];

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 using namespace std;
 6 long long a1[1001][1025],a0[1001][1025],b0[1001][1025],b1[1001][1025],mod=1000000007;
 7 int s[1001];
 8 int n,T;
 9 int main()
10 {
11     cin>>T;
12     while(T--)
13     {
14         memset(a1,0,sizeof(a1));
15         memset(a0,0,sizeof(a0));
16         memset(b0,0,sizeof(b0));
17         memset(b1,0,sizeof(b1));
18         cin>>n;
19         for(int i=1;i<=n;i++)
20         {
21             scanf("%d",&s[i]);
22             a1[i][s[i]]=1;
23             for(int j=0;j<1024;j++)
24             {
25                 a1[i][j^s[i]]=(a1[i][j^s[i]]+a1[i-1][j]+a0[i-1][j])%mod;
26                 a0[i][j]=(a0[i][j]+a0[i-1][j]+a1[i-1][j])%mod;
27             }
28         }
29         for(int i=n;i>=1;i--)
30         {
31             b1[i][s[i]]=1;
32             for(int j=0;j<1024;j++)
33             {
34                 b1[i][j&s[i]]=(b1[i][j&s[i]]+b1[i+1][j]+b0[i+1][j])%mod;
35                 b0[i][j]=(b0[i][j]+b0[i+1][j]+b1[i+1][j])%mod;
36             }
37         }
38         long long ans=0;
39         for(int i=1;i<n;i++)
40         {
41             for(int j=0;j<1024;j++)
42             {
43                 if(a1[i][j]&&b0[i][j])
44                 {
45                     ans=(ans+a1[i][j]*b0[i][j])%mod;
46                 }
47             }
48         }
49         cout<<ans<<endl;
50     }
51     return 0;
52 }

 

posted @ 2016-07-29 23:03  岚之川  阅读(128)  评论(0编辑  收藏  举报