hdu 4901 The Romantic Hero(計數dp)2014多校訓練第4場1005,hdu第4場

來源:互聯網
上載者:User

hdu 4901 The Romantic Hero(計數dp)2014多校訓練第4場1005,hdu第4場
The Romantic Hero                                                                              Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Problem DescriptionThere 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.
 
InputThe 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. 
OutputFor each test case, output the result in one line. 
Sample Input

231 2 341 2 3 3
 
Sample Output
1 4
 
題意:給出n個數,構造兩個序列,使得第一個序列裡面所有元素的異或值等於第二個序列裡面所有元素的AND(&)值,並且第一個序列裡所有元素的下標都小於第二個序列裡所有元素的下標。求一共有多少種構造方法,結果對1000000007取餘。
雖然比賽時就知道是dp,但是由於dp功底太弱,導致比賽時沒有做出來。分析:dp1[i][j]:由0~i的元素異或得到j的種類數。
dp2[i][j]:由i~n-1的元素AND得到j的種類數。dp3[i][j]:由i~n-1的元素,且一定包含a[i],AND得到j的種類數。求出這些,最後把dp1[i][j]*dp3[i+1][j]求和就得到答案了!
這裡多用了一個數組dp3,而不是直接用dp2,是為了防止重複計數。
#include<cstdio>#include<cstring>typedef __int64 LL;#define mod 1000000007const int MAXN = 1002;const int MAXA = 1025;int dp1[MAXN][MAXA], dp2[MAXN][MAXA], dp3[MAXN][MAXA];int a[MAXN];int main(){    int T, n, i, j, t;    scanf("%d",&T);    while(T--) {        scanf("%d",&n);        for(i = 0; i < n; i++)            scanf("%d",&a[i]);        memset(dp1, 0, sizeof(dp1));        memset(dp2, 0, sizeof(dp2));        memset(dp3, 0, sizeof(dp3));        dp1[0][a[0]] = 1;        for(i = 1; i < n - 1; i++) {            dp1[i][a[i]]++; //單獨一個元素構成一個集合            for(j = 0; j < MAXA; j++) {                if(dp1[i-1][j]) {                    dp1[i][j] += dp1[i-1][j]; //不添加第i個元素進行異或,繼承之前算好的                    dp1[i][j] %= mod;                    t = j ^ a[i];  //添加第i個元素進行異或                    dp1[i][t] += dp1[i-1][j];                    dp1[i][t] %= mod;                }            }        }        dp2[n-1][a[n-1]] = 1;        dp3[n-1][a[n-1]] = 1;        for(i = n-2; i > 0; i--) {            dp2[i][a[i]]++;            dp3[i][a[i]]++;   //單獨一個元素構成一個集合            for(j = 0; j < MAXA; j++) {                if(dp2[i+1][j]) {                    dp2[i][j] += dp2[i+1][j];  //不添加第i個元素進行按位與                    dp2[i][j] %= mod;                    t = j & a[i]; //添加第i個元素進行按位與                    dp2[i][t] += dp2[i+1][j];                    dp2[i][t] %= mod;                    dp3[i][t] += dp2[i+1][j]; //添加第i個元素進行按位與                    dp3[i][t] %= mod;                }            }        }        int ans = 0;        for(i = 0; i < n - 1; i++) {            for(j = 0; j < MAXA; j++) {                if(dp1[i][j] && dp3[i+1][j]) {                    ans += (LL(dp1[i][j]) * dp3[i+1][j] % mod);                    ans %= mod;                }            }        }        printf("%d\n", ans);    }    return 0;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.