HDU 4901 The Romantic Hero (计数DP)

The Romantic Hero

题目链接:

http://acm.hust.edu.cn/vjudge/contest/121349#problem/E

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 zp come again, actually he is very good at Algorithm contest, so he invites the leader of the 'MengMengda' party xiaodo 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的左边. 求能找出多少组子集,满足S中所有元素的异或(XOR)和T中所有元素的于(AND)相等.
##题解: 由于元素的大小只有1024,所以可以直接DP记录这一状态. dp1[i][j]: 前i个数的异或值为j的方式数. dp2[i][j]: 后n-i个数(i~n)的与值为j的方式数. 直接利用上面两组值可能会导致重复计数. 所以还需维护: dp3[i][j]: 前i个数的异或值为j,且一定包含a[i]的方式数.
对dp3[i][j]*dp2[i+1][j]求和即为最终结果. 注意考虑:集合非空(在初始化的时候考虑).

##代码: ``` cpp #include #include #include #include #include #include #include #include #include #define LL long long #define eps 1e-8 #define maxn 1100 #define mod 1000000007 #define inf 0x3f3f3f3f #define IN freopen("in.txt","r",stdin); using namespace std;

int n;
int num[maxn];
LL dp1[maxn][maxn];
LL dp2[maxn][maxn];
LL dp3[maxn][maxn];

int main(int argc, char const *argv[])
{
//IN;

int t; cin >> t;
while(t--)
{
    cin >> n;
    for(int i=1; i<=n; i++)
        scanf("%d", &num[i]);
    memset(dp1, 0, sizeof(dp1));
    memset(dp2, 0, sizeof(dp2));
    memset(dp3, 0, sizeof(dp3));

    dp1[1][num[1]] =1LL; 
    for(int i=2; i<=n; i++) {
        dp1[i][num[i]] = (dp1[i][num[i]] + 1LL) % mod;
        for(int j=1024; j>=0; j--) {
            dp1[i][j] = (dp1[i][j] + dp1[i-1][j]) % mod;
            dp1[i][j^num[i]] = (dp1[i][j^num[i]] + dp1[i-1][j]) % mod;
        }
    }

    dp2[n][num[n]] = 1LL;
    for(int i=n-1; i>=1; i--) {
        dp2[i][num[i]] = (dp2[i][num[i]] + 1LL) % mod;
        for(int j=1024; j>=0; j--) {
            dp2[i][j] = (dp2[i][j] + dp2[i+1][j]) % mod;
            dp2[i][j&num[i]] = (dp2[i][j&num[i]] + dp2[i+1][j]) % mod;
        }
    }

    dp3[1][num[1]] = 1LL;
    for(int i=2; i<=n; i++) {
        dp3[i][num[i]] = (dp3[i][num[i]] + 1LL) % mod;
        for(int j=1024; j>=0; j--) {
            dp3[i][j^num[i]] = (dp3[i][j^num[i]] + dp1[i-1][j]) % mod;
        }
    }

    LL ans = 0;
    for(int i=1; i<n; i++) {
        for(int j=1024; j>=0; j--) {
            ans = (ans + dp3[i][j]*dp2[i+1][j]) % mod;
        }
    }

    printf("%I64d\n", ans);
}
return 0;

}

posted @ 2016-07-29 17:39  Sunshine_tcf  阅读(222)  评论(0编辑  收藏  举报