HDU 4901 (hangdian multi-school training #3 1005 questions) the romantic hero (DP)

Source: Internet
Author: User

Address: HDU 4901

I did not expect this question to be made in the end ....

This topic uses two DP statements. The difference or clause is obtained first, and then the difference and calculation are obtained from the back. They are

1: Define the two-dimensional array Huo [1000] [1024] in the case of difference or, the former refers to the nth digit, and the latter refers to the concept of hash, if Huo [x] [Y] = 2, it indicates that the rightmost number is the x bit, And the XOR or value is y twice. You need to define another hash array, to save all the preceding situations. When looking for the number of digits, use the hash array to show all values that are different or different from the current number in the X-bit.

2: When the sum is obtained, the two-dimensional array Yu [1000] [1024] is defined, which is similar to the same or. However, the number of digits here refers to all the situations from this back, the XOR operator must contain this bit. Since the operation itself is the result of all the conditions, no need to define a hash array.

3: match from the beginning to the end. You only need to use Huo [x] to match Yu [x + 1. If the two have the same exclusive or value, the number of times is multiplied. The final answer is the correct answer.

Note: During the operation, the number in the number group may be long .. Therefore, the remainder of each step is required .. (Because of this, it is wrong several times ..)

The Code is as follows;

#include <iostream>#include <cstdio>#include <cstring>using namespace std;__int64 huo[1003][1125], yu[1003][1125], a[1013], _hash[1003][1030];const int mod=1e9+7;int main(){    __int64 t, n, i, j, k;    __int64 s;    scanf("%I64d",&t);    while(t--)    {        s=0;        memset(huo,0,sizeof(huo));        memset(yu,0,sizeof(yu));        scanf("%I64d",&n);        for(i=1; i<=n; i++)        {            scanf("%I64d",&a[i]);        }        memset(_hash,0,sizeof(_hash));        for(i=1; i<=n; i++)        {            for(j=0; j<=1024; j++)            {                if(_hash[i-1][j])                {                    _hash[i][a[i]^j]+=_hash[i-1][j];                    _hash[i][a[i]^j]%=mod;                    huo[i][a[i]^j]+=_hash[i-1][j];                    huo[i][a[i]^j]%=mod;                    _hash[i][j]+=_hash[i-1][j];                    _hash[i][j]%=mod;                }            }            _hash[i][a[i]]++;            huo[i][a[i]]++;        }        for(i=n; i>=1; i--)        {            for(j=0; j<=1024; j++)            {                if(yu[i+1][j])                {                    yu[i][a[i]&j]+=yu[i+1][j];                    yu[i][a[i]&j]%=mod;                    yu[i][j]+=yu[i+1][j];                    yu[i][j]%=mod;                }            }            yu[i][a[i]]++;        }        for(i=1; i<=n; i++)        {            for(j=0; j<=1024; j++)            {                if(huo[i][j]&&yu[i+1][j])                {                    s=(s+huo[i][j]*yu[i+1][j])%mod;                }            }        }        printf("%I64d\n",s);    }    return 0;}


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.