【动态规划】【2014 Multi-University Training Contest 4】The Romantic Hero

来源:互联网 发布:c#获取网页源码 编辑:程序博客网 时间:2024/04/30 01:10
The Romantic Hero


Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1215    Accepted Submission(s): 510




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.
 


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.
 


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


4

 

丽洁出的题……简直了……WJMZBMR is a cute loli ……


题目大意:直接看description 的最后几段,给n个数字,在其中选若干数字组成两个集合,并且S中的每一个元素都小于T中的任意元素,使得S中元素的Xor值==T中元素的and值


思路:一道比较明显的动态规划。

一开始的思路是:f1[i][j]表示i之前的数字中,由某些数字异或得到j的方案总数,f2[i][j]表示i之后的数字中,由某些数字and得到j的方案总数。

f1[i][j]=f1[i-1][j]+f1[i-1][pre],同理,f2[i][j]=f2[i+1][j]+f2[i+1][pre](pre就是and或xor一下a[i]后等于j的那个值),

最后枚举断点,枚举xor和and的值,统计一下,就得出答案。


但是这个方案过不了第二组样例(丽洁的样例真是良心样例啊!),然后找问题,发现上述方案有bug,说明如下:


假设现在给了6个数,有一种满足题意的方案为S={a[1],a[2]},T={a[5],a[6]},对于这一种可行的方案,我们在最后做统计的时候,枚举到i=3,4的时候都会算进去,重复了,所以导致错误,然后想办法改进。


设计f3[i][j]表示在i之后的数字中,用到a[i],and值得j的方案总数,在维护f2的时候顺手把f3也维护出来,统计的时候就f1和f3统计,这样就避免了上述问题,可以AC。

当然,f3也可以表示在i之前的数字中,用到a[i],xor值得j的方案总数,这个都是可以的。


 

#include <iostream>#include <cstring>#include <cstdio>using namespace std;long long f1[1000+10][1024+10],f2[1000+10][1024+10],f3[1000+10][1024+10];int t,n,a[1000+10];const long long mod=1e9+7;int main(){    scanf("%d",&t);    while (t--){        memset(f1,0,sizeof(f1));        memset(f2,0,sizeof(f2));        memset(f3,0,sizeof(f3));        scanf("%d",&n);        for (int i=0;i<n;i++)            scanf("%d",a+i);        f1[0][a[0]]=1;        for (int i=1;i<n-1;i++){            f1[i][a[i]]++;            for (int j=0;j<=1024;j++){                if (f1[i-1][j]){                    f1[i][j]=(f1[i][j]+f1[i-1][j])%mod;                    f1[i][j^a[i]]=(f1[i][j^a[i]]+f1[i-1][j])%mod;                }            }        }        f2[n-1][a[n-1]]=1;        f3[n-1][a[n-1]]=1;        for (int i=n-2;i>0;i--){            f2[i][a[i]]++;            f3[i][a[i]]++;            for (int j=0;j<=1024;j++){                if (f2[i+1][j]){                    f2[i][j]=(f2[i][j]+f2[i+1][j])%mod;                                        f2[i][j&a[i]]=(f2[i][j&a[i]]+f2[i+1][j])%mod;                    f3[i][j&a[i]]=(f3[i][j&a[i]]+f2[i+1][j])%mod;                }            }        }        long long ans=0;        for (int i=0;i<n-1;i++)            for (int j=0;j<=1024;j++){                if (f1[i][j]!=0 && f3[i+1][j]!=0){                    ans=(ans+f1[i][j]*f3[i+1][j])%mod;                }            }        printf("%I64d\n",ans);    }    return 0;}

0 0