[POJ1742]Coins

本文探讨了一种典型的支付组合问题,即如何利用不同面额的硬币支付从1到m之间的任意金额。通过动态规划的方法,文章详细介绍了如何计算出在给定的硬币种类和数量下,能够恰好支付的目标金额数目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Time Limit: 3000MS
Memory Limit: 30000K

Description

People in Silverland use coins.They have coins of value A1,A2,A3…An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn’t know the exact price of the watch.
You are to write a program which reads n,m,A1,A2,A3…An and C1,C2,C3…Cn corresponding to the number of Tony’s coins of value A1,A2,A3…An then calculate how many prices(form 1 to m) Tony can pay use these coins.

Input

The input contains several test cases. The first line of each test case contains two integers n(1<=n<=100),m(m<=100000) n ( 1 <= n <= 100 ) , m ( m <= 100000 ) .The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn(1<=Ai<=100000,1<=Ci<=1000) A 1 , A 2 , A 3... A n , C 1 , C 2 , C 3... C n ( 1 <= A i <= 100000 , 1 <= C i <= 1000 ) . The last test case is followed by two zeros.

Output
For each test case output the answer on a single line.

Sample Input

3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0

Sample Output

8
4

题意:
n n 种纸币,第i种面值是 Ai A i ,拥有的数量是 Ci C i ,问能配出 1m 1 到 m 的中的几种面值。
第一行输入两个个数 nm n , m
第二行输入2n个数,前n个是 A1...An A 1... A n ,后n个是 C1...Cn C 1... C n

题解:
楼教主男人八题之一….
f[j] f [ j ] 表示面值 j j 是否能取到。
g[i][j]表示用第 i i 种纸币取到j的面值至少需要多少张
则转移方程转化为

if(!f[j] && f[j-a[i]] && g[i][j-a[i]] < c[i]){
    f[j]=1;
    g[i][j]=g[i][j-a[i]]+1;
}

g数组可以压掉 i i <script type="math/tex" id="MathJax-Element-16">i</script>的那一维,每次做完记得要清零

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#define LiangJiaJun main
using namespace std;
int n,m,a[104],c[104];
int f[100004],g[100004];
int w33ha(){
    memset(f,0,sizeof(f));
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)scanf("%d",&c[i]);
    f[0]=1;
    int ans=0;
    for(int i=1;i<=n;i++){
        memset(g,0,sizeof(g));
        for(int j=a[i];j<=m;j++){
            if(!f[j]&&f[j-a[i]]&&g[j-a[i]]<c[i]){
                f[j]=1;
                g[j]=g[j-a[i]]+1;
            }
        }
    }
    for(int i=1;i<=m;i++)ans+=f[i];
    printf("%d\n",ans);
    return 0;
}

int LiangJiaJun(){
    while(scanf("%d%d",&n,&m)!=EOF){
        if(n==0&&m==0)break;
        w33ha();
    }
    return 0;
}
### POJ 1182 解法分析 POJ 1182 是一道经典的动态规划问题,其核心在于如何通过状态转移方程高效计算组合数。以下是对此问题的详细解析。 #### 动态规划模型构建 该问题可以被建模为一个多维背包问题的一种变体。目标是从有限种类的硬币中选取若干枚,使其总价值等于指定金额 \( n \),同时所选硬币的数量不得超过 100 枚。设 \( dp[i][j] \) 表示使用前 \( i \) 种硬币凑成总额 \( j \) 的方法数目,则可以通过如下递推关系定义: \[ dp[i][j] = dp[i-1][j] + dp[i][j-\text{coin}[i]] \] 其中: - \( dp[i-1][j] \) 表示不选用第 \( i \) 种硬币的情况; - \( dp[i][j-\text{coin}[i]] \) 表示至少选用一枚第 \( i \) 种硬币的情况; 为了进一步优化空间复杂度,可采用一维数组实现上述逻辑[^3]。 #### 边界条件设定 初始化时需注意以下两点: 1. 当总额为零 (\( j=0 \)) 时,无论考虑多少种硬币,均只有一种方案——即什么都不选。 2. 若当前硬币面额大于所需总额,则跳过此硬币的选择过程。 最终答案存储于变量 \( dp[\text{num\_coins}][n*100] \),表示利用全部可用硬币恰好构成目标金额的方法总数。 #### 实现代码示例 下面提供了一段基于 Python 的解决方案作为参考: ```python def coin_combinations(n): coins = [1, 5, 10, 25, 50] max_coins = 100 target_cents = n * 100 # Initialize DP table with zeros dp = [[0]*(target_cents+1) for _ in range(len(coins)+1)] # Base case initialization for i in range(len(coins)+1): dp[i][0] = 1 for i in range(1, len(coins)+1): for j in range(1, target_cents+1): if j >= coins[i-1]: use_current_coin = (max_coins - j//coins[i-1]) >= 0 and dp[i][j-coins[i-1]] not_use_current_coin = dp[i-1][j] dp[i][j] = (use_current_coin or not_use_current_coin) else: dp[i][j] = dp[i-1][j] return dp[-1][-1] print(coin_combinations(4)) ``` 以上程序实现了对输入参数 \( n \) 所对应的不同组合方式进行计数的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值