题目链接: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): 784 Accepted Submission(s): 327
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.
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.
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
Recommend
这个题通过动态规划解决,正向扫一遍异或的情况,然后再反向扫一遍与的情况。
LL dpx[1010][1024][2]; //dpx[i][j]表示下标在1~i,按位异或结果为j的有几种情况。 0表示不包括A[i],1表示包括包括A[i]
LL dpa[1010][1024][2]; //dpa[i][j]表示下标>i,按位与结果为j的有几种情况。
//#pragma comment(linker, "/STACK:36777216")
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
#define MAXN 2000
#define MAXM 3000
#define LL long long
#define MOD 1000000007
#define CLR(A) memset(A,0,sizeof(A))
LL dpx[1010][1024][2];
LL dpa[1010][1024][2];
int a[MAXN];
inline void check(LL &t){while(t>=MOD) t-=MOD;}
int main(){
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
CLR(dpx);CLR(dpa);
for(int i=1;i<=n;++i) scanf("%d",&a[i]);
for(int i=1;i<n;++i){//从前往后
dpx[i][a[i]][1]+=1;
for(int j=0;j<1024;++j){
dpx[i+1][j^a[i+1]][1]+=dpx[i][j][1];
dpx[i+1][j^a[i+1]][1]+=dpx[i][j][0];
dpx[i+1][j][0]+=dpx[i][j][1];
dpx[i+1][j][0]+=dpx[i][j][0];
check(dpx[i+1][j^a[i+1]][1]);
check(dpx[i+1][j][0]);
}
}
for(int i=n;i>1;--i){//从后往前
dpa[i][a[i]][1]+=1;
for(int j=0;j<1024;++j){
dpa[i-1][j&a[i-1]][1]+=dpa[i][j][1];
dpa[i-1][j&a[i-1]][1]+=dpa[i][j][0];
dpa[i-1][j][0]+=dpa[i][j][1];
dpa[i-1][j][0]+=dpa[i][j][0];
check(dpa[i-1][j&a[i-1]][1]);
check(dpa[i-1][j][0]);
}
}
LL ret=0;
for(int i=1;i<n;++i){
for(int j=0;j<1024;++j){
ret=(ret+dpx[i][j][1]*(dpa[i+1][j][0]+dpa[i+1][j][1]))%MOD;
}
}
printf("%I64d\n",ret);
}
return 0;
}