洛谷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)$的做法,想了好长时间觉得这题好像很 ...
随机推荐
- spring框架学习(三)spring与junit整合测试
package cn.mf.b_test; import javax.annotation.Resource; import org.junit.Test; import org.junit.runn ...
- html5 canvas贝塞尔曲线篇(下)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 原生JavaScript技巧大收集(1~10)
1.原生JavaScript实现字符串长度截取 01 function cutstr(str, len) { 02 var temp; 03 var icount = 0; 04 ...
- java CountDownLatch的使用
CountDownLatch能够使一个线程在等待另外一些线程完成各自工作之后,再继续执行.使用一个计数器进行实现.计数器初始值为线程的数量.当每一个线程完成自己任务后,计数器的值就会减一.当计数器的值 ...
- 20155238 2016-2017-2 《Java程序设计》第五周学习总结
教材学习内容总结 Java语言中所有的错误都会包装为对象.使用try.catch可以对对象做处理. 设计错误对象都继承自java.lang.Throwable类.Throwable定义了取得错误信息, ...
- VirtualBox中CentOS遇到的问题
centos7 安装步骤 https://www.cnblogs.com/hihtml5/p/8217062.html 静态ip设置 TYPE="Ethernet" PROXY_M ...
- android休眠唤醒驱动流程分析【转】
转自:http://blog.csdn.net/hanmengaidudu/article/details/11777501 标准linux休眠过程: l power managemen ...
- thymeleaf : EL1050E The arguments (...) for the constructor call are missing
<a href="list.html" th:href="${#strings.replace(new.href,'{page}',1)}" >MO ...
- Android方法引用数超过65535优雅解决
随着应用不断迭代更新,业务线的扩展,应用越来越大(比如:集成了各种第三方SDK或者公共开源的Library文件.jar文件)这样一来,项目耦合性就很高,重复作用的类就越来越多了,SO:问题就来了.相信 ...
- Web前端开发规范文档你需要知道的事
Web前端开发规范文档你需要知道的事 规范目的 为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档. 本规范文档一经确认, 前端开发人员必须按本文档规范进 ...