HDU 5800 To My Girlfriend (动态规划)

题目链接:HDU 5800


题面:

To My Girlfriend

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 737    Accepted Submission(s): 292



Problem Description
Dear Guo

I never forget the moment I met with you.You carefully asked me: "I have a very difficult problem. Can you teach me?".I replied with a smile, "of course"."I have n items, their weight was a[i]",you said,"Let's define f(i,j,k,l,m) to be the number of the subset of the weight of n items was m in total and has No.i and No.j items without No.k and No.l items.""And then," I asked.You said:"I want to know
i=1nj=1nk=1nl=1nm=1sf(i,j,k,l,m)(i,j,k,laredifferent)


Sincerely yours,
Liao
 

Input
The first line of input contains an integer T (T15) indicating the number of test cases.
Each case contains 2 integers n , s (4n1000,1s1000) . The next line contains n numbers: a1,a2,,an (1ai1000) .
 

Output
Each case print the only number — the number of her would modulo 109+7 (both Liao and Guo like the number).

 

Sample Input
  
  
2 4 4 1 2 3 4 4 4 1 2 3 4
 

Sample Output
  
  
8 8
 

Author
UESTC
 

Source


题意:

     吐槽下题意,很难懂。求的是,给定n个数,其中选定若干数,这若干数的权值和为m,且这些数中没有下标为i,j的数,有下标为k,l的数的集合个数。


解题:

   dp[i][j][s1][s2],代表的是前i个物品,总权值为j,已有s1个必选,s2必不选的方案数。那么对于当前一个状态,它有四种转移状态。

   1.选中当前的,增加权值,增加必选个数。

   2.选择当前的,增加权值,不增加必选个数。

   3.不选中当前的,不增加权值,增加不必选个数。

   4.不选中当前的,不增加权值,不增加必选个数。

   因为i,j可以互换,l,k也可以互换,故而最后方案数乘以4即为所求。


代码:

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define mod 1000000007
#define LL long long
#define p(a) printf("%d\n",a)
using namespace std;
unsigned dp[1005][1005][3][3];
int a[1005];
int main()
{ 
    int t,n,s,tmp;
	LL ans;
	scanf("%d",&t);
	while(t--)
	{
        ans=0;
		scanf("%d%d",&n,&s);
		for(int i=1;i<=n;i++)
		  scanf("%d",&a[i]);
		memset(dp,0,sizeof(dp));
		dp[1][a[1]][0][0]=1;
		dp[1][a[1]][1][0]=1;
		dp[1][0][0][0]=1;
		dp[1][0][0][1]=1;
		for(int i=2;i<=n;i++)
		{
			for(int j=0;j<=s;j++)
			{
               tmp=j+a[i];
			   if(tmp<=s)
			   {
				   for(int k=0;k<=2;k++)
				   {
					   dp[i][tmp][0][k]=(dp[i][tmp][0][k]+dp[i-1][j][0][k])%mod;
					   dp[i][tmp][1][k]=(dp[i][tmp][1][k]+dp[i-1][j][0][k])%mod;
					   dp[i][tmp][1][k]=(dp[i][tmp][1][k]+dp[i-1][j][1][k])%mod;
					   dp[i][tmp][2][k]=(dp[i][tmp][2][k]+dp[i-1][j][1][k])%mod;
					   dp[i][tmp][2][k]=(dp[i][tmp][2][k]+dp[i-1][j][2][k])%mod;

				   }
			   }
			   for(int k=0;k<=2;k++)
			   {
				   dp[i][j][k][0]=(dp[i][j][k][0]+dp[i-1][j][k][0])%mod;
				   dp[i][j][k][1]=(dp[i][j][k][1]+dp[i-1][j][k][0])%mod;
				   dp[i][j][k][1]=(dp[i][j][k][1]+dp[i-1][j][k][1])%mod;
				   dp[i][j][k][2]=(dp[i][j][k][2]+dp[i-1][j][k][1])%mod;
				   dp[i][j][k][2]=(dp[i][j][k][2]+dp[i-1][j][k][2])%mod;
			   }
			}
		}
		for(int i=1;i<=s;i++)
			ans=(ans+dp[n][i][2][2])%mod;
		printf("%lld\n",ans*4%mod);
	}
	return 0;
}  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值