HDU 5800 To My Girlfriend 背包
题目链接:
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 4sample 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 背包的更多相关文章
- hdu 5800 To My Girlfriend(背包变形)
To My Girlfriend Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- hdu 5800 To My Girlfriend + dp
传送门:hdu 5800 To My Girlfriend 题意:给定n个物品,其中i,j必选,l,m必不选,问组成体积为s的方法一共有多少种 思路:定义dp[i][j][s1][s2],表示前i种物 ...
- 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 ...
- 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 ...
- HDU 5800 (DP)
Problem To My Girlfriend (HDU 5800) 题目大意 给定一个由n个元素组成的序列,和s (n<=1000,s<=1000) 求 : f (i,j,k,l, ...
- HDU 1248 寒冰王座(全然背包:入门题)
HDU 1248 寒冰王座(全然背包:入门题) http://acm.hdu.edu.cn/showproblem.php?pid=1248 题意: 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票 ...
- HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)
HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...
- HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)
HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...
- HDOJ(HDU).1114 Piggy-Bank (DP 完全背包)
HDOJ(HDU).1114 Piggy-Bank (DP 完全背包) 题意分析 裸的完全背包 代码总览 #include <iostream> #include <cstdio&g ...
随机推荐
- CentOS 6.4安装AMH面板
复制以下代码 然后执行 或者下载wget http://amysql.com/file/AMH/3.2/amh.sh; chmod 775 amh.sh; ./amh.sh 2>&1 | ...
- DataGridView取消默认选中行
DataGridView在添加数据后会默认选中第 一个单元格或者第一行,我就想取消它的默认选中行.在DataGridView绑定数据之后加上了ClearSelection().这样一来,不论是启动窗体 ...
- 日志处理--Logo4Net与文件的并发处理
本文参考自:http://www.cnblogs.com/jiekzou/ 多线程操作同一个文件时会出现并发问题.解决的一个办法就是给文件加锁(lock),但是这样的话,一个线程操作文件时,其它的都得 ...
- Eruda——手机网页前端调试面板
前言 进行移动端网页开发时,想要查看手机浏览器信息从来都不是一件容易的事.特别是当目标环境为APP内置WebView,需要调用特定的JsBridge接口时,你根本都干不了什么,只能一遍又一遍地修改代码 ...
- cassandra 之 jdbc 使用【java、scala】
1.数据库创建 参考接上文cassandra入门 http://www.cnblogs.com/piaolingzxh/p/4197833.html 2.下载jdbc驱动源码,构建jar包 源码下载地 ...
- wordpress学习-plugins-001
plugins-插件 Akismet(Automattic Kismet)是应用广泛的一个垃圾留言过滤系统,其作者是大名鼎鼎的WordPress创始人Matt Mullenweg,Akismet也是W ...
- LayoutInflater中四种类型inflate方法的介绍
转自:http://blog.csdn.net/aa4790139/archive/2011/05/07/6401556.aspx 第一种: public View inflate (int reso ...
- Python初学者笔记(4)-简单的通讯录
要求: 编写一个简单的通讯录 1.通讯录包含至少包含姓名.电话号码.电子邮箱:2.通讯录的信息能够保存在本地磁盘:3.通讯录查找特定人员的信息:4.通讯录能够修改特定人员的信息:5.通讯录能够删除特定 ...
- HelloWorld IL代码
.assembly extern mscorlib .assembly HelloWorld.class HelloWorld extends [mscorlib] System.Object { ...
- hdu 1867 A + B for you again
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1867 A + B for you again Description Generally speaki ...