题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5800

To My Girlfriend

Time Limit: 2000/2000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)

问题描述

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=1n∑j=1n∑k=1n∑l=1n∑m=1sf(i,j,k,l,m)(i,j,k,laredifferent)

Sincerely yours,

Liao

输入

The first line of input contains an integer T(T≤15) indicating the number of test cases.

Each case contains 2 integers n, s (4≤n≤1000,1≤s≤1000). The next line contains n numbers: a1,a2,…,an (1≤ai≤1000).

输出

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

题意

问你所有和为k(0<=k<=s),且两个元素(i,j)在其中,两个元素(l,m)不在其中的所有四元组。

题解

变种背包问题。

dp[i][j][s1]s2表示现在处理到第i个数,和为j且s1个元素必选,s2个元素必不选的的情况。

那么每个元素有四种情况:塞到s1里面、塞到s2里面、塞进来但不放在s1,s2、根本不塞进来。

最后的答案就是4*sigma(dp[n][k][2][2])。乘4是以为之前没有考虑(i,j),(l,m)内部的顺序。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; const int maxn = 1001;
typedef long long LL; const int mod = 1e9 + 7; //开long long 的话就要用滚动数组了。
int dp[maxn][maxn][3][3];
int n, m;
int arr[maxn]; void add_mod(int &x, int y) {
x = (x + y) % mod;
} int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
memset(dp, 0, sizeof(dp));
dp[0][0][0][0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= m; j++) {
for (int s1 = 0; s1 < 3; s1++) {
for (int s2 = 0; s2 < 3; s2++) {
//不塞
add_mod(dp[i][j][s1][s2], dp[i - 1][j][s1][s2]);
//塞
if (j >= arr[i]) add_mod(dp[i][j][s1][s2], dp[i - 1][j - arr[i]][s1][s2]);
//塞进s1
if (j >= arr[i]&&s1-1>=0) add_mod(dp[i][j][s1][s2], dp[i - 1][j - arr[i]][s1 - 1][s2]);
//塞进s2
if (s2 - 1 >= 0) add_mod(dp[i][j][s1][s2], dp[i - 1][j][s1][s2 - 1]);
}
}
}
}
LL ans = 0;
for (int i = 0; i <= m; i++) ans+=dp[n][i][2][2],ans%=mod;
ans = 4 * ans%mod;
printf("%lld\n", ans);
}
return 0;
}

HDU 5800 To My Girlfriend 背包的更多相关文章

  1. hdu 5800 To My Girlfriend(背包变形)

    To My Girlfriend Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. hdu 5800 To My Girlfriend + dp

    传送门:hdu 5800 To My Girlfriend 题意:给定n个物品,其中i,j必选,l,m必不选,问组成体积为s的方法一共有多少种 思路:定义dp[i][j][s1][s2],表示前i种物 ...

  3. HDU 5800 To My Girlfriend(单调DP)

    [题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5800 [题目大意] 给出一个容量上限s,f[i][j][k][l][m]表示k和l两个物品不能选,i ...

  4. HDU 5800 To My Girlfriend

    背包变形.dp[i][j][g][h]表示前i个数字,和为j,有g个必选,有h个必不选的方案数. 答案为sum{dp[n][j][2][2]}*4 #pragma comment(linker, &q ...

  5. HDU 5800 (DP)

    Problem To My Girlfriend (HDU 5800) 题目大意 给定一个由n个元素组成的序列,和s (n<=1000,s<=1000) 求 :   f (i,j,k,l, ...

  6. HDU 1248 寒冰王座(全然背包:入门题)

    HDU 1248 寒冰王座(全然背包:入门题) http://acm.hdu.edu.cn/showproblem.php?pid=1248 题意: 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票 ...

  7. HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)

    HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...

  8. HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)

    HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...

  9. HDOJ(HDU).1114 Piggy-Bank (DP 完全背包)

    HDOJ(HDU).1114 Piggy-Bank (DP 完全背包) 题意分析 裸的完全背包 代码总览 #include <iostream> #include <cstdio&g ...

随机推荐

  1. MySQL语法

    sql(structure query language)结构化查询语言ansi iso/iec组织制定ddl(data definition language) 数据定义语言dml(manipula ...

  2. SQL中如何检查死锁

    SQL中如何检查死锁 编写人:CC阿爸 2014-6-15 在日常SQL数据库的操作中,SQL偶尔会出现表被死锁的问题.比如: 在执行事务时,突然中止事务.系统肯定会锁表. 大批量数据操作时,由于网络 ...

  3. BGP学习笔记

    源自红茶三杯: BGP应用于大规模网络或运营商,用作在AS间传递路由信息 使用BGP的三大理由 1. 大量路由需要承载, IGP只能容纳千条,而BGP可以容纳上万(应该是IGP结合BGP使用?) 2. ...

  4. RecyclerView的基本创建

    线性显示 类似于listview: 线性宫格显示 类似于grid view: 用线性宫格显示 类似于瀑布流: 结构图: 测试代码: activity_main.xml: <RelativeLay ...

  5. Windows7下Microsoft Office Excel 不能访问文件解决方案

    1).开始--〉运行--〉cmd 2)命令提示符下面,输入mmc -32,打开32的控制台 3).文件菜单中,添加删除管理单元--〉组件服务 4).在"DCOM配置"中找到&quo ...

  6. C# A窗口内容显示在B窗口中的方法

    HeScripts script = new HeScripts(); //A窗口中实例化B窗口 string okscripts = "test"; //设置字段内容 scrip ...

  7. delphi中的ClientDataSet组件的open和Execute方法各自在什么情况下用?

    ClientDataSet组件本来是给midas用的,也是所谓的borland的三层数据技术,使用这个控件必须发行midas.dll挺麻烦的 open是通过应用的SQL语句为SELECTexecute ...

  8. SPARK 数据统计程序性能优化。

    昨天写完R脚本 没测试就发到博客里, 结果实际运行发现很慢,运行时间在2小时以上, 查看spark控制台, 大量时间消耗在count上, 产生的stage多大70多个 . 分析原因. 1  selec ...

  9. Git - Tutorial [Lars Vogel]

    From: http://www.vogella.com/tutorials/Git/article.html Git - Tutorial Lars Vogel Version 5.6 Copyri ...

  10. uva 11186 Circum Triangle<叉积>

    链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...