hdu 5119 循环数组+简单dp

dp[i][j],在第i位上数值为j的方法数,没什么好说的,就是数据量大要用循环数组存

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 2000000

using namespace std;

typedef long long LL;

LL dp[2][MAX+7];

int a[MAX+7]; 
int n,m;

int main ( )
{
    int t;
    scanf ( "%d" , &t );
    int c = 1;
    while ( t-- )
    {
        scanf ( "%d%d" , &n , &m);
        for ( int i = 1 ; i <= n ; i++ )
            scanf ( "%d" , &a[i] );
        memset ( dp[1] , 0 , sizeof ( dp[1] ) );
        dp[1][a[1]] = 1 , dp[1][0] = 1;
        for ( int i = 2 ; i <= n ; i++ )
        {
            memset ( dp[i&1] , 0 , sizeof ( dp[i&1] ) );
            for ( int j = MAX  ; j >= 0 ; j-- )
            {
                if ( dp[(i-1)&1][j] )
                {
                    dp[i&1][j] += dp[(i-1)&1][j];
                    dp[i&1][j^a[i]] += dp[(i-1)&1][j];
                }
            }
        }
        LL ans = 0;
        for ( int i = m ; i <= MAX ; i++ ) ans += dp[n&1][i];
        printf ( "Case #%d: " , c++ );
        printf ( "%I64d\n" , ans );
    }   
}


你可能感兴趣的:(C++,动态规划)