Codeforces 543A Writing Code 【滚动数组优化dp】
2015-11-09 18:11
435 查看
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
output
input
output
input
output
题意:有n个程序员要完成m行的代码,要求出现的bug数目不超过b。给定每个程序员写一行代码会出现的bug数,问你有多少种方案完成这m行代码。
比赛的一道题, 当时真心没时间看了。看过后发现是 一个裸的dp + 滚动数组
思路:设dp[i][j][k]表示前i个程序员写j行代码出现k个bug的方案数。用bug[i]表示第i个程序员每写一行出现的bug数目。
则有dp[i][j][k] = dp[i-1][j][k],且当j >= 1 && k >= bug[i]时有dp[i][j][k] += dp[i][j-1][k-bug[i]]。
因为每次都把dp[i][j][k]更新到底,所以不用考虑dp[i-1][j-1][k-bug[i]]的方案数,或者说,该方案数已经被包含在dp[i-1][j][k]里面了。
下面只需要用滚动数组优化就好了,因为dp[i][j][k]的取值仅仅依赖于dp[i-1][j][k] 和 dp[i][j-1][k-bug[i]].
AC代码:
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行的代码,要求出现的bug数目不超过b。给定每个程序员写一行代码会出现的bug数,问你有多少种方案完成这m行代码。
比赛的一道题, 当时真心没时间看了。看过后发现是 一个裸的dp + 滚动数组
思路:设dp[i][j][k]表示前i个程序员写j行代码出现k个bug的方案数。用bug[i]表示第i个程序员每写一行出现的bug数目。
则有dp[i][j][k] = dp[i-1][j][k],且当j >= 1 && k >= bug[i]时有dp[i][j][k] += dp[i][j-1][k-bug[i]]。
因为每次都把dp[i][j][k]更新到底,所以不用考虑dp[i-1][j-1][k-bug[i]]的方案数,或者说,该方案数已经被包含在dp[i-1][j][k]里面了。
下面只需要用滚动数组优化就好了,因为dp[i][j][k]的取值仅仅依赖于dp[i-1][j][k] 和 dp[i][j-1][k-bug[i]].
AC代码:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <vector> #define INF 0x3f3f3f3f3f #define eps 1e-8 #define MAXN (100000+10) #define MAXM (50000000) #define Ri(a) scanf("%d", &a) #define Rl(a) scanf("%lld", &a) #define Rf(a) scanf("%lf", &a) #define Rs(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while(a--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define MOD 1000000007 #define LL long long #define lson o<<1, l, mid #define rson o<<1|1, mid+1, r #define ll o<<1 #define rr o<<1|1 using namespace std; int dp[2][501][501]; int bug[501]; int main() { int n, m, b, mod; Ri(n); Ri(m); Ri(b); Ri(mod); for(int i = 1; i <= n; i++) Ri(bug[i]); CLR(dp, 0); dp[0][0][0] = 1; for(int i = 1; i <= n; i++) { for(int j = 0; j <= m; j++) { for(int k = 0; k <= b; k++) { dp[i%2][j][k] = dp[(i-1)%2][j][k]; if(j >= 1 && k >= bug[i]) dp[i%2][j][k] = (dp[i%2][j][k] + dp[i%2][j-1][k-bug[i]]) % mod; } } } int ans = 0; for(int i = 0; i <= b; i++) { ans += dp[n%2][m][i]; ans %= mod; } Pi(ans); return 0; }
相关文章推荐
- Android 计算器的开发
- 协议中的属性和类中属性的区别
- ACE简单实现网络聊天(文字)
- 某某面试题
- AngularJS入门实例2——控制器的使用
- MariaDB10.0.21 编译安装 脚本
- 文件上传注意事项
- IC验证平台STE_V2.1实物图和架构图
- 文本挖掘之文本聚类(DBSCAN)
- Jquery 获取 radio选中值
- C# Protobuf-Net 序列化
- [LeetCode] Different Ways to Add Parentheses
- C# Protobuf-Net 序列化
- MYSQL
- Android开发-工具:MAC下Android的Eclipse开发环境的搭建
- HDU HDU 5375 Gray code(二进制和格雷码)
- 常见WEB开发安全漏洞、原因分析及解决之道
- Python学习笔记-Day03 -第二部分(双向队列-deque和单向队列Queue)
- initWithFrame方法的理解
- 开发者账号的申请流程(个人以及公司)