Pay the Price+uva+dp

Pay the Price

Input: standard input

Output: standard output

Time Limit: 2 seconds

Memory Limit: 32 MB

 

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+41+2+3and 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 12 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 NL1 and L2 as input, then you should output in how many ways N dollars can be paid using L1L1+1 …, L2coins (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

解决方案:看了别人的博客才知道,原来有一个规律:用最大面值为j硬币构成i与用小于或等于j个硬币构成i是等价的。这样就得dp[i][j]=dp[i-j][j]+dp[i][j-1],然后可先预处理出所有,然后输出就可以了。

code:

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long dp[400][400];
char input[1000];
int main()
{
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    for(int i=0; i<=300; i++)
    {
        for(int j=1; j<=300; j++)
        {
            if(j-1>=0)
                dp[i][j]+=dp[i][j-1];
            if(i-j>=0)
                dp[i][j]+=dp[i-j][j];
        }
    }
    while(gets(input))
    {
        int len=strlen(input);
        int num[5],u=0;
        memset(num,0,sizeof(num));
        for(int i=0; i<len; i++)
        {
            if(input[i]!=' ')
            {
                num[u]*=10;
                num[u]+=int(input[i]-'0');
            }
            else if(input[i]==' '&&input[i+1]!=' ')
            {
                u++;
            }
        }
        u++;
        int total=num[0];
        num[1]=num[1]>300?300:num[1];
        num[2]=num[2]>300?300:num[2];
        if(u==1)
        {
            printf("%lld\n",dp[total][total]);
        }
        else if(u==2)
        {
            printf("%lld\n",dp[total][num[1]]);
        }
        else
        {
            printf("%lld\n",dp[total][num[2]]-dp[total][num[1]-1]);
        }

    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值