洛谷P4141消失之物
题目描述
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消失之物的更多相关文章
- 洛谷P4141 消失之物——背包
题目:https://www.luogu.org/problemnew/show/P4141 竟然是容斥:不选 i 物品只需减去选了 i 物品的方案: 范围原来是2*10^3而不是2*103啊... ...
- 洛谷P4141消失之物(背包经典题)——Chemist
题目地址:https://www.luogu.org/problemnew/show/P4141 分析:这题当然可以直接暴力枚举去掉哪一个物品,然后每次暴力跑一遍背包,时间复杂度为O(m*n^2),显 ...
- [洛谷P4141] 消失之物「背包DP」
暴力:暴力枚举少了哪个,下面套一个01背包 f[i][j]表示到了i物品,用了j容量的背包时的方案数,f[i][j]=f[i-1][j]+f[i-1][j-w[i]]O(n^3) 优化:不考虑消失的, ...
- 洛谷P4141 消失之物 题解 背包问题扩展
题目链接:https://www.luogu.com.cn/problem/P4141 题目大意: 有 \(n\) 件物品,求第 \(i\) 件物品不能选的时候(\(i\) 从 \(1\) 到 \(n ...
- P4141 消失之物
目录 链接 思路 代码 链接 P4141 消失之物 思路 f[N];//表示删掉物品后能出现容积为i的方案数 a[N];//单纯0-1背包的方案数asd 那么就先求出a[i]来,然后转移就是 if(j ...
- [BZOJ 2287/POJ openjudge1009/Luogu P4141] 消失之物
题面: 传送门:http://poj.openjudge.cn/practice/1009/ Solution DP+DP 首先,我们可以很轻松地求出所有物品都要的情况下的选择方案数,一个简单的满背包 ...
- P4141 消失之物(背包)
传送门 太珂怕了……为什么还有大佬用FFT和分治的…… 首先如果没有不取的限制的话就是一个裸的背包 然后我们考虑一下,正常的转移的话代码是下面这个样子的 ;i<=n;++i) for(int j ...
- Luogu P4141 消失之物 背包 分治
题意:给出$n$个物品的体积和最大背包容量$m$,求去掉一个物品$i$后,装满体积为$w\in [1,m]$背包的方案数. 有 N 个物品, 体积分别是 W1, W2, …, WN. 由于她的疏忽, ...
- luogu p4141 消失之物(背包dp+容斥原理)
题目传送门 昨天晚上学长讲了这题,说是什么线段树分治,然后觉得不可做,但那还不是正解,然后感觉好像好难的样子. 由于什么鬼畜的分治不会好打,然后想了一下$O(nm)$的做法,想了好长时间觉得这题好像很 ...
随机推荐
- Linux ------清除内存中的cache
首先以Centos6.4的来说,Centos7有些区别 一.buffer/cache/swap的介绍 #cat /etc/redhat-release #查看系统版本 CentOS release ...
- ZCMU 1894: Power Eggs
http://acm.zcmu.edu.cn/JudgeOnline/problem.php?id=1894 题意: 有M个鹰蛋,N层楼,鹰蛋的硬度是E,也就是说在1~E层楼扔下去不会碎,E+1层楼扔 ...
- 内网服务器通过Squid代理访问外网
环境说明 项目整体需部署Zabbix监控并配置微信报警,而Zabbix Server并不能访问外网,故运维小哥找了台能访问外网的服务器做Suqid代理,Zabbix Server服务器通过代理服务器访 ...
- CentOS7 关闭防火墙和selinux
本文将简单介绍在CentOS7上如何临时和永久关闭防火墙和selinux. 关闭防火墙 # 查看防火墙状态 [root@localhost ~]# systemctl status firewalld ...
- redhat7配置本地yum源
1.首先是要有一个iso文件,并将这个文件挂载到某个目录 挂载: 配置: 检验: yum list 现在你就可以在没有网的情况下,安装软件了~~~
- 洛谷 P5089: CodeForces #500 (Div. 1) B / 1012B : Chemical table
题目传送门:洛谷P5089. 题意简述: 一张 \(n \times m\) 的表格,有一些格子有标记,另外一些格子没有标记. 如果 \((r_1,c_1),(r_1,c_2),(r_2,c_1)\) ...
- 大数据系列之并行计算引擎Spark介绍
相关博文:大数据系列之并行计算引擎Spark部署及应用 Spark: Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎. Spark是UC Berkeley AMP lab ( ...
- DDD领域模型企业级系统(三)
相关代码: public static void ShowArray() { //数据源 int[] arrayas = new int[] { 1, 2, 3, 4 }; //创建查询 var qu ...
- Navicat Premium 常用功能讲解
https://www.linuxidc.com/Linux/2016-04/130159.htm Navicat Premium 常用功能讲解 1.快捷键 1.1. F8 快速回到当前对象列表 1. ...
- Using MongoDB in C#
In order to use MongoDB in C#, you can import MongoDB C# Driver to your project. It's best to create ...