分析:首先定义状态dp[i][j][s1][s2]代表前i个物品中,选若干个物品,总价值为j

其中s1个物品时必选,s2物品必不选的方案数

那么转移的时候可以考虑,第i个物品是可选可可不选的

  • dp[i][j][s1][s2]+=dp[i-1][j][s1][s2]+dp[i-1][j-a[i]][s1][s2]

或者第i个物品必选,或者必不选

  • dp[i][j][s1][s2]+=dp[i-1][j-a[i]][s1-1][s2]+dp[i-1][j][s1][s2-1]

一点感想:这个题边界时dp[0][0][0][0]=1;

当i不等于0时,dp[i][0][s1][s2]也是有意义的,因为可以代表s2个物品必不选,所以从0开始

由于空间是O(9E6)的,太大,类似01背包优化成一维O(9e3)

时间复杂度O(N*S*9)(发现又是一道计数dp,一般涉及计数的dp不是特别好做,主要是人傻)

#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 1e3+;
const int mod = 1e9+;
int dp[N][][];
int a[N],T,n,s;
inline void up(int &x,int y){
x+=y;if(x>=mod)x-=mod;
}
inline int mul(int x,int y){
int ret=(1ll*x*y)%(1ll*mod);
return ret;
}
int main(){
scanf("%d",&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=s;j>=;--j)
for(int s1=;s1>=;--s1)
for(int s2=;s2>=;--s2){
int tmp=;
up(tmp,dp[j][s1][s2]);
if(j>=a[i])up(tmp,dp[j-a[i]][s1][s2]);
if(s1&&j>=a[i])up(tmp,dp[j-a[i]][s1-][s2]);
if(s2)up(tmp,dp[j][s1][s2-]);
dp[j][s1][s2]=tmp;
}
int ret=;
for(int i=;i<=s;++i)up(ret,dp[i][][]);
ret=mul(ret,);
printf("%d\n",ret);
}
return ;
}

HDU5800 To My Girlfriend 背包计数dp的更多相关文章

  1. hdu 5800 To My Girlfriend(背包变形)

    To My Girlfriend Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. 题解报告:hdu 1028 Ignatius and the Princess III(母函数or计数DP)

    Problem Description "Well, it seems the first problem is too easy. I will let you know how fool ...

  3. CodeForces 176B Word Cut (计数DP)

    Word Cut Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit St ...

  4. [DP之计数DP]

    其实说实在 我在写这篇博客的时候 才刚刚草了一道这样类型的题 之前几乎没有接触过 接触过也是平时比赛的 没有系统的做过 可以说0基础 我所理解的计数dp就是想办法去达到它要的目的 而且一定要非常劲非常 ...

  5. HDU4815/计数DP

    题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=4815] 简单说一下题意: 有n道题,每到题答对得分为a[ i ],假如A不输给B的最小概率是P,那么A ...

  6. HDU 6377 度度熊看球赛 (计数DP)

    度度熊看球赛 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  7. 计数dp

    计数dp 计数类的$dp$没做过几个,所以之前都放到"思维"标签下了,后来发现原来这属于一类问题啊...搬过来了. 管道取珠:https://www.lydsy.com/Judge ...

  8. [SDOI2010]地精部落[计数dp]

    题意 求有多少长度为 \(n\) 的排列满足 \(a_1< a_2> a_3 < a_4 \cdots\) 或者 $a_1> a_2 < a_3 > a_4\cdo ...

  9. 【BZOJ】2111: [ZJOI2010]Perm 排列计数 计数DP+排列组合+lucas

    [题目]BZOJ 2111 [题意]求有多少1~n的排列,满足\(A_i>A_{\frac{i}{2}}\),输出对p取模的结果.\(n \leq 10^6,p \leq 10^9\),p是素数 ...

随机推荐

  1. html 页面表单如果是disabled,则不能提交到服务器端,request.getParameter得到的将为null

    html 页面表单如果是disabled,则不能提交到服务器端,request.getParameter得到的将为null 解决方法:使用hidden 利用javascript赋值,传递到后台

  2. Data Flow ->> Slow Changing Dimension

    这里简单讲下SCD 在讲之前贴上两个有用的链接地址.作者的两篇文件讲解了SCD是什么以及应用 http://www.cnblogs.com/biwork/p/3363749.html http://w ...

  3. PV UV

    定义 PV: Page View       页面浏览量或点击量,用户每次刷新即被计算一次. UV: Unique Visitor  就是有多少个IP数量.就是指的有多少人在访问你的店.每个人用的电脑 ...

  4. Mybatis SqlSessionTemplate 源码解析

    As you may already know, to use MyBatis with Spring you need at least an SqlSessionFactory and at le ...

  5. sublime使用

    显示侧板的文件夹: View -> Side Bar 菜单[project]->add folder to  project.把文件夹显示在左边的sidebar上. 安装插件: packa ...

  6. android开发 eclipse alt+”/”自动提示失效

    http://blog.unvs.cn/archives/android-eclipse-alt.html 按照其中的第一条+第二条 处理 注意:abcd要替换为 abcdefghijklmnopqr ...

  7. R语言学习笔记——Base Graphics

    做exploratory data annalysis的作业,差点被虐死了,R从头开始,边做边学,最后搞到一点多才弄完,还有一个图怎么画都不对,最后发现是数据读取的时候有问题. 用来画图的数据来自:h ...

  8. AE 将地图导出为图片的两种方法

    在ArcGIS的开发中,我们经常需要将当前地图打印(或是转出)到图片文件中.将Map或Layout中的图象转出有两种方法,一种为通过IActiveView的OutPut函数,另外一种是通过IExpor ...

  9. hihoCoder 1040 矩形判断(计算几何)

    http://hihocoder.com/problemset/problem/1040 首先判断四条线段是否相交,给出八个点,如果有一些点重合,并且不同坐标的点只有4个的话,表示可以构成四边形. 然 ...

  10. 函数lock_rec_get_first_on_page

    lock结构体 详见 /*********************************************************************//** Gets the first ...