NC24837. [USACO 2009 Mar B]Dairy Queen
描述
输入描述
* Line 1: Two space-separated integers: N and C
* Lines 2..C+1: Line i+1 contains a single integer:
输出描述
* Line 1: A single line with a single integer that is the number of ways to create N cents of change using the supplied coins. The answer is guaranteed to fit into a signed 32-bit integer.
示例1
输入:
83 5 50 25 10 5 1
输出:
159
说明:
Here are 15 of the 159 ways of making 83 cents:C++14(g++5.4) 解法, 执行用时: 3ms, 内存消耗: 500K, 提交时间: 2019-09-09 21:18:21
#include <bits/stdc++.h> using namespace std; int main() { int n,c,cc; int a[305]; cin>>n>>c; memset(a,0,sizeof(a)); for(int i=1;i<=c;i++) { cin>>cc; a[cc]++; for(int j=cc+1;j<=n;j++) a[j]=a[j]+a[j-cc]; } cout<<a[n]<<endl; }
C++11(clang++ 3.9) 解法, 执行用时: 3ms, 内存消耗: 592K, 提交时间: 2020-02-26 18:23:12
#include<bits/stdc++.h> using namespace std; int c,n,ans,dp[200001]; int main() { cin>>ans>>n; for(int i=1;i<=n;i++) { cin>>c; dp[c]++; for(int j=c+1;j<=ans;j++) dp[j]+=dp[j-c]; } cout<<dp[ans]; }