hdu 4901 The Romantic Hero

来源:互联网 发布:noteshelf软件功能 编辑:程序博客网 时间:2024/05/17 22:35

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

1
4

【分析】
被这鬼题闹废了。。

翻译完大概就是在输入的数列中取两个集合,集合S的每个元素在数列中的下标都要比集合T中的元素的下标小,而且S集合中所有元素互相异或之后与T集合中元素互相and之后的值相同。

因为异或是可逆运算,而and不是
所以先用递推关系式找出来前i个数中异或后为j的方案数,用f储存
d同理,不过求的时候麻烦一点

//The Romantic Hero#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<algorithm>#define M(i) memset(i,0,sizeof i);#define fo(i,j,k) for(i=j;i<=k;i++)#define of(i,j,k) for(i=j;i>=k;i--)using namespace std;const int mod=1000000007;int f[1024][1100],d[1024][1100];int T,n,a[1001];int main(){    int i,j,k;    scanf("%d",&T);    while(T--)    {        M(f);        M(d);        int ans=0;        scanf("%d",&n);        fo(i,1,n)          scanf("%d",&a[i]);        f[0][0]=1;        fo(i,1,n)          fo(j,0,1023)            f[i][j]=(f[i-1][j]+f[i-1][j^a[i]])%mod;        for(i=n;i>=1;i--)        {            fo(j,0,1023) d[i][j]=d[i+1][j];            fo(j,0,1023) d[i][j&a[i]]=(d[i][j&a[i]]+d[i+1][j])%mod;            d[i][a[i]]=(d[i][a[i]]+1)%mod; //独立成为一个集合        }        fo(i,1,n)          fo(j,0,1023)            ans=(ans+(long long)f[i-1][j^a[i]]*d[i+1][j])%mod;        printf("%d\n",ans);    }    return 0;}
0 0
原创粉丝点击