题目链接:

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. 第一部分 CLR基础:第1章 CLR的执行模型

    1.1将源代码编译成托管模块

  2. HTTP首部及各状态码

    通用首部:客户端和服务器都可以使用的通用首部,比如Status Code: 请求首部:请求首部是请求报文特有的,它们为服务器提供了一些额外信息,例如Accept: */* 用来告知服务器客户端会接受与 ...

  3. JS预览图像

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  4. Delphi For Android 开发笔记-附:如何Delphi中同时实现Windows、Android版的GetModuleFileName函数

    在Windows中开发DLL时,经常会需要获取当前DLL所在目录以便读取同目录下的其他文件,而目前Delphi在开发android时,其实没多大必要获取,因为整个工程只有一个so文件,而这个so文件也 ...

  5. C# 平时碰见的问题【5】

    vs按F5启动调试,项目不会编译的解决办法 工具 -> 选项 -> 项目和解决方案 -> 运行时, 当项目过期(下拉框) -> 不要选[从不生成] 附英文版的:

  6. Autofac的注入和web.config配合

    public static void BuildMvcContainer() { var builder = new ContainerBuilder(); var assemblys = AppDo ...

  7. django-url调度器-初级篇

    Django 遵从 MVC 模型,并将其特色化为 MTV 模型.模型的核心是通过用户访问的 url 来指向处理的函数,而函数处理后返回相应的结果.所以url决定了用户访问的入口,另外表单处理的提交地址 ...

  8. Python脚本控制的WebDriver 常用操作 <三> 浏览器最大化

    下面将模拟执行一个控制浏览器最大化的操作 测试用例场景 当我们在测试中使用一些基于图像和坐标的辅助测试工具时,我们就会需要使浏览器在每次测试时保存最大化,以便在同一分辨率下进行图像比对和坐标点选. 举 ...

  9. TFS使用指南

    上一篇文章已经简略介绍过TFS的安装与管理,本篇文章主要描述一下我个人在工作过程中使用TFS的一些指南与建议.本章内容预览: 1.  项目计划与跟踪 经常有很多朋友在日常聊天中抱怨做计划很无畏,因为计 ...

  10. SQL Server Analysis Services 数据挖掘

    假如你有一个购物类的网站,那么你如何给你的客户来推荐产品呢?这个功能在很多 电商类网站都有,那么,通过SQL Server Analysis Services的数据挖掘功能,你也可以轻松的来构建类似的 ...