DP与朴素算法差异 【CF】Writing Code

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 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.

Examples

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个,对mod取模

每名程序员每写一行代码会有Vi个bug,论最后达成目标任务的总方案数

首先是朴素算法(假DP)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
ll dp[505][505];
int v[505];
int main()
{
    int n,m,b,mod;
    while(~scanf("%d%d%d%d",&n,&m,&b,&mod))
    {
        mem(dp,0);
        mem(v,0);
        for(int i=1;i<=n;i++){ //people
            scanf("%d",&v[i]);
        }
        dp[0][0]=1;
        for(int i=1;i<=n;i++){ //people
            for(int j=m;j>=0;j--){ //num of problem
                for(int k=b;k>=0;k--){ 
                    if(dp[j][k]==0)
                        continue;
                    for(int kk=0;;kk++){
                        if(j+kk>m)break;
                        if(k+v[i]*kk>b)break;
                        if(kk!=0)
                            dp[j+kk][k+v[i]*kk]=(dp[j+kk][k+v[i]*kk]+dp[j][k])%mod;
                        else
                            dp[j+kk][k+v[i]*kk]=(dp[j][k])%mod;
                    }
                }
            }
        }
        ll ans=0;
        for(int i=0;i<=b;i++){
            ans=(ans+dp[m][i])%mod;
        }
        printf("%I64d\n",ans);
    }
}

dp【i】【j】中的i表示当前行数,j表示当前bug数

很明显这是朴素算法,之后是正宗DP,也就是该题的完全背包

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
ll dp[505][505];
int v[505];
int main()
{
    int n,m,b,mod;
    while(~scanf("%d%d%d%d",&n,&m,&b,&mod))
    {
        mem(dp,0);
        mem(v,0);
        for(int i=1;i<=n;i++){ //people
            scanf("%d",&v[i]);
        }
        dp[0][0]=1;
        for(int i=1;i<=n;i++){ //people
            for(int j=1;j<=m;j++){ //num of problem
                for(int k=v[i];k<=b;k++){ //bug
                     dp[j][k]=(dp[j-1][k-v[i]]+dp[j][k])%mod;
                    }
                }
            
        }
        ll ans=0;
        for(int i=0;i<=b;i++){
            ans=(ans+dp[m][i])%mod;
        }
        printf("%I64d\n",ans);
    }
}

DP中优化到了三层for,其中DP是正遍历,而朴素是逆遍历(因为这样可以防止重复计算,以防止第二次计算时是对自身计算过的结果进行二次计算)

朴素的思想,就是暴力

DO的思想,是把当前的行数+1,之后他是否写不写下一行,由下一层for计算

就是每一层就是计算行数+1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值