To My Girlfriend

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

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

Sincerely yours,
Liao

 
Input
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).
 
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
/**
题目:To My Girlfriend
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5800
题意:如原题公式所示。
思路: 来源出题方给的题解。 令dp[i][j][s1][s2]表示前i个物品填了j的体积,有s1个物品选为必选,s2个物品选为必不选的方案数
(0<=s1,s2<=2),则有转移方程
dp[i][j][s1][s2] = dp[i - 1][j][s1][s2] + dp[i-1][j-a[i]][s1][s2] + dp[i - 1][j - a[i]][s1 - 1][s2] + dp[i - 1][j][s1][s2 - 1],
边界条件为dp[0][0][0][0] = 1,时间复杂度O(NS*3^2)。 dp[i - 1][j][s1][s2]: 不选第i个
dp[i-1][j-a[i]][s1][s2]: 选第i个
dp[i - 1][j - a[i]][s1 - 1][s2]: 第i个必选
dp[i - 1][j][s1][s2 - 1]: 第i个必不选 最终结果为ans += dp[n][x][2][2]*4;(1<=x<=s)
因为:
dp[n][x][2][2]算出来的都是没有排列时候选的i,j,k,l;
经过排列即:(i,j),(j,i),(k,l),(l,k)共四种。所有*4;
*/ #include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int mod=1e9+;
const int maxn=1e6+;
const double eps = 1e-;
int dp[][][][];
int a[];
int n, s;
int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d%d",&n,&s);
for(int i = ; i <= n; i++) scanf("%d",&a[i]);
memset(dp, , sizeof dp);
dp[][][][] = ;
for(int i = ; i <= n; i++){
for(int j = ; j <= s; j++){
for(int s1 = ; s1 <= ; s1++){
for(int s2 = ; s2 <= ; s2++){
dp[i][j][s1][s2] = dp[i-][j][s1][s2];
if(s1!=&&j>=a[i]){
dp[i][j][s1][s2] += dp[i-][j-a[i]][s1-][s2];
dp[i][j][s1][s2] %= mod;
}
if(j>=a[i]){
dp[i][j][s1][s2] += dp[i-][j-a[i]][s1][s2];
dp[i][j][s1][s2] %= mod;
}
if(s2!=){
dp[i][j][s1][s2] += dp[i-][j][s1][s2-];
dp[i][j][s1][s2] %= mod;
}
}
}
}
}
LL ans = ;
for(int i = ; i <= s; i++) {
ans = (ans+dp[n][i][][])%mod;
}
printf("%lld\n",ans*%mod);
}
return ;
}

hdu5800 To My Girlfriend dp 需要比较扎实的dp基础。的更多相关文章

  1. HDU5800 To My Girlfriend 背包计数dp

    分析:首先定义状态dp[i][j][s1][s2]代表前i个物品中,选若干个物品,总价值为j 其中s1个物品时必选,s2物品必不选的方案数 那么转移的时候可以考虑,第i个物品是可选可可不选的 dp[i ...

  2. 万能的林萧说:一篇文章教会你,如何做到招聘要求中的“要有扎实的Java基础”。

    来历 本文来自于一次和群里猿友的交流,具体的情况且听LZ慢慢道来. 一日,LZ在群里发话,"招人啦." 然某群友曰,"群主,俺想去." LZ回之,"你 ...

  3. 一篇文章教会你,如何做到招聘要求中的“要有扎实的Java基础

    来历 本文来自于一次和群里猿友的交流,具体的情况且听LZ慢慢道来. 一日,LZ在群里发话,“招人啦.” 然某群友曰,“群主,俺想去.” LZ回之,“你年几何?” 群友曰,“两年也.” LZ憾言之,“惜 ...

  4. dp乱写2:论dp在不在dp中(但在dp范畴)内的应用

    最近正儿八经的学习了dp,有一些题目非常明显看出来就是dp了比如说:过河卒.方格取数.导弹拦截.加分二叉树.炮兵阵地更加明显的还有:采药.装箱问题.过河.金明的预算方案.今天来谈谈dp的dp在不在dp ...

  5. [提升性选讲] 树形DP进阶:一类非线性的树形DP问题(例题 BZOJ4403 BZOJ3167)

    转载请注明原文地址:http://www.cnblogs.com/LadyLex/p/7337179.html 树形DP是一种在树上进行的DP相对比较难的DP题型.由于状态的定义多种多样,因此解法也五 ...

  6. 程序人生:01如何做到招聘要求中的“要有扎实的Java基础”

    本文摘自左潇龙博客,原文出处:http://www.zuoxiaolong.com/html/article_232.html 来历 本文来自于一次和群里猿友的交流,具体的情况且听LZ慢慢道来. 一日 ...

  7. 【转】斜率优化DP和四边形不等式优化DP整理

    (自己的理解:首先考虑单调队列,不行时考虑斜率,再不行就考虑不等式什么的东西) 当dp的状态转移方程dp[i]的状态i需要从前面(0~i-1)个状态找出最优子决策做转移时 我们常常需要双重循环 (一重 ...

  8. POJ2411Mondriaan's Dream(DP+状态压缩 or 插头DP)

    问题: Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after prod ...

  9. <DP> (高频)139 375 374 (DP hard)312

    139. Word Break 返回结果较为简单可用dp, 复杂用dfs class Solution { public boolean wordBreak(String s, List<Str ...

随机推荐

  1. iOS中设置backBarButtonItem的title和action

    一. 设置title 在需要显示该返回键的前一个Controller中设置: 1: navigationItem.backBarButtonItem = UIBarButtonItem(title: ...

  2. 自定义PHP页面跳转函数redirect($url, $time = 0, $msg = '')

    利用PHP的header()函数,可以实现页面跳转,如 header("Location: " . $url); 但它有个缺点,一旦HTTP报头块已经发送,就不能使用 header ...

  3. CASJAVA一些理解

    如果不用锁机制如何实现共享数据访问.(不要用锁,不要 用sychronized  块或者方法,也不要直接使用 jdk  提供的线程安全的数据结构,需要自己实现一个类来保证多个线程同时读写这个类中的共享 ...

  4. TDiocpCoderTcpServer返回数据记录有条数限制的问题

    TDiocpCoderTcpServer返回数据记录有条数限制的问题 在使用TDiocpCoderTcpServer控件返回查询数据的时候,发现当记录条数超过一定数量的时候(比方有人反试图返回30万条 ...

  5. 怎样使用libcurl获取隐藏了文件后缀的url网络文件类型

    CURLINFO_CONTENT_TYPE CURL: Get Returned Content Mime Type 例如 :以下代码可以查询出天地图的tile图像类型为jpg "http: ...

  6. Scala快学笔记(一)

    一,基本概念: 1,Scala是一种基于JVM的面向对象和函数式编程语言 2,基本类型:数值类型 ->:Byte,Short,Int,Long,Float,Double和布尔类型:Boolean ...

  7. Oracle数据类型,函数与存储过程

    字符串类型    固定长度:char nchar    n 表示Unicode编码    可变长度: varchar2 nvarchar2 数字类型:number(P,S)P:整数位数,S小数位数   ...

  8. lodash map

    _.map(collection, [iteratee=_.identity]) 创建一个经过 iteratee 处理的集合中每一个元素的结果数组. iteratee 会传入3个参数:(value, ...

  9. 在Ubuntu 12.04上配置iSCSI Target服务

      今天自己按照网上搜来的教程自己在Ubuntu 12.04上配置了iSCSI Target服务,在这里简单地做个纪录.操作系统是全新安装的Ubuntu 12.04,配置一块500 GB的SATA笔记 ...

  10. Tabs or Spaces?

    Never mix tabs and spaces. The most popular way of indenting Python is with spaces only. The second- ...