当前位置: 首页 > news >正文

Coin Combinations I(Dynamic Programming)

题目描述

Consider a money system consisting of n coins. Each coin has a positive integer value. Your task is to calculate the number of distinct ways you can produce a money sum x using the available coins.
For example, if the coins are {2,3,5} and the desired sum is 9, there are 8 ways:

2+2+5
2+5+2
5+2+2
3+3+3
2+2+2+3
2+2+3+2
2+3+2+2
3+2+2+2

输入

The first input line has two integers n and x: the number of coins and the desired sum of money.
The second line has n distinct integers c1,c2,...,cn: the value of each coin.

Constraints

1 ≤ n ≤ 100
1 ≤ x ≤ 10^6
1 ≤ ci ≤ 10^6

输出

Print one integer: the number of ways modulo 10^9+7.

样例输入
3 9
2 3 5
样例输出
8

思路分析

动态规划。

从1到x,找出所有金额的硬币组合方法。

初始化dp[0]=1,便于后期i=j的情况下直接添加一种方法。

本题样例解析:

i=1时,没有小于等于1的硬币金额,dp[1]=0.

i=2时,j=2,dp[2]+=dp[2-2],dp[2]=1.

i=3时,j=2,dp[3]+=dp[3-2],dp[3]=0; j=3,dp[3]+=dp[3-3],dp[3]=1.

i=4时,j=2,dp[4]+=dp[4-2],dp[4]=1; j=3,dp[4]+=dp[4-3],dp[4]=1.

i=5时,j=2,dp[5]+=dp[5-2],dp[5]=1; j=3,dp[5]+=dp[5-3],dp[5]=2;

         j=5,dp[5]+=dp[5-5],dp[5]=3.

i=6时,j=2,dp[6]+=dp[6-2],dp[6]=1; j=3,dp[6]+=dp[6-3],dp[6]=2;

         j=5,dp[6]+=dp[6-5],dp[6]=2.

i=7时,j=2,dp[7]+=dp[7-2],dp[7]=2; j=3,dp[7]+=dp[7-3],dp[7]=4;

         j=5,dp[7]+=dp[7-5],dp[7]=5.

i=8时,j=2,dp[8]+=dp[8-2],dp[8]=2; j=3,dp[8]+=dp[8-3],dp[8]=5;

         j=5,dp[8]+=dp[8-5],dp[8]=6.

i=9时,j=2,dp[9]+=dp[9-2],dp[9]=5; j=3,dp[9]+=dp[9-3],dp[9]=7;

         j=5,dp[9]+=dp[9-5],dp[9]=8.

代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=1e6+1;
const ll mod=1e9+7;
ll n,x;
int main(){ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);cin>>n>>x;vector<ll>a(n,0);for(ll i=0;i<n;i++){cin>>a[i];}sort(a.begin(),a.end());vector<ll>dp(N,0);dp[0]=1;for(ll i=1;i<=x;i++){for(ll&j:a){if(j>i)break;dp[i]+=dp[i-j];dp[i]%=mod;}}cout<<dp[x];return 0;
}

http://www.lryc.cn/news/609333.html

相关文章:

  • Docker环境离线安装指南
  • 解剖 .NET 经典:从 Component 到 BackgroundWorker
  • node.js常用函数
  • GaussDB case when的用法
  • SpringBoot AI自动化测试实战案例
  • GitCode疑难问题诊疗
  • Linux命令基础(下)
  • 1.内核模块
  • 14.Redis 哨兵 Sentinel
  • 2. 字符设备驱动
  • IO流-对象流
  • 克罗均线策略思路
  • `npm error code CERT_HAS_EXPIRED‘ 问题
  • Java Stream API 编程实战
  • 2025年渗透测试面试题总结-2025年HW(护网面试) 77-1(题目+回答)
  • 《测试驱动的React开发:从单元验证到集成协同的深度实践》
  • 【2025ICCV-目标检测方向】WaveMamba:用于 RGB-红外目标检测的小波驱动曼巴融合
  • 百度招黑产溯源安全工程师
  • SQL注入SQLi-LABS 靶场less31-38详细通关攻略
  • Maxscript在选择的可编辑多边形每个面上绘制一个内部圆形
  • 【高等数学】第七章 微分方程——第十节 常系数线性微分方程组解法举例
  • [硬件电路-140]:模拟电路 - 信号处理电路 - 锁定放大器概述、工作原理、常见芯片、管脚定义
  • 类与对象(中),咕咕咕
  • Zama的使命
  • 零确认双花攻击
  • 8月3日星期日今日早报简报微语报早读
  • 《从原理到实践:MySQL索引优化与SQL性能调优全解析》
  • 【Redis学习路|第一篇】初步认识Redis
  • C的运算符与表达式
  • BP神经网络:当线性模型已到尽头,如何用“人造大脑”挖掘非线性预测规律?