码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5800 To My Girlfriend

时间:2016-08-07 23:01:45      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

背包变形。dp[i][j][g][h]表示前i个数字,和为j,有g个必选,有h个必不选的方案数。

答案为sum{dp[n][j][2][2]}*4

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-8;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
    char c = getchar();  while(!isdigit(c)) c = getchar();
    int x = 0;
    while(isdigit(c)) { x = x * 10 + c - 0; c = getchar(); }
    return x;
}

const int maxn=1000+10;
int dp[maxn][maxn][3][3],f[maxn][maxn][3][3];
int mod=1e9+7;
int T,n,s,a[maxn];

int main()
{
    scanf("%d",&T); while(T--)
    {
        scanf("%d%d",&n,&s);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        memset(dp,0,sizeof dp); dp[0][0][0][0]=1;

        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=s;j++)
            {
                for(int p=0;p<=2;p++)
                {
                    for(int h=0;h<=2;h++)
                    {
                        int num=0;
                        num=(num+dp[i-1][j][p][h])%mod;
                        if(j-a[i]>=0) num=(num+dp[i-1][j-a[i]][p][h])%mod;
                        if(p-1>=0&&j>=a[i]) num=(num+dp[i-1][j-a[i]][p-1][h])%mod;
                        if(h-1>=0) num=(num+dp[i-1][j][p][h-1])%mod;
                        dp[i][j][p][h]=num;
                    }
                }
            }
        }
        LL ans=0;
        for(int i=1;i<=s;i++) ans=(ans+dp[n][i][2][2])%mod;
        printf("%lld\n",(ans*4)%(LL)mod);
    }
    return 0;
}

 

HDU 5800 To My Girlfriend

标签:

原文地址:http://www.cnblogs.com/zufezzt/p/5747332.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!