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\n", ans);
}
return 0;
}

UVA 10313(完全背包变形)的更多相关文章

  1. 紫书 例题 9-5 UVa 12563 ( 01背包变形)

    总的来说就是价值为1,时间因物品而变,同时注意要刚好取到的01背包 (1)时间方面.按照题意,每首歌的时间最多为t + w - 1,这里要注意. 同时记得最后要加入时间为678的一首歌曲 (2)这里因 ...

  2. UVa 1213 (01背包变形) Sum of Different Primes

    题意: 选择K个质数使它们的和为N,求总的方案数. 分析: 虽然知道推出来了转移方程, 但还是没把代码敲出来,可能基本功还是不够吧. d(i, j)表示i个素数的和为j的方案数,则 d(i, j) = ...

  3. FZU 2214 Knapsack problem 01背包变形

    题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...

  4. Codeforces Round #214 (Div. 2) C. Dima and Salad (背包变形)

    C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. codeforce Gym 101102A Coins (01背包变形)

    01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...

  6. HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. P1282 多米诺骨牌 (背包变形问题)

    题目描述 多米诺骨牌有上下2个方块组成,每个方块中有1~6个点.现有排成行的 上方块中点数之和记为S1,下方块中点数之和记为S2,它们的差为|S1-S2|.例如在图8-1中,S1=6+1+1+1=9, ...

  8. 【01背包变形】Robberies HDU 2955

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...

  9. J-流浪西邮之寻找火石碎片 【经典背包变形】

    题目来源:2019 ACM ICPC Xi'an University of Posts & Telecommunications School Contest 链接:https://www. ...

随机推荐

  1. 09-UIKit(UICollectionViewController、UITabBarController)

    目录: 一.UICollectionViewController 二.UITabBarController(标签控制器) 三.视图和试图控制器的生命周期 四.其他控件 回到顶部 一.UICollect ...

  2. Kivy: Crossplatform Framework for NUI

    Kivy: Crossplatform Framework for NUI ivy - Open source Python library for rapid development of appl ...

  3. 为开发用途mac电脑瘦身

    本文介绍的瘦身方法仅仅针对开发用途的mac电脑,在我们的开发过程中会发现随着编译的程序过多非常导致硬盘空间的占用率不断上升. 原文地址:http://blog.csdn.net/qqmcy/artic ...

  4. Session 转台服务器的使用方法

    Session的缺陷:为了保持自身的稳定,IIS在访问量大的时候,可能会不自觉的重启,这时候Session就会丢失用户就会被迫下线 解决方案1:将Session放到一个专门的转台服务器上 方案2:将S ...

  5. 遭遇“HTTP 错误 500.19 无法访问请求的页面,因为该页的相关配置数据无效。”

    windows 2008下IIS7 安装ASP.NET 遇到如下错误: HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. ...

  6. 经典系统windows xp sp3官方原版下载(附XP序列号)

    经典系统windows xp sp3官方原版下载(附XP序列号)    在过几日,微软最新的操作系统windows 7就将像外发售了!虽然Vista操作系统已经问世多年,但XP系统仍然占据着王位,在市 ...

  7. 京JS 2013 - A two-day conference in Beijing for the JavaScript and Node.js community

    京JS 2013 - A two-day conference in Beijing for the JavaScript and Node.js community 关于技术大会 京JS 2013 ...

  8. Android ListView条目全选功能,不用checkbox实现!

    大家好,翻了翻曾经的笔记,发现了一个我特别标记的功能,那就是ListView全选功能,顿时想起了我那个时候苦逼的生涯,因为我大学机械出身,大学毕业了都不知道什么叫代码,在58干了一段销售.实在是干不下 ...

  9. gcc configure: error: Building GCC requires GMP 4.2+, MPFR 2.3.1+ and MPC 0.8.0

    从svn checkout svn://gcc.gnu.org/svn/gcc/trunk拿了GCC的最新代码,打算编译了学东西习学习C++ 11的东西,结果在configure的时候出现例如以下问题 ...

  10. BLE简介和Android BLE编程

    一.BLE和BT区别 其实我知道许多程序员不太喜欢阅读除了代码以外的文档,因为有时这些过于冗长的文档对编程并没有更多的好处,有了协议,接口,demo差不多很多人就能写出很好质量的代码了.但其实更深入的 ...