Codeforces Round #302 (Div. 1) 543A Writing Code(dp)

完全背包问题(二维数组) 通过三天的发文章,终于稍微弄明白Markdown了,接下来话不多说,直接干正事。 前两篇文章分别和大家讲了一下01背包的两种做法,感兴趣的可以去看看。 今天和大家分享一下完全背包问题的解题思路(二维数组)。 本文在写完全背包的解题方法时会与01背包做对比 题目如下: 【题目描述】 设有n种物品,每种物品有一个重量及一个价值。但每种物品的数量是无限的,同时有一个背包,最大载重量为M,今从n种物品中选取若干件(同一种物品可以多次选取),使其重量的和小于等于M,而价值的和为最大。【输入】 第一行:两个整数,M(背 阅读详情

A. Writing Code
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Programmers working on a large project have just received a task to write exactly m lines of code. There are n programmers working on a project, the i-th of them makes exactly ai bugs in every line of code that he writes.

Let's call a sequence of non-negative integers v1, v2, ..., vn a plan, if v1 + v2 + ... + vn = m. The programmers follow the plan like that: in the beginning the first programmer writes the first v1 lines of the given task, then the second programmer writes v2 more lines of the given task, and so on. In the end, the last programmer writes the remaining lines of the code. Let's call a plan good, if all the written lines of the task contain at most b bugs in total.

Your task is to determine how many distinct good plans are there. As the number of plans can be large, print the remainder of this number modulo given positive integer mod.

Input

The first line contains four integers n, m, b, mod (1 ≤ n, m ≤ 500, 0 ≤ b ≤ 500; 1 ≤ mod ≤ 109 + 7) — the number of programmers, the number of lines of code in the task, the maximum total number of bugs respectively and the modulo you should use when printing the answer.

The next line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 500) — the number of bugs per line for each programmer.

Output

Print a single integer — the answer to the problem modulo mod.

Sample test(s)
input
3 3 3 100
1 1 1
output
10
input
3 6 5 1000000007
1 2 3
output
0
input
3 5 6 11
1 2 1
output
0



n个程序员写m行代码,一行会出现a[i]个bug,问你出现bug总数不超过b的方案数。


完全背包的思想,将dp降维优化,状态转移方程:dp[k][j] += dp[k - 1][j - a[i]]。


AC代码:


#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MAXN = 510;
typedef long long ll;
int n, m, b, mod, a[MAXN];
ll dp[MAXN][MAXN];
int main(int argc, char const *argv[])
{
	scanf("%d%d%d%d", &n, &m, &b, &mod);
	for(int i = 1; i <= n; ++i)
		scanf("%d", &a[i]);
	dp[0][0] = 1;
	for(int i = 1; i <= n; ++i)
		for(int k = 1; k <= m; ++k)
			for(int j = a[i]; j <= b; ++j) {
				dp[k][j] += dp[k - 1][j - a[i]];
				dp[k][j] %= mod;
			}
	ll ans = 0;
	for(int i = 0; i <= b; ++i)
		ans += dp[m][i];
	printf("%lld\n", ans % mod);
	return 0;
}


地质建模师必备:用Petrel快速生成浅地层TST/TVT厚度图的5个关键步骤 本文详细解析了地质建模师如何利用Petrel平台,将二维浅地层SEG-Y数据高效转化为真实地层厚度图(TST)和真实垂直厚度图(TVT)。文章系统梳理了从数据规范化导入、坐标系统一、地质界面解释到厚度计算与成图的5个关键实战步骤,并强调了误差控制与工程应用,为海上风电等工程地质评估提供了完整解决方案。 阅读详情

相关推荐

HAMi 核心模块与架构设计

HAMi 的架构设计是围绕 Kubernetes 生态系统构建的,通过扩展 Kubernetes 的调度能力和设备管理能力,为用户提供了高效、灵活的异构设备虚拟化和管理解决方案。

北斗云的博客 2552

Codeforces Round #302 (Div. 2)C (完全背包)

C. Writing Code time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Programmers working on a large project have

u013268685的专栏(有一种幸福叫AC,有一种期待叫AK) 1186

Win11 C盘爆满别慌!手把手教你安全迁移AppData和用户文件夹(附一键脚本)

本文针对Win11 C盘空间不足问题,提供了一套安全有效的解决方案。通过深入解析用户目录结构,指导用户利用符号链接和Robocopy工具,将AppData、下载文档等占用大户迁移至其他分区,并附带了自动化脚本,从根本上实现C盘瘦身,同时确保系统稳定和数据安全。

weixin_29231221的博客 2087

codeforces544

You are given a string q. A sequence of k strings s1, s2, …, sk is called beautiful, if the concatenation of these strings is string q (formally, s1 + s2 + … + sk = q) and the first characters of these

Fire Dancer 573

【ros2】从零认识URDF:机器人模型描述的“说明书”

visual>子节点只负责机器人在RViz2中的可视化效果,不参与任何物理计算,就像给零件“穿衣服”。<origin>:定义零件的局部坐标系相对于连杆原点的位姿,参数xyz表示平移(单位:米),rpy表示旋转(单位:弧度,绕x→y→z轴旋转);<geometry>:定义零件的几何形状,支持四种基础形状+外部3D模型;<material>:定义零件的颜色,需要提前定义材质。常见几何形状示例-- 连杆名称:base_link(底座),全局唯一 --> < visual > <!

EnHengNa的博客 1610

关于SVPWM七段式五段式调制后的相电压和线电压波形——大疆DJI技术笔试

1.SVPWM七段式/五段式调制的相电压和线电压波形 脉宽调制(PWM)在并网变流技术中得到了广泛的应用。与正弦脉宽调制(SPWM)的控制方法相比,SVPWM方法具有直流侧电压利用率高,电压谐波小,易于数字化实现等显著的优点,从而使SVPWM在实际的应用中越来越普遍。TMS320F28335是32位浮点的DSP控制器,具有150MHz的高速处理能力,18路PWM输出,与前几代数字信号控制器相比,性能平均提高了50%。其中具体的SVPWM理论实现的原理,请参考我的博客永磁同步电机的矢量控制策略(七)一一一SV

昔时扬尘处 2万+

Codeforces 543 A Writing Code 计数DP

http://codeforces.com/problemset/problem/543/A 题目大意:n个人,一共要写m行程序,每个程序员每行出现的bug数为ai,要求整个程序出现的bug数不超过b的方案数. 题目分析:简单的计数DP,01背包变形。需要记录的是写到第i行时错误的bug数为j的方案。所以定义二维数组dp[i][j]。转移与01背包类似。具体见代码;初始化d

WKLL_ 1015

告别复杂配置!5分钟在Vue/React项目中集成讯飞语音听写WebSocket API

本文详细介绍了如何在Vue/React项目中快速集成讯飞语音听写WebSocket API,实现高效语音识别功能。通过对比不同通信方案,突出WebSocket在实时语音交互中的优势,并提供框架适配、音频处理及性能优化等关键技术点,帮助开发者5分钟内完成专业级语音听写功能集成。

weixin_28730353的博客 274

Writing Code(详细)

思路 代码 #include <iostream> #include <algorithm> using namespace std; const int N=1010; int n,m,b,mod; long long sum; long long f[N][N],a[N]; int i,j,k; int main() { cin>>n>...

萌新的博客 603

【UE5】RTS游戏的框选功能+行军线效果实现

步骤: 一、项目准备; 二、框选NPC并移动到指定地点; 三、框选效果; 四、行军线效果

ChaoChao66666的博客 2378

AUTOSAR&UDS 理论要点及isolar实战-DEM模块

从这节开始,将讲解UDS诊断中非常重要的知识点:故障事件触发、故障信息上报、故障信息处理、故障信息存储和故障系统降级,后续会陆续更新。

cqaa11的博客 552

Codeforces Round #302 (Div. 1)

Codeforces Round #302 (Div. 1)

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 284

用 ABAP LSMW 的 Standard Batch / Direct Input 稳准快迁移物料主数据:从 MM01 录制到 RMDATIND 直入的落地指南

本文详细介绍了SAP数据迁移中LSMW工具的使用方法,重点分析了物料主数据迁移的两种主要路径:Batch Input Recording和Standard Batch/Direct Input。前者通过录制事务操作实现快速起步,后者通过调用预置接口程序实现稳定高效的批量处理。文章深入解析了Standard Direct Input的具体实施步骤,包括对象属性设置、源结构定义、字段映射规则等关键环节,并特别强调了迁移前的准备工作(如编号策略、视图策略等)对项目成功的重要性。通过对比两种方法的优缺点,为不同业务

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 154

C. Writing Code(Codeforces Round #302(div2)

C. Writing Code time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Programmers working on a large project have

ACway的专栏 3977

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Codeforces Round #302 (Div. 1) 训练

链接: http://codeforces.com/contest/543 过程: 惨淡的只做出了A和C 题解: A 题解: 简单的一道题 我们用$dp[i][j]$表示当前考虑到前num个人(这个另外枚举),当前写了$i$行代码,当前的bug数量为$j$的方案数 然后转移就是$dp[i][j]=\sum {dp[i-1][j-a[num]]}$ ...

aozhuan8489的博客 222

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

完全背包 Codeforces Round #302 (Div. 2) C Writing Code

题目传送门 1 /* 2 题意:n个程序员,每个人每行写a[i]个bug,现在写m行,最多出现b个bug,问可能的方案有几个 3 完全背包:dp[i][j][k] 表示i个人,j行,k个bug dp[0][0][0] = 1 表示不选择人的时候所有的bug的种类犯错误都只有一种 4 dp[i][j][k] += dp[i%2]...

weixin_34194702的博客 101

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Codeforces Round #302 (Div. 2)——C dp—— Writing Code

Programmers working on a large project have just received a task to write exactlymlines of code. There arenprogrammers working on a project, thei-th of them makes exactlyaibugs i...

dgjm4087的博客 142

Codeforces Round #302 (Div. 2) -- (A,B,C)

题目传送:Codeforces Round #302 (Div. 2) A. Set of Strings 思路:注意开头字母都不相同 AC代码: #include #include #include #include #include #include #include #include #include #include

Never say never 843

Codeforces Round #302 (Div. 2)C. Writing Code--dp

C. Writing Code time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Programmers working on a large project have

qq_21820377的专栏 378

Codeforces Round #302 (Div. 1) A. Writing Code

A. Writing Code time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Programmers working on a large project have

习惯与规则决定命运 985

Codeforces Round #302 (Div. 2) A B C

Codeforces Round #302 (Div. 2) A. Set of Strings 字符串 q 被称为 "beautiful" 当且仅当 q 可以被拆分成 k 个子串 (s1, s2, s3, ... , sk) 并且任意两个字串满足首字母不一样。 直接模拟,对 q 的每个字符进行判断,如果该字符在之前没有出现过,那么从它开始就可以组成一个新的字符串,并且计数,...

weixin_30542079的博客 104

完全背包——Codeforces Round #302 (Div. 2) C. Writing Code

题目链接: http://codeforces.com/problemset/problem/544/C 题意:给出 NN 个程序猿,第 ii 个程序猿写一行代码会产生 aia_i 个bug。你是他们的Boss,想知道怎么分配任务使得写完m行代码产生的bug数量不超过 mm ,否则你的公司就倒闭了:) 分析: 因为每个程序猿的工作量没有上限(是的呢,剩余价值无限大(老铁,修仙吗?)),所以我们发现这

FeBr2的博客 467

Codeforces Round #302 (Div. 2) A.Set of Strings&B.Sea and Islands&C.Writing Code

A. 题目意思是给定一个字符串和k,问这个字符串能否拆分成k个首字母不同的串,如果多个答案输出一组。 思路:扫描一遍字符串里有多少个不同的字符,如果小于k肯定不行...大于等于k的时候直接遇到首字母没出现过的元素就拆即可。 #include #include #include #include #include using namespace std; char s[300]; int ma

468

Codeforces Round #302 (Div. 2) C. Writing Code dp

题意大意:每个人写一行代码,会产生a个bug,给第总人数,需要写的代码总行数,及产生的总bug的最大值,每个人可以写多行代码(>=0),求总共有多少方案数(即安排每个人写的代码数的方案)。 连接:http://codeforces.com/contest/544/problem/C 其实是个完全背包,我就连别人的代码看了很久很久才有所觉悟,我们不难发现人数,代码数,bug数是需要我们考

chen_lin111的专栏 479

Codeforces Round #302 (Div. 2) C Writing Code(dp)

链接 n个程序员写m行代码,每个程序员写一行代码会出现ai个bug。一个优秀的plan是每个程序员写vi行代码。 使得:v1+v2+..+vn=m。并且总的bug不超过b。有多少种这样的plan。 题解: #include<bits/stdc++.h> using namespace std; #define ll...

weixin_30347335的博客 109

Writing Code【Codeforces Round #302 (Div. 2)】【周赛第一场E】【DP】【完全背包】

        这道题是我们周赛的第一场的一道题,一道经典的DP问题。Programmers working on a large project have just received a task to write exactly m lines of code. There are n programmers working on a project, the i-th of them mak...

既然弱小,就只顾变强就是了 411
上一篇: POJ2309 BST(树状数组)
下一篇: HDOJ3018 Ant Trip(欧拉回路 + 并查集)
GKHack
博客等级 码龄11年 48粉丝 291原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值