hdu 4901 The Romantic Hero 计数dp,位计算

Posted ljbguanli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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): 1128    Accepted Submission(s): 469


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
 


题意是 给n个数.

前面取随意个数 组成集合S  取在全部S中元素后面的随意个元素 T   ,S   和T 不为空集;

计算能让S集合全部元素的异或(^)后得到的数等于 T集合全部元素与(&)后得到的数.  这样取两个集合,不同的取法有多少个.


data[ i ][ j ]表示在i号元素曾经,包含i, 能通过^运算得到j的方法数

data2[ i ][ j ]表示在i号元素以后,包含i,能通过&运算得到j的方法数

data3[ i ][ j ] 表示在i号元素以后,包含i,能通过&运算得到j,且一定取了i, 的方法数.

然后,不断递推,就能够了.

#include<stdio.h>
#include<string.h>
__int64 data[2000][1200],data2[2000][1200],data3[2000][1200];
int main()
{
    int t,n,i,a[2000],j;
    __int64 ans;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        memset(data,0,sizeof(data));
        memset(data2,0,sizeof(data2));
        memset(data3,0,sizeof(data3));
        for(i=1;i<=n;i++)
        {
            if(i!=1)
            {
                for(j=0;j<1024;j++)
                {
                    if(data[i-1][j])
                    {
                        data[i][j^a[i]]+=data[i-1][j];//取了当前这个
                        data[i][j^a[i]]%=(1000000000+7);
                        data[i][j]+=data[i-1][j];//没取当前这个
                        data[i][j]%=(1000000000+7);
                    }
                }
            }
            data[i][a[i]]++;//仅仅取当前这个 前面的都没取
            data[i][a[i]]%=(1000000000+7);
        }
        ans=0;
        for(i=n;i>=1;i--)//data2  代表所有的  data3 代表取了当前这个的,避免反复计算
        {
            data3[i][a[i]]++;//仅仅取了当前这个
            data3[i][a[i]]%=(1000000000+7);
            data2[i][a[i]]++;
            data2[i][a[i]]%=(1000000000+7);
            if(i!=n)
            {
                for(j=0;j<1024;j++)
                {
                    if(data2[i+1][j])
                    {
                        data2[i][j&a[i]]+=data2[i+1][j];//取了当前这个
                        data2[i][j&a[i]]%=(1000000000+7);
                        data3[i][j&a[i]]+=data2[i+1][j];
                        data3[i][j&a[i]]%=(1000000000+7);
                        data2[i][j]+=data2[i+1][j];//没取当前这个
                        data2[i][j]%=(1000000000+7);
                    }
                }
            }
            for(j=0;j<1024;j++)
            {
                if(data3[i][j])
                {
                    ans+=data[i-1][j]*data3[i][j];
                    ans=ans%(1000000000+7);
                }
            }
        }
        printf("%I64d\n",ans);
    }
    return 0;
}



以上是关于hdu 4901 The Romantic Hero 计数dp,位计算的主要内容,如果未能解决你的问题,请参考以下文章

HDU 4901 The Romantic Hero 题解——S.B.S.

hdu 4901 The Romantic Hero 计数dp,位计算

HDU - 4901 The Romantic Hero(dp)

HDU 2669 Romantic(扩展欧几里德)

HDU 2669 Romantic(扩展欧几里德)

HDU2669 Romantic (扩展欧几里德)