hdu5119 dp

题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5119

题意: 输入T组数据,每组数据包括两个数n和m,接下来n个数,这n个数可以随意取(可以取0个),问在这些数的异或和大于等于m的情况下有几种情况。

看了下时间限制是6s,内存的限制也很大,下面大概是一种最好理解代码。

状态转移方程:dp[i][j]=dp[i–1][j];

                  dp[i][j^a[i]]=dp[i–1][j];

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define ll long long

const int maxn=1e6+5;

ll a[45];
ll dp[45][maxn];

int main()
{
    int T,N,M;
    cin>>T;
    for(int t=1; t<=T; t++)
    {
        cin>>N>>M;
        for(int i=1; i<=N; i++)
            cin>>a[i];
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=1; i<=N; i++)
        {
            for(int j=0; j<=1e6; j++)
            {
                dp[i][j]+=dp[i-1][j];
                dp[i][j^a[i]]+=dp[i-1][j];
            }
        }
        ll ans=0;
        for(int i=M; i<maxn; i++)
            ans+=dp[N][i];
        cout<<"Case #"<<t<<':'<<' '<<ans<<endl;
    }
    return 0;
}

 

posted @ 2016-10-20 21:15  a_clown_cz  阅读(620)  评论(0)    收藏  举报