Pay the Price

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 310
long 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解决这个问题King Julien rules the Madagascar island whose primary crop is coconuts. If the price of coconuts is P , then King Julien’s subjects will demand D(P ) = 1200 − 100P coconuts per week for their own use. The number of coconuts that will be supplied per week by the island’s coconut growers is S(p) = 100P. (a) (2 pts) Calculate the equilibrium price and quantity for coconuts. (b) (2 pts) One day, King Julien decided to tax his subjects in order to collect coconuts for the Royal Larder. The king required that every subject who consumed a coconut would have to pay a coconut to the king as a tax. Thus, if a subject wanted 5 coconuts for himself, he would have to purchase 10 coconuts and give 5 to the king. When the price that is received by the sellers is pS, how much does it cost one of the king’s subjects to get an extra coconut for himself? (c) (3 pts) When the price paid to suppliers is pS, how many coconuts will the king’s subjects demand for their own consumption (as a function of pS)? 2 (d) (2 pts) Under the above coconut tax policy, determine the total number of coconuts demanded per week by King Julien and his subjects as a function of pS. (e) (3 pts) Calculate the equilibrium value of pS, the equilibrium total number of coconuts produced, and the equilibrium total number of coconuts consumed by Julien’s subjects. (f) (5 pts) King Julien’s subjects resented paying the extra coconuts to the king, and whispers of revolution spread through the palace. Worried by the hostile atmosphere, the king changed the coconut tax. Now, the shopkeepers who sold the coconuts would be responsible for paying the tax. For every coconut sold to a consumer, the shopkeeper would have to pay one coconut to the king. For this new policy, calculate the number of coconuts being sold to the consumers, the value per coconuts that the shopkeepers got after paying their tax to the king, and the price payed by the consumers.
最新发布
03-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值