CF543A Writing Code

题目描述

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

Let's call a sequence of non-negative integers v1,v2,...,vn v_{1},v_{2},...,v_{n} v1,v2,...,vn a plan, if v1+v2+...+vn=m v_{1}+v_{2}+...+v_{n}=m v1+v2+...+vn=m . The programmers follow the plan like that: in the beginning the first programmer writes the first v1 v_{1} v1 lines of the given task, then the second programmer writes v2 v_{2} 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 b 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 mod mod .

输入输出格式

输入格式:

The first line contains four integers n n n , m m m , b b b , mod mod mod ( 1<=n,m<=500 1<=n,m<=500 1<=n,m<=500 , 0<=b<=500 0<=b<=500 0<=b<=500 ; 1<=mod<=109+7 1<=mod<=10^{9}+7 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 n n space-separated integers a1,a2,...,an a_{1},a_{2},...,a_{n} a1,a2,...,an ( 0<=ai<=500 0<=a_{i}<=500 0<=ai<=500 ) — the number of bugs per line for each programmer.

输出格式:

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

输入输出样例

输入样例#1: 复制
3 3 3 100
1 1 1
输出样例#1: 复制
10
输入样例#2: 复制
3 6 5 1000000007
1 2 3
输出样例#2: 复制
0
输入样例#3: 复制
3 5 6 11
1 2 1
输出样例#3: 复制
0


设f[i][j][k]表示前i个人, 写了j行, 有k个bug的方案个数;
然后完全背包转移, 加上滚动数组。


 
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
inline int read(){
    int res = 0;char ch=getchar();bool fl = 0;
    while(!isdigit(ch)){if(ch=='-')fl=1;ch=getchar();}
    while(isdigit(ch)){res=(res<<3)+(res<<1)+(ch^48);ch=getchar();}
    return fl?-res:res;
}
int n, m, b, mod;
int a[505];
int f[2][505][505];
int ans;

int main()
{
    n = read(), m = read(), b = read(), mod = read();
    for (register int i = 1 ; i <= n ; i ++) a[i] = read();
    f[0][0][0] = 1;
    for (register int i = 1 ; i <= n ; i ++){
        memset(f[i%2], 0, sizeof f[i%2]);
        for (register int j = 0 ; j <= m ; j ++){
            for (register int k = 0 ; k <= b ; k ++){
                if (k >= a[i] and j >= 1)
                    f[i%2][j][k] = f[i%2][j-1][k-a[i]] % mod;;
                f[i%2][j][k] = (f[i%2][j][k] + f[(i-1)%2][j][k]) % mod;
            }
        }
    }
    for (register int i = 0 ; i <= b ; i ++) ans = (ans + f[n%2][m][i]) % mod;
    printf("%d", ans % mod);
    return 0;
}
 
   

 


转载于:https://www.cnblogs.com/BriMon/p/9329273.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值