ACM--DP--Cut Ribbon--完全背包


题目地址:传送门




C - Cut Ribbon
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit  Status

Description

Polycarpus has a ribbon, its length is n. He wants to cut the ribbon in a way that fulfils the following two conditions:

  • After the cutting each ribbon piece should have length ab or c.
  • After the cutting the number of ribbon pieces should be maximum.

Help Polycarpus and find the number of ribbon pieces after the required cutting.

Input

The first line contains four space-separated integers nab and c (1 ≤ n, a, b, c ≤ 4000) — the length of the original ribbon and the acceptable lengths of the ribbon pieces after the cutting, correspondingly. The numbers ab and c can coincide.

Output

Print a single number — the maximum possible number of ribbon pieces. It is guaranteed that at least one correct ribbon cutting exists.

Sample Input

Input
5 5 3 2
Output
2
Input
7 5 5 2
Output
2

Hint

In the first example Polycarpus can cut the ribbon in such way: the first piece has length 2, the second piece has length 3.

In the second example Polycarpus can cut the ribbon in such way: the first piece has length 5, the second piece has length 2




题解:我们可以把n看成背包容积,把a,b,c看成不同的花费。则状态转移方程式为dp[i] = max(dp[i],dp[i-x]+1)(i-x>=0)表示当长度为i的时候考虑放x的性价比是多少。所以,我们可以用一个for就可以解决该问题了。


#include
#include
#include
#include
#include
using namespace std;
int main(){
    int n,a[3],dp[4010];
    scanf("%d%d%d%d",&n,&a[0],&a[1],&a[2]);
    memset(dp, -40, sizeof(dp));
    dp[0]=0;
    for(int i=0;i<3;i++){
        for(int j=a[i];j<=n;j++){
           dp[j]=max(dp[j],dp[j-a[i]]+1);
        }
    }
    printf("%d\n",dp[n]);

}







你可能感兴趣的:(ACM算法,ACM刷题录,ACM,DP,Cut,Ribbon,完全背包)