codeforces 543A 完全背包

安排n个人写m行代码,每个人每行会出a[i]个bug,求最多出现b个bug的方案数。

一个二维的完全背包,每个人有两个状态:写j行代码出k个bug

dp[i][j][k] 前i个程序员写钱j行出现k个bug的方案数。

dp[i][j][k] = dp[i][j-1][k-a[i]] + dp[i-1][j][k];

注意这里数组会超内存,需要用滚动数组。

#include <iostream>
using namespace std;

int n, m, b;
long long mod;

long long a[505];

long long dp[2][505][505];

int main () {

    cin >> n >> m >> b >> mod;

    for (int i=1; i<=n; i++) {
        cin >> a[i];
    }

    for (int i=1; i<=n; i++) dp[i % 2][0][0] = 1L;

    for (int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) {
            for (int k=0; k<=b; k++) {
                if (k < a[i]) dp[i % 2][j][k] = dp[(i-1) % 2][j][k] % mod;
                else dp[i % 2][j][k] = (dp[i % 2][j-1][k - a[i]] + dp[(i-1) % 2][j][k]) % mod;
            }
        }
    }

    long long ans = 0;

    for (int i=0; i<=b; i++) {
        ans = (ans + dp[n % 2][m][i]) % mod;
    }

    cout << ans % mod << endl;

    return 0;
}
时间: 2024-08-10 14:59:17

codeforces 543A 完全背包的相关文章

CodeForces 543A - Writing Code DP 完全背包

有n个程序,这n个程序运作产生m行代码,但是每个程序产生的BUG总和不能超过b, 给出每个程序产生的代码,每行会产生ai个BUG,问在总BUG不超过b的情况下, 我们有几种选择方法思路:看懂了题意之后就是一个完全背包题了 定义dp[ i ][ j ][ k ] 表示前 i 个程序员,已经写了 j 行代码, 已经产生了 k 个 bugs . 根据题意,得知第 i 个程序员会写 r 行代码,那么相当于 dp[ i ][ j ][ k ] += dp[i - 1][j - r][k - ra[ i ]

CodeForces - 864E (背包+路径)

链接:http://codeforces.com/contest/864/problem/E 给出n个物品,以及每个物品的拿取所需时间,和最终时间(最终时间-1为可以拿取的时间),以及物品的价值. 问你可以拿到的最大价值值和一共拿取几件.同一时间只能够拿取一件,按照最大价值的拿取顺序输出. 思想:利用背包的特性,因为有最终时间和所需时间,利用01背包然后找去最佳拿取时间,最后利用vector将上一时间的物品,转化到此时. #include<cstdio> #include<cstring

Codeforces 336C 0-1背包

题意:每个水果有两个值,一个美味度 a,一个卡路里 b,从中挑选一些,要求 sum(aj) / sum(bj) = k,使得 sum(a) 最大. 分析:没有那个条件就是一个01背包,可以转换,对公式变形,每个水果的重量为 a[i] - b[i] *k ,那么无论怎么挑选,都满足那个条件,价值是 a[i] 但是,a[i] - b[i] *k 可能为负,那么可以分类讨论,背包容量10000即可,最后枚举背包容量,能够达到的值是两个部分的和. #include <bits/stdc++.h> us

!CodeForces 543A Writing Code --DP--(三维dp,滚动数组)

题意:n个程序员一起写m行代码,第i个程序员每写一行有a[i]个bug,求总bug不超过b的分配方案有多少种 分析:这题很像完全背包,不过求的不是最大/最小bug数,求的是bug数小于上限的分配方案.所以这题要用三维dp dp[i][j][k]表示前i个个程序员总共写了j行代码产生了k个bug时的plan数,这题有两种转移:1)第i个程序员再写一行:2)换第i+1号程序员写 所以方程:dp[i][j][k]=dp[i-1][j][k]+dp[i][j-1][k-a[i]],注意:程序员可能一行都

H - Fire CodeForces - 864E 01背包

https://codeforces.com/problemset/problem/864/E 这个题目要把这个按照物品毁灭时间进行排序,如果时间短就要排在前面,这个是因为要保证之后的物品的拯救不会影响到之前的. #include <cstdio> #include <cstring> #include <cstdlib> #include <queue> #include <stack> #include <vector> #inc

Codeforces 543A Writing Code

A. Writing Code 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

[训练日志] 7月22-31日

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "PingFang SC"; color: #454545 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Times New Roman"; color: #454545; min-height: 15.0px } span.s1 { } span.s2 { font: 12.0px "

Writing Code

Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 543A Description Programmers working on a large project have just received a task to write exactly m lines of code. There are n programmer

codeforces 148E Aragorn&#39;s Story 背包DP

Aragorn's Story Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/148/E Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who