hdu4901 The Romantic Hero

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.

可以看成两部分,从左开始取^,从右开始取&。
f[i][j]表示(从左开始数)前i位得到j的方案总数。
f[i][j]=f[i-1][j]+f[i-1][j^a[i]](因为^的逆运算是^)
g[i][j]表示(从右开始数)前j位得到j的总方案数。
g[i][j]+=g[i+1][j];
g[i][j&a[i]]+=g[i+1][j];(因为&没有逆运算的性质,只能用刷表法)
g[i][a[i]]++;(因为0&a[i]=0,所以需要额外加上自己是第一个的情况)
枚举答案时要注意,为了避免重复计数【如果左边的右边界和右边的左边界离的很远的话,在中间取分界点就会重复算】,直接强行将分界线放在左集合中【也可以看成枚举的就是左集合的右端点】。

#include
#include
#define M(a) memset(a,0,sizeof(a))
const int mod=1000000007;
int a[1010],f[1010][1100],g[1010][1100]; 
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("1.txt","w",stdout);
    int i,j,k,m,n,p,q,x,y,z,T,ans;
    scanf("%d",&T);
    while (T--)
    {
        M(a);
        M(f);
        M(g);
        ans=0;
        scanf("%d",&n);
        for (i=1;i<=n;i++)
          scanf("%d",&a[i]);
        f[0][0]=1;
        for (i=1;i<=n;i++)
          for (j=0;j<=1024;j++)
            f[i][j]=(f[i-1][j]+f[i-1][j^a[i]])%mod;
        for (i=n;i>=1;i--)
        {
            //g[i][a[i]]=(g[i][a[i]]+1)%mod;
            g[i][a[i]]=1;
            for (j=0;j<=1024;j++)
            {
                g[i][j]=(g[i][j]+g[i+1][j])%mod;
                g[i][j&a[i]]=(g[i][j&a[i]]+g[i+1][j])%mod;
            }
        }
        for (i=1;i<=n;i++)
          for (j=0;j<=1024;j++)
            ans=(ans+(long long)f[i-1][j^a[i]]*g[i+1][j])%mod;
        printf("%d\n",ans);
    }
}

你可能感兴趣的:(动态规划,hdu)