Codeforces544C: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 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 nmbmod (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个程序,这n个程序运作产生m行代码,但是每个程序产生的BUG总和不能超过b,给出每个程序产生的代码,每行会产生ai个BUG,问在总BUG不超过b的情况下,我们有几种选择方法

思路:看懂了题意之后就是一个完全背包题了

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define LL long long
#define N 205
#define MOD 19999997
#define INF 0x3f3f3f3f
#define EXP 1e-8

const double Pi = acos(-1.0);

int n,m,b,mod;
int a[505];
LL dp[505][505],ans;

int main()
{
    int i,j,k;
    cin>>n>>m>>b>>mod;
    UP(i,1,n)
    cin>>a[i];
    dp[0][0] = 1;
    UP(i,1,n)
    {
        UP(k,1,m)
        {
            UP(j,a[i],b)
            {
                dp[k][j]+=dp[k-1][j-a[i]];
                dp[k][j]%=mod;
            }
        }
    }
    ans = 0;
    UP(i,0,b)
    {
        ans+=dp[m][i];
        ans%=mod;
    }
    cout<<ans<<endl;

    return 0;
}

时间: 2024-11-08 22:56:58

Codeforces544C:Writing Code(完全背包)的相关文章

完全背包 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][j-1][k-a[i]]: 5 错误示范:dp[i][j][k] += dp[i-1][j-l][k-l*a[i]]; 其实要从上一行的状态推出,即少一行 6 内存限制,

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

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 ]

背包DP || Codeforces 544C Writing Code

程序员写bug的故事23333 题意:n个程序员,一共写m行程序,最多产生b个bug,问方案数 思路:f[i][j]表示写了i行,产生了j个bug的方案数,因为每个人都是可以独立的,所以i循环到n都做一遍 f[i][j] += f[i-1][j-a[i]] 在前一行  i 的 a[i] 个bug还没有写上去的情况数 #include <iostream> #include <cstdio> #include <algorithm> using namespace std

Codeforces Round #302 (Div. 2)——C dp—— 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 sequence of non

!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]],注意:程序员可能一行都

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

What Every CLR Developer Must Know Before Writing Code

https://github.com/dotnet/coreclr/blob/master/Documentation/coding-guidelines/clr-code-guide.md

2017-8-8 Summer:背包系列+斜率优化

A - 最大报销额(01背包) 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C类),要求每张发票的总额不得超过1000元,每张发票上,单项物品的价值不得超过600元.现请你编写程序,在给出的一堆发票中找出可以报销的.不超过给定额度的最大报销额. Input 测试输入包含若干测试用例.每个测试用例的第1行包含两个正数 Q 和 N,其中 Q 是给定的报销额度,N(<=30)是发票张数.随后是 N 行输入,每行的格式为: m Type1:price1