题目

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)的更多相关文章

  1. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  2. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  3. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  4. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  5. 初探动态规划(DP)

    学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...

  6. Tour(dp)

    Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...

  7. 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)

    .navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...

  8. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

  9. 最长公共子序列长度(dp)

    /// 求两个字符串的最大公共子序列长度,最长公共子序列则并不要求连续,但要求前后顺序(dp) #include <bits/stdc++.h> using namespace std; ...

随机推荐

  1. Orchard 模块开发学习笔记 (1)

    创建模块 首先,打开Bin目录下的Orchard.exe 等到出现orchard>后, 看看命令列表中是否存在 codegen module 如果不存在,则需要先执行:feature enabl ...

  2. Java基本数据类型总结

    基本类型,或者叫做内置类型,是JAVA中不同于类的特殊类型.它们是我们编程中使用最频繁的类型.java是一种强类型语言,第一次申明变量必须说明数据类型,第一次变量赋值称为变量的初始化. 1. Java ...

  3. Rails sanitize

    The SanitizeHelper module provides a set of methods for scrubbing text of undesired HTML elements. T ...

  4. PYTHON文件操作(二)

    class file(object) def close(self): # real signature unknown; restored from __doc__ 关闭文件 "" ...

  5. Linux less 命令

    - less 打开文件后: shift + g 转至文件末尾 g 转至文件开头 搜索字段(/<pattern>)后: n 转至下一个匹配的字段 shift + n 转至上一个匹配的字段 同 ...

  6. C# I/O

    获取运行时的动态目录 private static string GetDataDir_Data() { var parent = Directory.GetParent(Directory.GetC ...

  7. jemalloc在linux上从安装到使用

    jemalloc在linux上从安装到使用 上次在引导大家安装Redis时提到可能会报错:  发现了redis有用到jemalloc. 首先,jemalloc是干什么的? 我们看看作者自己的介绍: j ...

  8. Entity Framework 简单查询

    前言 首先来简单的复习一下如何使用Code First. 第一步还是先建立一个控制台的应用程序,然后通过Nuget添加Entity Framework.那么同时会给packages.config和Ap ...

  9. Fabric远程自动化使用说明

    背景: 关于Fabric的介绍,可以看官网说明.简单来说主要功能就是一个基于Python的服务器批量管理库/工具,Fabric 使用 ssh(通过 paramiko 库)在多个服务器上批量执行任务.上 ...

  10. 通过挂载系统光盘搭建本地yum仓库的方法

    在CentOS系统中,我们常常会安装大量的软件,但许多软件包都存在需要依赖性,当然我们可以通过一一安装依赖包来完成安装,但对于有些软件包需要大量的依赖包,再一一安装起来会显得特别麻烦.接下来我们就来讲 ...