HDU 4901 the romantic hero

Source: Internet
Author: User
Tags bitwise
Time Limit: 6000/3000 MS (Java/others) memory limit: 131072/131072 K (Java/Others)
Total submission (s): 1367 accepted submission (s): 581


Problem descriptionthere is an old country and the king fell in love with a dedevil. the dedevil 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 dedevil. also, this dedevil 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 dedevil 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 should 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 'mengda' party xiaod * o to compete in an algorithm contest.

As z * P is both handsome and talkative, he has cute 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 shoshould 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 shoshould 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
Give n numbers and construct two sequences, S and T, so that the exclusive or value of all elements in the first sequence is equal to the and (&) value of all elements in the second sequence, in addition, the subscripts of all elements in the first sequence are smaller than those of all elements in the second sequence. Evaluate the total number of constructor methods and obtain the remainder of the result 1000000007.
Idea: DP [I] [J] indicates that the first I elements are different or equal to the number of J types, DP [I] [J] indicates the number of the next I element and the number of classes equal to J. It is easy to think of ANS = DP [I] [J] * DP [n-I] [j] (0 <I <n, 0 <= j <= 1024). However, this is incorrect because it will repeat. For Example 2: you can calculate DP [2] [3] = 1, that is, S = {1, 2}. At this time, DP [2] [3] = 3, that is, t = {3}, {3}, {3, 3} in this case, 1*3 = 3 types of DP [3] [3] = 2 that is, S = {1, 2} and {3}. In this case, DP [1] [3] = 1. T = {3} in this case, there are 2*1 = 2, so ans = 2 + 3 = 5, but in fact the correct answer is 4, from the above analysis, we can see that S = {1, 2}, t = {3} is repeated. That is, this method is incorrect.
Therefore, an array dp2 [I] [J] is required to indicate the next I element and the number of classes equal to J and must contain a [I]. That is, the final ans = DP [I] [J] * dp2 [n-I] [J] (0 <I <n, 0 <= j <= 1024)
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#define ll long longusing namespace std;const ll mod=1000000007;const int maxn=1050;const int N=1024;ll dp[maxn][maxn],Dp[maxn][maxn],Dp2[maxn][maxn];int a[maxn],pre[maxn],n,m;void input(){    scanf("%d",&n);    for(int i=1; i<=n; i++)  scanf("%d",&a[i]);    memset(dp,0,sizeof(dp));    memset(Dp,0,sizeof(Dp));    memset(Dp2,0,sizeof(Dp2));}void solve(){    for(int i=1; i<=n; i++)    {        dp[i][a[i]]=1;        for(int j=N; j>=0; j--)        {            dp[i][j]=(dp[i][j]+dp[i-1][j])%mod;            dp[i][j^a[i]]=(dp[i][j^a[i]]+dp[i-1][j])%mod;        }    }    //cout<<dp[2][3]<<endl;    reverse(a+1,a+n+1);    for(int i=1; i<=n; i++)    {        Dp[i][a[i]]=1;        Dp2[i][a[i]]=1;        for(int j=N; j>=0; j--)        {            Dp[i][j]=(Dp[i][j]+Dp[i-1][j])%mod;            Dp[i][j&a[i]]=(Dp[i][j&a[i]]+Dp[i-1][j])%mod;            Dp2[i][j&a[i]]=(Dp2[i][j&a[i]]+Dp[i-1][j])%mod;        }    }    ll ans=0;    for(int i=1; i<n; i++)    {        int k=n-i;        for(int j=0; j<=N; j++)        {            if(dp[i][j] && Dp[k][j])            {                ans=(ans+dp[i][j]*Dp2[k][j]%mod)%mod;            }        }    }    printf("%I64d\n",ans%mod);}int main(){    int T;    scanf("%d",&T);    while(T--)    {        input();        solve();    }    return 0;}

HDU 4901 the romantic hero

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.