码迷,mamicode.com
首页 > 其他好文 > 详细

HDU4901:The Romantic Hero(DP)

时间:2014-08-01 13:51:31      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:hdu   dp   

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
用DP把所有状态记录下来就行了,然后最后再把所有情况总和
时间限制3S,一开始跑了2.9S,后来加了几个判断还是要2.6S,算了,懒得再优化了
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int mod = 1000000007;
__int64 a0[1111][1111],a1[1111][1111],b1[1111][1111],b0[1111][1111],s[1111];

int main()
{
    int i,j,t,n;
    scanf("%d",&t);
    while(t--)
    {
        memset(a0,0,sizeof(a0));
        memset(a1,0,sizeof(a1));
        memset(b0,0,sizeof(b0));
        memset(b1,0,sizeof(b1));
        scanf("%d",&n);
        for(i = 1; i<=n; i++)
        {
            scanf("%d",&s[i]);
            a1[i][s[i]] = b1[i][s[i]] = 1;
        }
        for(i = 2; i<=n; i++)
            for(j = 0; j<=1024; j++)
            {
                if(a1[i-1][j] || a0[i-1][j])
                {
                    a1[i][j^s[i]] = (a1[i][j^s[i]]+a1[i-1][j]+a0[i-1][j])%mod;
                    a0[i][j] = (a0[i][j]+a1[i-1][j]+a0[i-1][j])%mod;
                }
            }
        for(i = n-1; i>=1; i--)
            for(j = 0; j<=1024; j++)
            {
                if(b1[i+1][j] || b0[i+1][j])
                {
                    b0[i][j] = (b0[i][j]+b1[i+1][j]+b0[i+1][j])%mod;
                    b1[i][j&s[i]] = (b1[i][j&s[i]]+b1[i+1][j]+b0[i+1][j])%mod;
                }
            }
        __int64 sum = 0;
        for(i = 1; i<=n; i++)
            for(j = 0; j<=1024; j++)
            {
                if(a1[i][j] && b0[i][j])
                    sum = (sum+(a1[i][j]*b0[i][j])%mod)%mod;
            }
        printf("%I64d\n",sum%mod);
    }

    return 0;
}


HDU4901:The Romantic Hero(DP),布布扣,bubuko.com

HDU4901:The Romantic Hero(DP)

标签:hdu   dp   

原文地址:http://blog.csdn.net/libin56842/article/details/38333131

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!