The romantic hero
Time Limit: 6000/3000 MS (Java/others) memory limit: 131072/131072 K (Java/Others)
Total submission (s): 1078 accepted submission (s): 450
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
Authorwjmzbmr
Question and code:
# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; const int mod = 1000000007; typedef long ll; ll dp1 [1004] [2050]; ll dp2 [1004] [2050]; int C [1010]; void Init () {memset (dp1, 0, sizeof (dp1); memset (dp2, 0, sizeof (dp2);} int main () {int t, n, x; scanf ("% d", & T); While (t --) {scanf ("% d", & N); Init (); int MA =-1, t; for (INT I = 1; I <= N; I ++) {scanf ("% d", & C [I]); MA = max (MA, C [I]); if (I = N) break; Dp1 [I] [C [I] ++; // remember to record T = min (MA *) for each input number; For (Int J = 0; j <= T; j ++) {dp1 [I] [J] = (dp1 [I] [J] + dp1 [I-1] [J] + dp1 [I-1] [J ^ C [I]) % MOD; // printf ("I: % d J: % d \ n", I, j, dp1 [I] [J]);} // printf ("\ n"); For (INT I = N; I> = 2; I --) {dp2 [I] [C [I] ++; // same as above, record for (Int J = 0; j <= T; j ++) {dp2 [I] [J] + = dp2 [I + 1] [J]; dp2 [I] [J & C [I] = (dp2 [I] [J & C [I] + dp2 [I + 1] [J]) % mod ;}/ * For (INT I = N; I >=2; I --) {for (Int J = 0; j <= T; j ++) {printf ("I: % d J: % d \ n", I, j, dp2 [I] [J]) ;}} puts (""); */ll ans = 0; For (INT I = 2; I <= N; I ++) {for (Int J = 0; j <= T; j ++) {If (dp1 [I-1] [J] & dp2 [I] [J]) ans = (ANS + dp1 [I-1] [J] * (dp2 [I] [J]-dp2 [I + 1] [J]) % MOD; // It should be repeated. // Therefore, dp2 [I] [J]-dp2 [I + 1] [J] // ensure that each multiplication result is a new result/ /printf ("I: % d J: % d \ n ", I, j, ANS) ;}} printf (" % i64d \ n ", ANS) ;}return 0 ;} /* the question is to give a series of numbers, and ask whether the numbers on the left side can be used for XOR, and the numbers on the right side can be used for equality. Because the maximum number given in the question is 1024, the final total result cannot exceed 2048, so we can open an array to record when we reach a certain number, what number can we get and what the number is. The XOR operation is performed from left to right, and the operation is performed from right to left, and then multiplied by comparison. The following formula is used to derive two DP values: 1. XOR: dp2 [I] [J & C [I] = (dp2 [I] [J & C [I] + dp2 [I + 1] [J]) % MOD; 2. and: dp2 [I] [J] + = dp2 [I + 1] [J]; dp2 [I] [J & C [I] = (dp2 [I] [J & C [I] + dp2 [I + 1] [J]) % MOD; * indicate the source for reprinting. Thank you. */