zoukankan      html  css  js  c++  java
  • UVA 10313(完全背包变形)

    Problem B

    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

    题意:现在有1到300的硬币,有3种情况。首先输入n,表示要凑成的价值为n,然后如果后面没数字,求组成n的方式几种,如果后跟1个数字a,求用不多于a的硬币数组成n有几种,如果后跟2个数字a,b,求使用数量a到b的硬币组成方式有几种。

    思路:凑硬币的变形,完全背包。dp数组多开一维用来表示用的硬币数。状态转移方程为:

    dp[j][k] += dp[j - i][k - 1]。

    代码:

    #include <stdio.h>
    #include <string.h>
    
    int n, a, b, i, j, k;
    long long dp[305][305], ans;
    char sb[20];
    
    int min(int a, int b) {
    	return a < b ? a : b;
    }
    int main() {
    	dp[0][0] = 1;
    
    	for (i = 1; i <= 300; i ++) {
    		for (j = i; j <= 300; j ++) {
    				for (k = 1; k <= 300; k ++) {
    				dp[j][k] += dp[j - i][k - 1];
    			}
    		}
    	}
    	while (gets(sb) != NULL) {
    		a = b = -1; ans = 0;
    		sscanf(sb, "%d%d%d", &n, &a, &b);
    		a = min(a, 300); b = min(b, 300);
    		if (a == -1) {
    			for (i = 0; i <= n; i ++) {
    				ans += dp[n][i];
    			}
    		}
    		else if (b == -1) {
    			for (i = 0; i <= a; i ++) {
    				ans += dp[n][i];
    			}
    		}
    		else {
    			for (i = a; i <= b; i ++) {
    				ans += dp[n][i];
    			}
    		}
    		printf("%lld
    ", ans);
    	}
    	return 0;
    }


  • 相关阅读:
    sudo 之后 unable to resolve host的问题解决办法
    Linux 查找具体的文件名称
    linux 访问远程务器代码
    spark 安装配置
    R基本介绍
    BIEE多层表头报表的制作方法
    支付宝新漏洞引发恐慌,那如何关闭小额免密支付呢
    大家注意了,支付宝被曝重大安全漏洞,回应称正在跟进排查
    2017年5个不应该被忽视的机器学习项目
    婚前最后一次加班
  • 原文地址:https://www.cnblogs.com/riskyer/p/3312951.html
Copyright © 2011-2022 走看看