hdu 4901 The Romantic Hero

來源:互聯網
上載者:User

標籤:des   style   color   java   os   strong   io   for   

The Romantic HeroTime Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1078    Accepted Submission(s): 450


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
 
AuthorWJMZBMR 



題解及代碼:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int mod=1000000007;typedef long long ll;ll dp1[1004][2050];ll dp2[1004][2050];int c[1010];void init(){    memset(dp1,0,sizeof(dp1));    memset(dp2,0,sizeof(dp2));}int main(){    int T,n,x;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        init();        int ma=-1,t;        for(int i=1;i<=n;i++)        {            scanf("%d",&c[i]);            ma=max(ma,c[i]);            if(i==n) break;            dp1[i][c[i]]++;   //記得每輸入一個數,就要記錄一下            t=min(ma*2,2048);            for(int j=0;j<=t;j++)            {                dp1[i][j]=(dp1[i][j]+dp1[i-1][j]+dp1[i-1][j^c[i]])%mod;                //printf("i:%d j:%d %d\n",i,j,dp1[i][j]);            }        }       //printf("\n");        for(int i=n;i>=2;i--)        {            dp2[i][c[i]]++;   //同上,記錄            for(int j=0;j<=t;j++)            {                dp2[i][j]+=dp2[i+1][j];                dp2[i][j&c[i]]=(dp2[i][j&c[i]]+dp2[i+1][j])%mod;            }        }        /*for(int i=n;i>=2;i--)        {            for(int j=0;j<=t;j++)            {               printf("i:%d j:%d %d\n",i,j,dp2[i][j]);            }        }        puts("");         */        ll ans=0;        for(int i=2;i<=n;i++)        {            for(int j=0;j<=t;j++)            {                if(dp1[i-1][j]&&dp2[i][j])                ans=(ans+dp1[i-1][j]*(dp2[i][j]-dp2[i+1][j]))%mod; //應為會出現重複的情況                                                                   //所以dp2[i][j]-dp2[i+1][j]                                                                  //保證每次相乘的都是新出現的結果               // printf("i:%d j:%d %d\n",i,j,ans);            }        }        printf("%I64d\n",ans);    }    return 0;}/*題目意思是給出一串數序列,問是否能利用左側的部分數進行異或,右側的數進行與,來得到相等的數。由於題目中給出的數最大為1024,所以最後總得到的結果最大也不超過2048,所以我們可以開出一個數組來記錄當我們進行到某個數時,我們能得到什麼數,且它的數目是多少。異或的操作從左向右進行,與的操作從右向左進行,最後對照相乘就行了。這裡給出兩個dp的推導公式:1.異或:dp2[i][j&c[i]]=(dp2[i][j&c[i]]+dp2[i+1][j])%mod;2.與:dp2[i][j]+=dp2[i+1][j]; dp2[i][j&c[i]]=(dp2[i][j&c[i]]+dp2[i+1][j])%mod;*轉載請註明出處,謝謝。*/


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.