题目描述
给定 V 种不同面值的货币(单位:元),每种货币使用的次数不限。
现在,要你用这 V 种货币凑出 N 元钱,请问共有多少种不同的凑法。
输入格式
第一行包含两个整数 V 和 N。
接下来的若干行,将一共输出 V 个整数,每个整数表示一种货币的面值。
输出格式
输出一个整数,表示所求总方案数。
数据范围
1≤V≤25,
1≤N≤10000
样例
输入样例:
3 10
1 2 5
输出样例:
10
算法1
(完全背包)
C++ 代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N=10010;
typedef long long ll;
int n,m;
ll a[N],f[N];
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
f[0]=1;
for(int i=1;i<=n;i++)
{
for(int j=a[i];j<=m;j++)
f[j]+=f[j-a[i]];
}
cout<<f[m]<<endl;
return 0;
}