UVA10313 Pay the Price【完全背包+Coin Change+DP】

In ancient days there was a country whose people had very interesting habits. Some of them were lazy, some were very rich, some were very poor and some were miser. Obviously, some of the rich were miser (A poor was never miser as he had little to spend) and lazy but the poor were lazy as well (As the poor were lazy they remained poor forever). The following things were true for that country
a) As the rich were miser, no things price was more than 300 dollars (Yes! their currency was dollar).
b) As all people were lazy, the price of everything was integer (There were no cents and so beggars
always earned at least one dollar)
c) The values of the coins were from 1 to 300 dollars, so that the rich (who were idle) could pay any
price with a single coin.
    Your job is to find out in how many ways one could pay a certain price using a limited number of coins (Note that the number of coins paid is limited but not the value or source. I mean there was infinite number of coins of all values). For example, by using three coins one can pay six dollars in 3 ways, 1+1+4, 1+2+3, and 2+2+2. Similarly, one can pay 6 dollars using 6 coins or less in 11 ways.
Input
The input file contains several lines of input. Each line of input may contain 1, 2 or 3 integers. The first integer is always N (0 ≤ N ≤ 300), the dollar amount to be paid. All other integers are less than 1001 and non-negative.
Output
For each line of input you should output a single integer.
    When there is only one integer N as input, you should output in how many ways N dollars can be paid.
    When there are two integers N and L1 as input, then you should output in how many ways N dollars can be paid using L1 or less coins.
    When there are three integers N, L1 and L2 as input, then you should output in how many ways N dollars can be paid using L1, L1 + 1, . . . , L2 coins (summing all together). Remember that L1 is not greater than L2.
Sample Input
6
6 3
6 2 5
6 1 6
Sample Output
11
7
9
11

问题链接UVA10313 Pay the Price
问题简述:有N种硬币面值是1到300。输入有3种情况,先输入n表示要目标是价值n,如果输入只有n,则求组成n有几种可能?若n后跟1个整数a,则求用不多于a的硬币数组成n有几种可能?若n后跟2个数字a和b,则求使用数量a到b的硬币组成n有几种可能?
问题分析
    有关Coin Change的完全背包问题,给出2种解法。
    前一种解法中,d[i][j]表示用不超过j面值的硬币构成面值为i。当选择了j面值的硬币构造i的方案数为dp[i - j][j],不选择的话为dp[i][j - 1],所以状态转换方程是dp[i][j] = dp[i - j][j] + dp[i][j - 1]。
    后一种解法中,dp[i][j] 表示价值为i用j个硬币组成几种可能,dp[j][k] += dp[j-i][k-1] 意思是多加一枚价值为i的硬币,加上价值为j-i用k-1个硬币的总数。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10313 Pay the Price */

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int N = 300;
LL dp[N + 2][N + 2];

void dodp()
{
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    for(int i = 0; i <= N; i++)
        for(int j = 1; j <= N; j++) {
            if(i >= j) dp[i][j] = dp[i - j][j] + dp[i][j - 1];
            else dp[i][j] = dp[i][j - 1];
        }
}

int main()
{
    dodp();

    int n, a, b;
    char s[N];
    while(gets(s)) {
        int cnt = sscanf(s, "%d%d%d", &n, &a, &b);
        a = min(a, N);
        b = min(b, N);

        LL ans;
        if(cnt == 1) ans = dp[n][n];
        else if(cnt == 2) ans = dp[n][a];
        else if(cnt == 3) ans = dp[n][b] - dp[n][a - 1];

        printf("%lld\n", ans);
    }

    return 0;
}

AC的C++语言程序如下:

/* UVA10313 Pay the Price */

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int N = 300;
LL dp[N + 1][N + 1];
char s[256];

void dodp()
{
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    for(int i = 1; i <= N; i++)
        for(int j = i; j <= N; j++)
            for(int k = 1; k <= N; k++)
                dp[j][k] += dp[j - i][k - 1];
}

int main()
{
    dodp();

    int n, a, b;
    while(gets(s) != NULL) {
        int cnt = sscanf(s, "%d%d%d", &n, &a, &b);
        a = min(a, N);
        b = min(b, N);

        LL ans = 0;
        int start, end;
        if(cnt == 1) start = 0, end = n;
        else if(cnt == 2) start = 0, end = a;
        else if(cnt == 3) start = a, end = b;
        for(int i = start; i <= end; i++)
            ans += dp[n][i];

        printf("%lld\n", ans);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值