HDU 4901 the romantic hero (count DP) 2014 multi-school training 4th games 1005

Source: Internet
Author: User
The romantic hero Time Limit: 6000/3000 MS (Java/others) memory limit: 131072/131072 K (Java/Others)

Problem descriptionthere is an old country and the king fell in love with a dedevil. the dedevil 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 dedevil. also, this dedevil 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 dedevil 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 should 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 'mengda' party xiaod * o to compete in an algorithm contest.

As z * P is both handsome and talkative, he has cute 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 shoshould 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 shoshould output the result modulo 10 ^ 9 + 7.
 
Inputthe 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. 
Outputfor each test case, output the result in one line.
Sample Input
231 2 341 2 3 3
 
Sample output
1 4
 
Give n numbers and construct two sequences so that the exclusive or value of all elements in the first sequence is equal to the and (&) value of all elements in the second sequence, in addition, the subscripts of all elements in the first sequence are smaller than those of all elements in the second sequence. Evaluate the total number of constructor methods and obtain the remainder of the result 1000000007.
Although it was known to be DP during the competition, due to the weak DP skills, it was not made during the competition. Analysis: dp1 [I] [J]: from 0 ~ The elements of I are different or the number of J types is obtained.
Dp2 [I] [J]: by I ~ Element and of N-1 to obtain the number of J types. Dp3 [I] [J]: by I ~ N-1 elements, which must contain a [I], and obtain the number of J types. Find these values, and then sum dp1 [I] [J] * dp3 [I + 1] [J] to get the answer!
Here we use an array dp3 instead of directly using dp2 to prevent repeated counts.
# Include <cstdio> # include <cstring> typedef _ int64 ll; # define mod 1000000007 const int maxn = 1002; const int maxa = 1025; int dp1 [maxn] [maxa], dp2 [maxn] [maxa], dp3 [maxn] [maxa]; int A [maxn]; int main () {int t, n, I, j, T; scanf ("% d", & T); While (t --) {scanf ("% d", & N ); for (I = 0; I <n; I ++) scanf ("% d", & A [I]); memset (dp1, 0, sizeof (dp1 )); memset (dp2, 0, sizeof (dp2); memset (dp3, 0, sizeof (dp3); dp1 [0] [A [0] = 1; for (I = 1; I <n-1; I ++) {dp1 [I] [A [I] ++; // a single element forms a set for (j = 0; j <maxa; j ++) {If (dp1 [I-1] [J]) {dp1 [I] [J] + = dp1 [I-1] [J]; // do not add the I-th element for XOR, inherit the previously calculated dp1 [I] [J] % = MOD; t = J ^ A [I]; // Add the I-th element for rejection or dp1 [I] [T] + = dp1 [I-1] [J]; dp1 [I] [T] % = mod ;}} dp2 [n-1] [A [n-1] = 1; dp3 [n-1] [A [n-1] = 1; for (I = n-2; I> 0; I --) {dp2 [I] [A [I] ++; dp3 [I] [A [I] ++; // a single element forms a set for (j = 0; j <maxa; j ++) {If (dp2 [I + 1] [J]) {dp2 [I] [J] + = dp2 [I + 1] [J]; // do not add the I-th element for bitwise AND dp2 [I] [J] % = MOD; t = J & A [I]; // Add the I-th element for bitwise AND dp2 [I] [T] + = dp2 [I + 1] [J]; dp2 [I] [T] % = MOD; dp3 [I] [T] + = dp2 [I + 1] [J]; // Add the I-th element and dp3 [I] [T] % = mod ;}} int ans = 0; for (I = 0; I <n-1; I ++) {for (j = 0; j <maxa; j ++) {If (dp1 [I] [J] & dp3 [I + 1] [J]) {ans + = (LL (dp1 [I] [J]) * dp3 [I + 1] [J] % mod); ans % = mod ;}} printf ("% d \ n", ANS);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.