HDU 4901 The Romantic Hero 题解——S.B.S.
The Romantic Hero
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1675 Accepted Submission(s): 705
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
好题。
线性DP。
枚举断点,双向背包。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<queue> 7 #include<cstdlib> 8 #include<iomanip> 9 #include<cassert> 10 #include<climits> 11 #define maxn 1200 12 #define F(i,j,k) for(int i=j;i<=k;i++) 13 #define M(a,b) memset(a,b,sizeof(a)) 14 #define FF(i,j,k) for(int i=j;i>=k;i--) 15 #define inf 0x7fffffff 16 const int q=1000000007; 17 using namespace std; 18 int read(){ 19 int x=0,f=1;char ch=getchar(); 20 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 21 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 22 return x*f; 23 } 24 int T,n; 25 int a[1010]; 26 int dp[1010][1025],dp1[1010][1025],s[1010][1025]; 27 int main() 28 { 29 std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y; 30 // freopen("data.in","r",stdin); 31 // freopen("data.out","w",stdout); 32 cin>>T; 33 while(T--) 34 { 35 cin>>n; 36 for(int i=1;i<=n;i++) 37 cin>>a[i]; 38 memset(dp,0,sizeof(dp)); 39 dp[0][0]=1; 40 for(int i=1;i<=n;i++) 41 { 42 for(int j=0;j<1024;j++) 43 { 44 dp[i][j]=dp[i-1][j]+dp[i-1][j^a[i]]; 45 if(dp[i][j]>=q) dp[i][j]-=q; 46 } 47 for(int j=0;j<1024;j++) 48 s[i][j]=dp[i-1][j^a[i]]; 49 } 50 memset(dp1,0,sizeof(dp1)); 51 for(int i=n;i>=1;i--) 52 { 53 dp1[i][a[i]]++; 54 for(int j=0;j<1024;j++) 55 { 56 dp1[i][j&a[i]]=(dp1[i][j&a[i]]+dp1[i+1][j])%q; 57 dp1[i][j]=(dp1[i][j]+dp1[i+1][j])%q; 58 } 59 } 60 int ans=0; 61 for(int k=1;k<=n-1;k++) 62 { 63 for(int j=0;j<1024;j++) 64 { 65 ans=(ans+(long long)s[k][j]*dp1[k+1][j])%q; 66 if(ans>=q) ans-=q; 67 } 68 } 69 cout<<ans<<endl; 70 } 71 return 0; 72 }