题目

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. centos6.5 mysql-server 5.1.73启动失败

    yum install mysql-server 安装mysql服务端会把相应的客户端也装上 service mysqld start  启动mysql服务 解决办法: 1.chomod 777  / ...

  2. 使用php+swoole对client数据实时更新(下)

    上一篇提到了swoole的基本使用,现在通过几行基本的语句来实现比较复杂的逻辑操作: 先说一下业务场景.我们目前的大多数应用都是以服务端+接口+客户端的方式去协调工作的,这样的好处在于不论是处在何种终 ...

  3. Dotnet文件格式解析

    0x0.序 解析过程并没有介绍对pe结构的相关解析过程,网上此类相关资料很多可自行查阅,本文只介绍了网上资料较少的从pe结构的可选头中的数据目录表中获取dotnet目录的rva和size,到完全解析d ...

  4. Json.net日期格式化

    1. 全局设置,可以在App_Global中配置 JsonSerializerSettings setting = new JsonSerializerSettings(); JsonConvert. ...

  5. php检测文件内容编码的方法

    核心用到的是mb_convert_encoding函数,示例代码如下: <?php header("Content-type: text/html; charset=utf-8&quo ...

  6. ffmpeg+x264 Windows MSVC 静态编译

    尝试ubuntu和win下mingw编译版本,但都在Vistual Studio链接时因为依赖 libgcc.a, libmingw.a, libmingwex.a 会与mscrt 有符号冲突. 最后 ...

  7. Ubuntu16.04安装Atom

    转自:http://blog.csdn.net/q1302182594/article/details/51304401 sudo add-apt-repository ppa:webupd8team ...

  8. Linux kilin 安装和按键服务器步骤

    U盘启动,傻瓜式安装 设置root 密码 sudo passwd root vsftpd和ssh服务 使用apt-get可以直接安装vsftpd和ssh,首先要sudo apt-get update ...

  9. 创建Odoo8数据库时的“new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII)“问题

    Odoo8创建数据库时,显示如下错误信息: DataError: new encoding (UTF8) is incompatible with the encoding of the templa ...

  10. js获取系统时间时自动补齐日期带零

    最近在开发中发现有日期不规范的问题,正常规则应该是yy-mm-dd,而在输出时候却变成yy-mm-d,这是js的date()方法在作怪 解决思路是若在10号前,则自动给它补齐一个0,下面给出解决方法, ...