problem Link : 

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+4, 1+2+3, and 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 1, 2 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 N, L1 and L2 as input, then you should output in how many ways N dollars can be paid using L1, L1 + 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

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include <bits/stdc++.h>
using namespace std;
int main()
    // dp[v][n] = num ways to form v using n coins
    vector < vector<long long>> dp(301,vector < long long>(301,0));
    dp[0][0] = 1;
    // for each coin value (smallest coin first)
    for(int i=1;i < =300;i++)
        // for each usage of the coin
        for(int j=1;j < =300;j++)
            // for each possible value
            for(int k=300;k>=i;k--)
                dp[k][j] += dp[k-i][j-1];

    string in;
    int v,left,right;
        istringstream iss(in);
        vector<int> input;
        while(iss >> v) input.push_back(v);
        if(input.size() == 1){
            left = 0, right = 300;
        }else if(input.size() == 2){
            left = 0, right = input[1];
        } else {
            left = input[1], right = input[2];
        long long sum =0;
        right = min(300,right);
        for(int i=left;i<=right;i++) sum += dp[input[0]][i];
Copy The Code & Try With Live Editor


6 3
6 2 5
6 1 6




UVA Online Judge solution 10306  - 10313 - Pay the Price - UVA Online Judge solution in C,C++,java

UVA Online Judge solution 10306 - e - Coins - UVA Online Judge solution in C,C++,java
UVA Online Judge solution - 10325-The Lottery - UVA Online Judge solution in C,C++,java