To My Girlfriend

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

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
Recommend
wange2014   |   We have carefully selected several similar problems for you:  6032 6031 6030 6029 6028 
 
题解:

就是给你 n个数 其中(序号i,j)是必选的,(序号k,l)是必不选的.使得其中的子集在n个数中的总权值为m. 
例如样例 
f(1,2,3,4)=1 f(2,1,3,4)=1 f(1,3,2,4)=1 f(3,1,2,4)=1 
f(1,2,4,3)=1 f(2,1,4,3)=1 f(1,3,4,2)=1 f(3,1,4,2)=1 所以答案为8.

个人感想: 
看了一下题目,我基本没什么思路,然后喵了一下题解,说是dp,然后我想了一个晚上..我觉得自己真是弱鸡,还是没怎么想到,看了网上题解,可以推出朴素的O(n^3)算法,我尼玛我一点感觉也没,我都不知道怎么推出朴素了..我反而悟出了这道题的前2维的计数,但是我就想不到怎么把必选不选的排掉.很烦躁
然后还是看了一下转载了.但是我就想不通最后两维,好艰难啊. 
今天早上还是琢磨了一下,我才把它弄懂.

se->select. 
dp[i][j][se][nse] 代表 前i个数,和为j,有se个数必选,有nse个必不选的方案数
是不是很凌乱.我们一步步来,先想前2维.我到不知道为什么别人数是个背包,千万别和背包混一起,我感觉会走弯路,不过确实也类似了.

我的开始是这样想的. 
首先 dp[i][j].前i个数和为j,对于当前这个数,如果选择了,就得+dp[i-1][j-a[i]],这时如果不选,+dp[i-1][j].这个先想明白. 
然后再想到第3维(第4维也是类似). 
dp[i][j][1]+=dp[i-1][j][1] ,首先前[i-1]个数和为j的方案数,而且其中有1个是必选了,但没有选a[i]
dp[i][j][1]+=dp[i-1][j-a[i]][1],首先前[i-1]个数和为j的方案数,而且其中有1个是必选了,,但选了a[i].a[i],不是必选的
dp[i][j][1]+=dp[i-1][j-a[i]][0],首先前[i-1]个数和为j的方案数,而且其中有1个是必选了,,选了a[i].a[i]是必选的
这样理解应该懂了吧,其中1维也是这样的思想..这就推出来了几种必选必不选的方案数了.

分析: 计数dp.

转载:http://blog.csdn.net/zzz805/article/details/52135094

#include <bits/stdc++.h>

using namespace std;
const int mod=1e9+;
int dp[][][][];
int n,s,T;
int a[];
int main()
{
scanf("%d",&T);
for(;T>;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++) //前i件物品
for(int j=;j<=s;j++) //背包当前容量为j
for(int x=;x<=;x++) //dp[i][j]x][y]中有x个物品是属于必选的
for(int y=;y<=;y++) //dp[i][j][x][y]中有y个物品是属于不能选的
{
dp[i][j][x][y]=(dp[i][j][x][y]+dp[i-][j][x][y])%mod; //第i件item不选
if (y->=) dp[i][j][x][y]=(dp[i][j][x][y]+dp[i-][j][x][y-])%mod;
if (j-a[i]>=)
{
dp[i][j][x][y]=(dp[i][j][x][y]+dp[i-][j-a[i]][x][y])%mod;
if (x->=) dp[i][j][x][y]=(dp[i][j][x][y]+dp[i-][j-a[i]][x-][y])%mod;
}
}
long long ans=;
for(int j=;j<=s;j++) ans=(ans+dp[n][j][][])%mod;
ans=(ans*)%mod;
printf("%lld\n",ans);
}
return ;
}

最新文章

  1. mysql 远程访问权限
  2. Thinking in java学习笔记之迭代器
  3. python基础知识
  4. MVC 图片预览
  5. Lucene/Solr搜索引擎开发笔记 - 第2章 Solr安装与部署(Tomcat篇)
  6. CSS练习一(模仿163邮箱登陆)
  7. android SDK启动的错误
  8. 2014-10 u-boot make过程分析
  9. 140. Word Break II
  10. mysql触发器之姓名转姓名拼音
  11. 关于ng的路由的几点想法(ui-view)
  12. angularjs directive (自定义标签解析)
  13. Java IO 学习总结 学习手册总结
  14. Webpack 2 视频教程 011 - Webpack2 中加载 CSS 的相关配置与实战
  15. 提高测试脚本复用性降低DOM结构引起路径变化的影响
  16. 在 Visual Studio 中使用 IntelliTrace 快照功能
  17. nginx+keepalived高可用及双主模式
  18. 使用windows命令和iconv.exe批量转换文件编码
  19. node.js之爬虫
  20. flexible.js结合rem实现移动端自适应布局

热门文章

  1. 【MSDN_C#】C#版本介绍
  2. js正则匹配两位小数
  3. Python3.x:chrome运行webdriver脚本提示--ignore-certificate-errors
  4. 0ctf2017-pages-choices
  5. js事件委托篇(附js一般写法和js、jq事件委托写法)
  6. Spring MVC工作流程图
  7. Centos7 ActiveMQ 安装并配置为开机启动
  8. dd命令参数解析
  9. 批处理命令 For循环命令详解!
  10. 如何使用JMX监控Kafka