UVa 10313 Pay the price:dp& integer split

Source: Internet
Author: User

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 is a country whose people had very interesting habits. Some of them were lazy, Some were-very-rich, Some-were very poor and Some were. Obviously, some of the rich were miser (A poor is never miser as he had little to spend) and lazy but the poor were As OK (as the poor were lazy they remained poor). The following things were true for that country

A) as the rich were miser, no things price is more than300 dollars (yes! their currency is dollar).

(b) As all people were lazy, the price of Everything is integer (There were no cents and so beggars always earned at least One dollar)

c) The values of the coins were from1 to-dollars, so that's the rich (who were idle) could pay any and a single C Oin.

Your job is to find out in how many ways one could pay a certain price using a limited number of coins Er of coins paid is limited but not the value or source. I mean there is 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 one ways.

Input

The input file contains several lines of input. Each line of the input may contain1, 2 or 3 integers. The 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 in should output a single integer.

When there are only one integer N as input, your should output in how to many waysn dollars can be paid.

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

When there are three integers n,l1 and L2 as input, then to should output in how many waysn the can be dollars using paid, L1+1 ..., L2 coins (summing all together). Remember that L1 are not greater thanL2.

Sample Input

6

6 3

6 2 5

6 1 6

Sample Output

11

7

9

11

Ideas:

This topic involves a conclusion that the number of schemes with no more than J-coins to produce the par I is the same as the number of schemes with a coin with a nominal value of not more than J. Mathematically, the split fraction of the integer I split into no more than J integers is the same as the split fraction of integers that are split into integers that are not more than J. The concrete proof uses the character of the Ferrers image.

In this way, we can take a two-dimensional array f[i][j] to represent the number of schemes with a nominal value of no more than J, so if I use the face value J, the corresponding solution species number should add f[i-j][j], if we do not use the face value J, then the corresponding scheme number should add f[i][ J-1]. In other words, the state transition equation is f[i][j]= f[i-j][j]+ f[i][j-1].

Complete code:

/*0.055s*/
    
#include <bits/stdc++.h>  
using namespace std;  
const int MAXN =;  
    
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,), L2 = min (L2,);  
        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;  
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.