HDU 4901 The Romantic Hero
Posted SilverNebula
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 4901 The Romantic Hero相关的知识,希望对你有一定的参考价值。
Time Limit: 3000MS | Memory Limit: 131072KB | 64bit IO Format: %I64d & %I64u |
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.
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.
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
Source
2014 Multi-University Training Contest 4
动规。先预处理好XOR集合和AND集合在每个位置的最优解,之后枚举断点,左边是XOR集合,右边是AND集合,累加答案。
1 #include<algorithm> 2 #include<cstdio> 3 #include<cstring> 4 #include<iostream> 5 using namespace std; 6 const int q=1000000007; 7 const int mxn=1200; 8 int n,k; 9 int a[mxn]; 10 int f[mxn][mxn],t[mxn][mxn]; 11 int main(){ 12 int T; 13 scanf("%d",&T); 14 while(T--){ 15 memset(f,0,sizeof(f)); 16 memset(t,0,sizeof(t)); 17 scanf("%d",&n); 18 int i,j; 19 for(i=1;i<=n;i++)scanf("%d",&a[i]); 20 //xor 21 f[0][0]=1; //边界 22 for(i=1;i<=n;i++){ 23 for(j=0;j<1024;j++){ 24 f[i][j]=(f[i-1][j]+f[i-1][j^a[i]])%q; 25 } 26 } 27 //and 28 for(i=1;i<=n;i++)t[i][a[i]]=1; 29 for(i=n;i>=1;i--){ 30 for(j=0;j<1024;j++){ 31 t[i][j]=t[i+1][j];//不选 32 } 33 for(j=0;j<1024;j++){//选 34 t[i][j&a[i]]=(t[i][j&a[i]]+t[i+1][j])%q; 35 } 36 t[i][a[i]]=(t[i][a[i]]+1)%q; 37 } 38 // 39 int ans=0; 40 for(i=1;i<n;i++){ 41 for(j=0;j<1024;j++) 42 ans=(ans+(long long)t[i+1][j]*f[i-1][j^a[i]])%q;//累加结果 43 } 44 printf("%d\n",ans); 45 } 46 return 0; 47 }
以上是关于HDU 4901 The Romantic Hero的主要内容,如果未能解决你的问题,请参考以下文章
HDU 4901 The Romantic Hero 题解——S.B.S.
hdu 4901 The Romantic Hero 计数dp,位计算