题目链接:

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. C# Datetime类常用技巧

    C#类常用技巧 //今天DateTime.Now.Date.ToShortDateString();//昨天,也就是今天的日期减一DateTime.Now.AddDays(-1).ToShortDat ...

  2. 【转】src与href属性的区别

    ref:http://www.jb51.net/web/77258.html src和href之间存在区别,能混淆使用.src用于替换当前元素,href用于在当前文档和引用资源之间确立联系. src是 ...

  3. 怎么利用CSS3绘制三角形

    最近三角形挺火,很多地方都能碰到,如网页,微信,或者QQ空间的时间轴等地方都能看到,而且这些并不是图片插入进去的,那就需要用CSS来做了 <p class="bbb"> ...

  4. ListView的几种形式

    一. ArrayAdapter ListView listView = (ListView) findViewById(R.id.list_view);//ListView的参数为id listVie ...

  5. 自适应游标共享技术01(Adaptive Cursor Sharing)

    什么是ACS(adaptiver cursor sharing) Oracle通过绑定变量技术解决了SQL语句硬解析过多的问题,降低了资源的争用.但是绑定变量在引入cursor sharing,增加了 ...

  6. Keil的使用方法 - 常用功能(一)

    Ⅰ.概述 学习一门软件的开发,开发工具的掌握可以说尤为重要.由于Keil集成开发工具支持多种MCU平台的开发,是市面上比较常见的,也是功能比较强大一款IDE.所以,对于大多数人说,选择Keil几乎是单 ...

  7. VS2013+Qt5.6+VSaddin1.2.5

    1 下载Qt(1)Qt安装包http://download.qt.io/official_releases/qt/(2)Qt插件http://ftp.jaist.ac.jp/pub/qtproject ...

  8. 使用MSYS2编译64位gvim

    1. 下载安装MSYS2 在https://msys2.github.io/下载MSYS2,推荐下载x86-64版,此版本内置了MinGW32与MinGW64 安装后首先更新MSYS2系统,顺序执行下 ...

  9. React Native分析(index.ios.js)

    定义创建组件MyComponent(index.ios.js): 'use strict' var React = require('react-native'); var { AppRegistry ...

  10. AMD 和 CMD as lazy as possible

    http://blog.chinaunix.net/uid-26672038-id-4112229.html AMD 与 CMD 区别到底在哪里?       看了以上 AMD,requireJS 与 ...