HDU5800 To My Girlfriend(DP)
题目
Source
http://acm.hdu.edu.cn/showproblem.php?pid=5800
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
$$\sum_{i=1}^{n}\sum_{j=1}^{n}\sum_{k=1}^{n}\sum_{l=1}^{n}\sum_{m=1}^{s}f(i,j,k,l,m)\quad (i,j,k,l\quad are\quad different)$$
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
分析
题目大概就是说给n个数和s,然后求上面那个式子的结果,f(i,j,k,l,m)表示下标i和j的数必选、k和l不选且选出数的和为s的选法总数。
- dp[x][y][i][j]表示前i个数中选出总和为j且有x个数确定必选、y个数确定不选的方案数
- 转移就是选和不选从i到i+1转移,选可以向x或x+1转移,不选可以向y或y+1转移
- 最后的结果就是A(2,2)*A(2,2)*Σdp[2][2][n][0...s]
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int d[3][3][1111][1111];
int main(){
int t,n,s,a;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&s);
memset(d,0,sizeof(d));
d[0][0][0][0]=1;
for(int i=0; i<n; ++i){
scanf("%d",&a);
for(int j=0; j<=s; ++j){
for(int x=0; x<3; ++x){
for(int y=0; y<3; ++y){
if(d[x][y][i][j]==0) continue;
d[x][y][i+1][j]+=d[x][y][i][j];
d[x][y][i+1][j]%=1000000007;
if(y<2){
d[x][y+1][i+1][j]+=d[x][y][i][j];
d[x][y+1][i+1][j]%=1000000007;
}
if(j+a>s) continue;
d[x][y][i+1][j+a]+=d[x][y][i][j];
d[x][y][i+1][j+a]%=1000000007;
if(x<2){
d[x+1][y][i+1][j+a]+=d[x][y][i][j];
d[x+1][y][i+1][j+a]%=1000000007;
}
}
}
}
}
long long ans=0;
for(int i=0; i<=s; ++i){
ans+=d[2][2][n][i];
ans%=1000000007;
}
ans<<=2;
ans%=1000000007;
printf("%I64d\n",ans);
}
return 0;
}
HDU5800 To My Girlfriend(DP)的更多相关文章
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 初探动态规划(DP)
学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...
- Tour(dp)
Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...
- 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)
.navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...
- Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
- 最长公共子序列长度(dp)
/// 求两个字符串的最大公共子序列长度,最长公共子序列则并不要求连续,但要求前后顺序(dp) #include <bits/stdc++.h> using namespace std; ...
随机推荐
- nubia Z5 mini 小牛 黑砖qhsusb dload修复
给手机分区,用了分区助手,将一些分区移动. 结果分区坏了,只有两三个分区在电脑显示,当时晕菜把数据线拔了重插. 手机变砖,不能启动,黑屏,不能进recovery... 参考 http://bbs.nu ...
- JavaScript中的 offset, client,scroll
在js 中我们要用到的 offset, client, scroll 在这我把自己理解的给大家分享一下. offset div.offsetTop 指div距离上方或上层控件的距离,单位像素 div. ...
- saltstack命令执行过程
saltstack命令执行过程 具体步骤如下 Salt stack的Master与Minion之间通过ZeroMq进行消息传递,使用了ZeroMq的发布-订阅模式,连接方式包括tcp,ipc salt ...
- mysql 基础
(1)插入多条数据 INSERT INTO users(name, age) VALUES('姚明', 25), ('比尔.盖茨', 50), ('火星人', 600); (2)将查询出来的字段插入其 ...
- Python 30分钟入门——数据类型 and 控制结构
Python是一门脚本语言,我也久闻大名,但正真系统的接触学习是在去年(2013)年底到今年(2014)年初的时候.不得不说的是Python的官方文档相当齐全,如果你是在Windows上学习Pytho ...
- 一篇很好的Java、C、PHP、前端、Android、IOS的文章
http://www.cctime.com/html/2016-11-8/1238265.htm 很好的讲了这些技术的学习路线,其中的文档资料很丰富,值得学习参考
- qt5.5程序打包发布以及依赖【转】
玩qt5也有一段时间了,惭愧的是一直没有好好的发布过程序,因为写的都是小程序没啥需要用到发布,而且qt也说不上很熟悉,本来打算到基本掌握qt之后再来研究研究怎么打包程序,最近晚上的空闲时间多了,闲着也 ...
- 学号20145332 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验
实验目的 掌握在 ARM 开发板实现一个简单 WEB 服务器的过程. 学习在 ARM 开发板上的 SOCKET 网络编程. 学习 Linux 下的 signal()函数的使用. 实验内容 学习使用 s ...
- October 25th Week 44th Tuesday 2016
The best preparation for tomorrow is doing your best today. 过好今天,就是对明天最好的准备. Tomorrow is always base ...
- 解决fedora25安装vmware12问题:
运行vmware需要几个工具.gcc 编译工具是必须要有的.dnf groupinstall “Development tools“rpm -qa |grep kernel-headersrpm -q ...