UVa 10313 Pay the Price:DP&整数拆分

10313 - Pay the Price

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1254

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 than300 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 from1 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 pay6 dollars using 6 coins or less in 11 ways.

Input

The input file contains several lines of input. Each line of input may contain1, 2 or 3 integers. The first integer is alwaysN (0<=N<=300), the dollar amount to be paid. All other integers are less than1001 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 waysN dollars can be paid.

When there are two integers N andL1 as input, then you should output in how many ways N dollars can be paid usingL1 or less coins.

When there are three integers N,L1 and L2 as input, then you should output in how many waysN dollars can be paid using L1, L1+1 …,L2 coins (summing all together). Remember that L1 is not greater thanL2.

Sample Input

6

6 3

6 2 5

6 1 6

Sample Output

11

7

9

11

思路:

这个题目涉及到一个结论,用不超过j个硬币凑出面值i的方案种数,是和用面值不超过j的硬币凑出面值i的方案种数是相同的。说得再数学一点,就是整数i拆分成不超过j个整数的拆分数,是和整数i拆成若干个值不超过j的整数的拆分数是相同的。具体的证明用到了Ferrers图像的性质。

这样的话我们就可以取一个二维数组f[i][j]表示用面值不超过j的硬币凑出面值i的方案的种数,那么如果我使用了面值j,对应方案种数就应该加上f[i-j][j],如果我们不使用面值j,那么对应的方案种数就应该加上f[i][j-1]。也就是说状态转移方程为f[i][j]= f[i-j][j]+ f[i][j-1]。

完整代码:

/*0.055s*/

#include<bits/stdc++.h>
using namespace std;
const int maxn = 301;  

char s[20];
long long dp[maxn][maxn];  

int main()
{
    int n, L1, L2;
    dp[0][0] = 1;
    for (int i = 0; i < maxn; ++i)
    {
        for (int j = 1; j < maxn; ++j)
        {
            if (j <= i) dp[i][j] = dp[i - j][j] + dp[i][j - 1];
            else dp[i][j] = dp[i][j - 1];
        }
    }
    while (gets(s))
    {
        L1 = L2 = -1;
        sscanf(s, "%d%d%d", &n, &L1, &L2);
        L1 = min(L1, 300), L2 = min(L2, 300);
        if (L1 == -1) printf("%lld\n", dp[n][n]);
        else if (L2 == -1) printf("%lld\n", dp[n][L1]);
        else
        {
            if (L1 < 2) printf("%lld\n", dp[n][L2]);
            else printf("%lld\n", dp[n][L2] - dp[n][L1 - 1]);
        }
    }
    return 0;
}

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索was
, lazy
, of
, The
c整数拆分
10313轴承、10313 徐州医学院、pay the bill、pay the ghost、pay to the order of,以便于您获取更多的相关知识。

时间: 2024-08-02 18:43:43

UVa 10313 Pay the Price:DP&amp;整数拆分的相关文章

UVa 357 Let Me Count The Ways:经典DP&amp;amp;硬币组合数&amp;amp;整数拆分

357 - Let Me Count The Ways Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=293 After making a purchase at a large department store, Mel's change was 17

UVa 147 Dollars:经典DP&amp;amp;硬币组合数&amp;amp;整数拆分

147 - Dollars Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=83 和UVa 357一样. 注意所有数除以5再算. 完整代码: /*0.019s*/ #include<cstdio> #include<cstring> const int coin[11

算法题:UVA 10313(完全背包变形)

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 spe

C++ 整数拆分方法详解_C 语言

一.问题背景 整数拆分,指把一个整数分解成若干个整数的和 如 3=2+1=1+1+1 共2种拆分 我们认为2+1与1+2为同一种拆分 二.定义 在整数n的拆分中,最大的拆分数为m,我们记它的方案数为 f(n,m) 即 n=x1+x2+······+xk-1+xk ,任意 x≤m 在此我们采用递归递推法 三.递推关系 1.n=1或m=1时 拆分方案仅为 n=1 或 n=1+1+1+······ f(n,m)=1 2.n=m时 S1选取m时,f(n,m)=1,即n=m S2不选取m时,f(n,m)=

UVa 10739 String to Palindrome (DP)

10739 - String to Palindrome Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1680 思路:对于每个区间[i, j]: 若str[i] == str[j],dp[i][j] = dp[i + 1][j - 1]; 若str[i]

UVa 10131 Is Bigger Smarter? (DP&amp;amp;LIS)

10131 - Is Bigger Smarter? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1072 Some people think that the bigger an elephant is, the smarter it is. To d

UVa 10651 Pebble Solitaire:DP&amp;amp;bitset

10651 - Pebble Solitaire Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1592 化为二进制进行状态转移,详见代码. 完整代码: /*0.016s*/ #include<bits/stdc++.h> using names

UVa 10404 Bachet&#039;s Game (DP&amp;amp;博弈)

10404 - Bachet's Game Time limit: 6.666 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1345 博弈~ 规则1:一个状态是必败状态当且仅当它所有后继是必败状态 规则2:一个状态是必胜状态当且仅当它至少有一个后继是必败状态 边界:这一题0是必败状态 思路

算法:uva 1407 Caves (树形背包dp)

题意 一棵n个节点的树,树的边有正整数权,表示两个节点之间的距离.你的任务是回答这样的询问:从跟 节点出发,走不超过x单位的距离, 最多能经过多少节点?同一个节点经过多次, 只能算一个. 思路 这题同样是多天前看的, 在今天才想出解法的. 动态规划就是这么有意思 :) 遍历n个节点, 有两种情 况, 第一种是遍历完之后不回到出发点, 第二种是要回到出发点. 两种都可能会重复经过某些边, 但是显 然还是第二种遍历的花费会更大 在这一题中, 遍历之后不需要回到出发点. f(i, j, 0): 表示遍