Pay the Price

来源:互联网 发布:津元天钓具淘宝 编辑:程序博客网 时间:2024/04/30 18:10

Problem F : Pay the Price

From:UVA, 10313

Submit (Out Of Contest)

Time Limit: 3000 MS

 

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 …, 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


(The Decider Contest, Problem setter: Shahriar Manzoor)

此题题意是给出数字代表总价值,如果给一个n,则表示用1~300个硬币凑出n的方法;如果给2个数n,m,表示用1~m个硬币凑成n的方法数,如果给3个数n,m,f,则表示用m~f个硬币组成的方法数。此题要用到拆分数定理,即用不超过j个硬币凑成n的方法数,与用面值不超过j的硬币凑成的方法数相同。因此,求用不超过j,凑成i的个数,对于面值j的硬币有两种可能,取或不取,类比0,1背包,的转移方程:dp[i][j]+=dp[i-j][j];dp[i][j]+=dp[i][j-1];对于S(n~m)=S(1~m)-S(1~n);

#include<iostream>#include<vector>#include<string.h>#include<sstream>using namespace std;#define maxn 310long long  dp[maxn][maxn];vector<int>ve;void fact(){     dp[0][0]=1;    for(int i=0;i<=300;i++){//注意0 n 的值应该是1,而不是0//dp[i][j]表示由1到j价值的钱币构成i的个数        for(int j=1;j<=300;j++){             if(i - j >= 0)                dp[i][j] += dp[i - j][j];            if(j - 1 >= 0)                dp[i][j] += dp[i][j - 1];        }    }}int main(){    string str;    int no;    fact();    while(getline(cin,str)){        if(!str.length()) continue;        stringstream ss(str);        while(ss>>no){            ve.push_back(no);        }        if(ve.size()==1) cout<<dp[ve[0]][ve[0]]<<endl;        else if(ve.size()==2){            if(ve[1]>=300) ve[1]=300;            //solve(ve[0],ve[1]);            cout<<dp[ve[0]][ve[1]]<<endl;        }        else{            if(ve[1]>=300) ve[1]=300;            if(ve[2]>=300) ve[2]=300;           // solve(ve[0],ve[2]);            long long sum=dp[ve[0]][ve[2]];            if(ve[1]<=1) cout<<sum<<endl;//注意ve[1]可能为0            else{                //solve(ve[0],ve[1]-1);//注意传入参数为0的情况                long long num=dp[ve[0]][ve[1]-1];                cout<<sum-num<<endl;            }        }        ve.clear();    }    return 0;}




0 0
原创粉丝点击