程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 4901 The Romantic Hero(二維dp)

HDU 4901 The Romantic Hero(二維dp)

編輯:C++入門知識

HDU 4901 The Romantic Hero(二維dp)


題目大意:給你n個數字,然後分成兩份,前邊的一份裡面的元素進行異或,後面的一份裡面的元素進行與。分的時候按照給的先後數序取數,後面的裡面的所有的元素的下標一定比前面的大。問你有多上種放元素的方法可以使得前面異或的值和後面與的值相等。

dp[x][y] 表示走到第x步,得到y這個數字一共有多少種方法。

但是需要注意這裡得分一下,不能直接用dp數組存種數,你需要分一下從上一層過來的次數,和這一層自己可以到達的次數。然後取和的時候前後兩個集合的種數進行乘法,注意邊乘邊取余。

順便給一組數據:

4

3 3 3 3

輸出:12。

 

The Romantic Hero

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



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 2014 Multi-University Training Contest 4
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 
#define max( x, y )  ( ((x) > (y)) ? (x) : (y) )
#define min( x, y )  ( ((x) < (y)) ? (x) : (y) )
#define Mod 1000000007
#define LL long long

using namespace std;

const int maxn = 1200;
LL dp1[maxn][maxn];
LL dp2[maxn][maxn];
LL dp11[maxn][maxn];
LL dp22[maxn][maxn];

int num[maxn];

int main()
{
    int T;
    cin >>T;
    while(T--)
    {
        int n;
        scanf(%d,&n);
        for(int i = 1; i <= n; i++) scanf(%d, &num[i]);
        for(int i = 1; i <= n; i++)
            for(int j = 0; j < 1024; j++) dp1[i][j] = dp2[i][j] = 0;
        for(int i = 0; i < 1024; i++) dp11[1][i] = dp22[n][i] = 0;
        dp1[1][num[1]] ++;
        dp11[1][num[1]] ++;
        for(int i = 2; i <= n; i++)
        {
            dp1[i][num[i]]++;
            for(int j = 0; j < 1024; j++)
            {
                if(dp11[i-1][j])
                {
                    dp1[i][j^num[i]] += dp11[i-1][j];
                    dp1[i][j^num[i]] %= Mod;
                }
            }

            for(int j = 0; j < 1024; j++)
            {
                dp11[i][j] = dp1[i][j]+dp11[i-1][j];
                dp11[i][j] %= Mod;
            }
        }
        dp2[n][num[n]]++;
        dp22[n][num[n]]++;
        for(int i = n-1; i >= 1; i--)
        {
            dp2[i][num[i]] ++;
            for(int j = 0; j < 1024; j++)
            {
                if(dp22[i+1][j])
                {
                    dp2[i][j&num[i]] += dp22[i+1][j];
                    dp2[i][j&num[i]] %= Mod;
                }
            }

            for(int j = 0; j < 1024; j++)
            {
                dp22[i][j] = dp2[i][j]+dp22[i+1][j];
                dp22[i][j] %= Mod;
            }
        }
        for(int i = n-1; i >= 1; i--)
        {
            for(int j = 0; j < 1024; j++)
            {
                dp2[i][j] += dp2[i+1][j];
                dp2[i][j] %= Mod;
            }
        }
        LL sum = 0;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 0; j < 1024; j++)
            {
                if(dp1[i][j] && dp2[i+1][j])
                {
                    sum += ((dp1[i][j]%Mod)*(dp2[i+1][j]%Mod))%Mod;
                }
            }
        }
        cout<<(sum%Mod)<

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved