HDU 4901 The Romantic Hero (计数dp)

The Romantic Hero

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


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
 

Author
WJMZBMR
 

Source
 

Recommend
We have carefully selected several similar problems for you:   6286  6285  6284  6283  6282 
 
#include <queue>
#include <iostream>
#include<cstring>
#include<stdio.h>
#define maxn 1050
#define maxa 1025
using namespace std;
const int MOD=1e9+7;
int seq[maxn],n,t;
long long  dp1[maxn][maxa],dp2[maxn][maxa],dp3[maxn][maxa];
void init()
{
    memset(dp1,0,sizeof(dp1));
    memset(dp2,0,sizeof(dp2));
    memset(dp3,0,sizeof(dp3));
}
/*
本质上有点背包dp的感觉,
将集合分成两类,在两类中可以挑选任意的数字组成子集,
然后要求左边子集的异或和等于右边子集的与和。

dp1设定为从0~i中异或和为j的计数,
dp2设定为从i~n中与和为j的计数
dp3与2类似,但要求是和为j且必须包含端点值,
方便计数。

dp2的存在是为了计算dp3的。
下面用了点背包思想。
*/
int main()
{
    ios::sync_with_stdio(false);
    int tt;cin>>tt;
    while(tt--)
    {
        init();
        cin>>n;
        for(int i=0;i<n;i++) cin>>seq[i];
        dp1[0][seq[0]]++;
        for(int i=1;i<n-1;i++)
        {
            dp1[i][seq[i]]++;
            for(int j=0;j<maxa;j++)
            {
                if(dp1[i-1][j]==0) continue;
                dp1[i][j]+=dp1[i-1][j];
                dp1[i][j]%=MOD;

                t=j^seq[i];
                dp1[i][t]+=dp1[i-1][j];
                dp1[i][t]%=MOD;
            }
        }
        dp2[n-1][seq[n-1]]=1;
        dp3[n-1][seq[n-1]]=1;
        for(int i=n-2;i>0;i--)
        {
            dp2[i][seq[i]]++;
            dp3[i][seq[i]]++;
            for(int j=0;j<maxa;j++)
            {
                if(dp2[i+1][j]==0) continue;
                dp2[i][j]+=dp2[i+1][j];
                dp2[i][j]%=MOD;

                t=j&seq[i];
                dp2[i][t]+=dp2[i+1][j];
                dp2[i][t]%=MOD;

                dp3[i][t]+=dp2[i+1][j];
                dp3[i][t]%=MOD;
            }
        }

        long long ans=0;
        for(int i=0;i<n-1;i++)
            for(int j=0;j<maxa;j++)
        {
            if(dp1[i][j]&&dp3[i+1][j])
            {
                //cout<<i<<" "<<j<<endl;
              //  cout<<dp1[i][j]<<" "<<dp3[i+1][j]<<endl;
                ans+=(dp1[i][j]%MOD)*(dp3[i+1][j]%MOD)%MOD;
                ans%=MOD;
            }
        }
        cout<<ans<<endl;

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/80387524
今日推荐