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-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 1001 1 1

output

10

input

3 6 5 10000000071 2 3

output

0

input

3 5 6 111 2 1

output

0

大意:有n个人写m行代码,最大的bug错误为b,取模为mod,下面n行表示这n个人写一行代码为犯得错误,问你一共有多少种情况是的bug数目不超过b

定义 dp[i][j] 表示选定了i个人,犯得bug为j的种类   那么得到状态转移方程 dp[j][k] =(dp[j][k] + dp[j-1][k-a[i]])%mod

表示当前j个人k个bug可以由原来的以及少一个人之后选择那个人转移过来

注意初始化为dp[0][0...b] = 1 表示不选择人的时候所有的bug的种类犯错误都只有一种

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    int dp[550][550];
    int n,m,a[550],mod,b;
    while(~scanf("%d%d%d%d",&n,&m,&b,&mod)){
        for(int i = 1; i <= n ;i++)
            scanf("%d",&a[i]);
        memset(dp,0,sizeof(dp));
        for(int i = 0; i <= b ;i++)
            dp[0][i] = 1;
        for(int i = 1; i <= n ; i++){
            for(int j = 1; j <= m; j++){
                for(int k = a[i]; k <= b ; k++){
                    dp[j][k] = (dp[j][k]+dp[j-1][k-a[i]])%mod;
                }
            }
        }
        printf("%d\n",dp[m][b]);
    }
    return 0;
}

  

时间: 2024-12-06 12:24:00

Codeforces Round #302 (Div. 2)——C dp—— 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 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 的每个字符进行判断,如果该字符在之前没有出现过,那么从它开始就可以组成一个新的字符串,并且计数,如果到了k 了则把之后的都归为一个字符串. #include <cstring> #include

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

题目传送:Codeforces Round #302 (Div. 2) A. Set of Strings 思路:注意开头字母都不相同 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include &

水题 Codeforces Round #302 (Div. 2) A Set of Strings

题目传送门 1 /* 2 题意:一个字符串分割成k段,每段开头字母不相同 3 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <string> 9 #include <algorithm> 10 using namespace std; 11 12 con

Codeforces Round #261 (Div. 2) E (DP)

E. Pashmak and Graph Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem. You are

Codeforces Round #339 (Div. 2) B. Gena&#39;s Code

B. Gena's Code It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, f

Codeforces Round #302 (Div. 2) (ABCD题解)

比赛链接:http://codeforces.com/contest/544 A. Set of Strings time limit per test:1 second memory limit per test:256 megabytes You are given a string q. A sequence of k strings s1,?s2,?...,?sk is called beautiful, if the concatenation of these strings is

Codeforces Round #302 (Div. 1 D)

http://codeforces.ru/contest/543/problem/D Problem 给一棵树,n个节点,n-1条边.把每个点当作首都,输出一个结果,一共要输出n个结果.边有两种形态,一种是好边,一种是坏边,当把一个点当作首都的时候,要求这个点到每个点的路径上坏边总数<=1,问有多少种树的形态. n: 10^5级别 Solution 很显然树形dp.设dp[i]表示,以 i 为根的子树的方案数.如果只需要算dp[1],直接dfs一遍,回溯的时候,dp[i]=∏(dp[j]+1),

Codeforces Round #302 (Div. 1 A)

http://codeforces.ru/contest/543/problem/A Problem N个人写代码,a[i]表示第i个人,写一行代码会出现a[i]个bug(不多也不少).现在问,N个人总共写M行,出现bug总数不超过b的方案数(允许有人一行也不写) 数据范围 N,M,b,a[i] : [1,500] Solution 先考虑最直接的dp.dp[i][j][k]表示前i个人,写了j 行,出现k个bug的方案数,那么转移,dp[i][j+j1][k+j1*a[i]]+=dp[i][j