HDU 4901 DP backpack

Source: Internet
Author: User

Give you n numbers and ask you to divide the number into two arrays. the requirements of all elements in S, T, and t are larger than those in S, ask how many types of conditions are there when all elements in s perform the XOR operation and all the elements in T perform the & operation value equal.

DP backpack ideas

DPA [I] [J] [0] indicates the number of solutions starting from left to I, where I is not obtained and the status is J.

DPA [I] [J] [1] indicates the number of solutions from job start to I, from I to status J.

DPB [I] [J] indicates the number of solutions in the status of J from right to I.

Because the s set must be on the left of the t set, the split lines of the set can be enumerated, and the enumeration scheme must be unique, just make sure that the points on the split line are forcibly obtained. In the final calculation, DPA [I] [J] [1] is used to force the left side to obtain the split line.


#include "stdio.h"#include "string.h"__int64 mod=1000000007;__int64 dpa[1010][1025][2],dpb[1010][1025],a[1010];int main(){    int Case,n,i,j;    __int64 ans;    scanf("%d",&Case);    while (Case--)    {        scanf("%d",&n);        for (i=1;i<=n;i++)            scanf("%I64d",&a[i]);        memset(dpa,0,sizeof(dpa));        memset(dpb,0,sizeof(dpb));        dpa[1][a[1]][1]=1;        for (i=2;i<=n;i++)        {            dpa[i][a[i]][1]++;            for (j=0;j<1024;j++)            {                dpa[i][j][0]+=(dpa[i-1][j][0]+dpa[i-1][j][1])%mod;                if (dpa[i][j][0]>mod) dpa[i][j][0]-=mod;                dpa[i][j^a[i]][1]+=(dpa[i-1][j][1]+dpa[i-1][j][0])%mod;                if (dpa[i][j^a[i]][1]>mod) dpa[i][j^a[i]][1]-=mod;            }        }        dpb[n][a[n]]=1;        for (i=n-1;i>=1;i--)        {            dpb[i][a[i]]++;            for (j=0;j<1024;j++)            {                dpb[i][j&a[i]]+=dpb[i+1][j];                if (dpb[i][j&a[i]]>mod) dpb[i][j&a[i]]-=mod;                dpb[i][j]+=dpb[i+1][j];                if (dpb[i][j]>mod) dpb[i][j]-=mod;            }        }        ans=0;        for (i=1;i<n;i++)            for (j=0;j<=1024;j++)            {                ans+=(dpa[i][j][1]*dpb[i+1][j])%mod;                if (ans>mod)ans-=mod;            }        printf("%I64d\n",ans);    }    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.