题目描述

ftiasch 有 N 个物品, 体积分别是 W1, W2, …, WN。 由于她的疏忽, 第 i 个物品丢失了。 “要使用剩下的 N – 1 物品装满容积为 x 的背包,有几种方法呢?” — 这是经典的问题了。她把答案记为 Count(i, x) ,想要得到所有1 <= i <= N, 1 <= x <= M的 Count(i, x) 表格。

输入输出格式

输入格式:

第1行:两个整数 N (1 ≤ N ≤ 2 × 10^3)N(1≤N≤2×103) 和 M (1 ≤ M ≤ 2 × 10^3)M(1≤M≤2×103),物品的数量和最大的容积。

第2行: N 个整数 W1, W2, …, WN, 物品的体积。

输出格式:

一个 N × M 的矩阵, Count(i, x)的末位数字。

This DP is pretty hard.

First we should know that F[i][j] means that how many funcation what we can have when we put i's stuff in the bag which has j's volume.

If the i's stuff had to taken, it wil  be f[i-1][j-w[i]], else, it will be f[i-1][j], So we can get the funcation :f[i][j]=f[i-1][j]+f[i-1][j-w[i];

we can use rounded array change it to f[j]=f[j]+f[j-w[i]].

So, how can we get the count ?

we had to enumeration whitch stuff we had lost.

if w[i]>j, that means, all of the answer has include the stuff i, because it was bigger than the volume. Therefore, the answer should be f[j] , which means take all of the answer.

if w[i]<=j, that means there are some anwer has be counted. what we should do is minus the rest of stuff(except i) to pull in j-w[i]. which is f[j]-c[i][j-w[i]]

if w[i]==0 , the c[i][j] will be 1.

that's all.

 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define in(a) a=read()
#define REP(i,k,n) for(int i=k;i<=n;i++)
using namespace std;
inline int read(){
int x=,f=;
char ch=getchar();
for(;!isdigit(ch);ch=getchar())
if(ch=='-')
f=-;
for(;isdigit(ch);ch=getchar())
x=x*+ch-'';
return x*f;
}
int n,m;
int f[],c[][],w[];
int main(){
in(n),in(m);
REP(i,,n) in(w[i]);
f[]=;
REP(i,,n)
for(int j=m;j>=w[i];j--)
f[j]=(f[j]+f[j-w[i]])%;
REP(i,,n){
c[i][]=;
REP(j,,m){
if(j<w[i]) c[i][j]=f[j];
else c[i][j]=(f[j]-c[i][j-w[i]]+)%;
printf("%d",c[i][j]);
}
printf("\n");
}
return ;
}

洛谷P4141消失之物的更多相关文章

  1. 洛谷P4141 消失之物——背包

    题目:https://www.luogu.org/problemnew/show/P4141 竟然是容斥:不选 i 物品只需减去选了 i 物品的方案: 范围原来是2*10^3而不是2*103啊... ...

  2. 洛谷P4141消失之物(背包经典题)——Chemist

    题目地址:https://www.luogu.org/problemnew/show/P4141 分析:这题当然可以直接暴力枚举去掉哪一个物品,然后每次暴力跑一遍背包,时间复杂度为O(m*n^2),显 ...

  3. [洛谷P4141] 消失之物「背包DP」

    暴力:暴力枚举少了哪个,下面套一个01背包 f[i][j]表示到了i物品,用了j容量的背包时的方案数,f[i][j]=f[i-1][j]+f[i-1][j-w[i]]O(n^3) 优化:不考虑消失的, ...

  4. 洛谷P4141 消失之物 题解 背包问题扩展

    题目链接:https://www.luogu.com.cn/problem/P4141 题目大意: 有 \(n\) 件物品,求第 \(i\) 件物品不能选的时候(\(i\) 从 \(1\) 到 \(n ...

  5. P4141 消失之物

    目录 链接 思路 代码 链接 P4141 消失之物 思路 f[N];//表示删掉物品后能出现容积为i的方案数 a[N];//单纯0-1背包的方案数asd 那么就先求出a[i]来,然后转移就是 if(j ...

  6. [BZOJ 2287/POJ openjudge1009/Luogu P4141] 消失之物

    题面: 传送门:http://poj.openjudge.cn/practice/1009/ Solution DP+DP 首先,我们可以很轻松地求出所有物品都要的情况下的选择方案数,一个简单的满背包 ...

  7. P4141 消失之物(背包)

    传送门 太珂怕了……为什么还有大佬用FFT和分治的…… 首先如果没有不取的限制的话就是一个裸的背包 然后我们考虑一下,正常的转移的话代码是下面这个样子的 ;i<=n;++i) for(int j ...

  8. Luogu P4141 消失之物 背包 分治

    题意:给出$n$个物品的体积和最大背包容量$m$,求去掉一个物品$i$后,装满体积为$w\in [1,m]$背包的方案数. 有 N 个物品, 体积分别是 W1, W2, …, WN. 由于她的疏忽, ...

  9. luogu p4141 消失之物(背包dp+容斥原理)

    题目传送门 昨天晚上学长讲了这题,说是什么线段树分治,然后觉得不可做,但那还不是正解,然后感觉好像好难的样子. 由于什么鬼畜的分治不会好打,然后想了一下$O(nm)$的做法,想了好长时间觉得这题好像很 ...

随机推荐

  1. 用matplotlib制作的比较满意的蜡烛图

    用matplotlib制作的比较满意的蜡烛图 2D图形制作包, 功能强大, 习练了很久, 终于搞定了一个比较满意的脚本. 特点: 使用方面要非常简单 绘制出来的图要非常的满意, 具有如下的特点 时间和 ...

  2. gulp.js 的安装以及使用

    首先:电脑需要安装 Node.js 一个大绿色的安装按钮,点击就可以. 但还是推荐,点击download选中一款适合电脑配置的版本. Node安装过程,就是下一步 and 下一步~~ 测试手否安装成功 ...

  3. 在Emacs中启用Fcitx输入法

    安装fcitx输入法,在 ~/.xinitrc文件中添加如下内容 (我用startx启动图形环境,所以在~/.xinitrc中配置X会话) export LC_CTYPE="zh_CN.UT ...

  4. auto

    把左和右外边距设置为 auto,规定的是均等地分配可用的外边距.结果就是居中的元素: <style> .centerrr { margin:auto; width:70%; backgro ...

  5. Unity3d 常用代码

    //创建一个名为"Player"的游戏物体 //并给他添加刚体和立方体碰撞器. player=new GameObject("Player"); player. ...

  6. linux笔记_day09

    1.运算器.控制器.存储器.输入输出(IO) 地址总线:内存寻址 数据总线:传输数据 控制总线:控制指令 寄存器:cpu暂时存储器 2.系统设定 默认输出设备:标准输出,STDOUT,1(描述符)(显 ...

  7. 从Dying gasp功能看Linux的响应速度(zhuan)

    转自https://blog.csdn.net/qq_20405005/article/details/77967358 前一阵子在做dying gasp功能测试,过程中恰好测试到了Linux的响应速 ...

  8. 查看mysql 库信息和表结构与表创建方法

    一.查看基础信息 1.查看数据库存储位置 show global variables like "%datadir%"; 2.查看数据库的版本 select version(); ...

  9. Redis常见业务场景应用

    一定时间范围内不可重复发短信问题 Redis实现消息队列 Redis实现Session共享 ...

  10. 08 Go 1.8 Release Notes

    Go 1.8 Release Notes Introduction to Go 1.8 Changes to the language Ports Known Issues Tools Assembl ...