codefroces 543A Writing Code dp优化 完全背包

7 篇文章 0 订阅

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.

Example
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

思路:
首先考虑最直接的三维方程
dp(i, j, k) 表示前i个程序员写了j行代码bug为k个
dp(i,j,k)=∑dp(i−1,j−r,k−r∗a[i]),第i个程序员写了r行代码
我们可以换种视角优化这个方程
1)第i个程序员没写代码
2)第i个程序员写了至少一行
dp(i,j,k)=dp(i−1,j,k)+dp(i,j−1,k−a[i])
这个方程在时间复杂度上已经是O(n^3)了
如果我们在储存的时候省掉第一维
那么在枚举到 (i, j, k)这个状态的时候实际储存的是 (i-1, j, k)

这是三维的 三维的要初始化 第i个程序员 什么都没做 文章也没开始的情况为1,然后顺着推过去。

#include <bits/stdc++.h>
using namespace std;
int dp[2][520][520];
int a[520];
int main()
{
   int n,m,k,mod;
   cin>>n>>m>>k>>mod;
   for(int i=1;i<=n;i++)
   {
   	scanf("%d",&a[i]);
   }	
   for(int i=1;i<=n;i++)
   	dp[i&1][0][0]=1; //因为是三维,也就是把所有员工情况
   //分开保存了,所以要把每个员工的初始情况记录下来
   for(int i=1;i<=n;i++)
   {
   	for(int j=1;j<=m;j++)
   	{
   		for(int kk=0;kk<=k;kk++)
   		{
   			if(a[i]>kk)
   				 dp[i&1][j][kk]=dp[(i-1)&1][j][kk]%mod;
   			else 
   			{	
   			dp[i&1][j][kk]=dp[(i-1)&1][j][kk]+dp[i&1][j-1][kk-a[i]];//重点 由于 第j个程序可以写很多行,所以采用了多重背包
   			dp[i&1][j][kk]%=mod;
   		    }
   		}
   	}
   }
   int res=0;
   for(int i=0;i<=k;i++)
   {
   	res+=dp[n&1][m][i];
   	res%=mod;
   }
   res%=mod;
   printf("%d\n",res );

}

化简成 二维 由于 dp(i,j,k)=dp(i−1,j,k)+dp(i,j−1,k−a[i])
dp(i−1,j,k)和dp(i,j−1,k−a[i]) 情况不冲突,用完全背包从前往后不会有冲突。所以可以滚动成二维。

dp[i][j][k]前i个程序员写了j行产生k个BUG的方案数。
由于是完全背包所以可以减掉一维。
最后更新 n个人 m行 每一行都是一个0 1.

#include <bits/stdc++.h>
using namespace std;

int dp[1010][1010];
int bug[1010];
int main()
{
	int n,m,kk,mod;
	scanf("%d%d%d%d",&n,&m,&kk,&mod);
	for(int i=1;i<=n;i++)
		scanf("%d",&bug[i]);
	dp[0][0]=1;//前i个人做了j项工作产生k个BUG  
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
		for(int k=kk;k>=bug[i];k--)
		{
			dp[j][k]+=dp[j-1][k-bug[i]];
			dp[j][k]%=mod;
		}
	    }
	}
	long long res=0;
	for(int i=0;i<=kk;i++)
	{
		res+=dp[m][i];
		res%=mod;
	}
	printf("%lld\n",res );
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值