DP算法:
#include <iostream> #include <cstdio> using namespace std; int main() { int m,n; scanf("%d%d",&m,&n); int dp[305]={0}; int i,j; int A[10]; for (i=n;i>=1;i--) scanf("%d",&A[i]); //dp dp[0]=1; for (i=1;i<=n;i++) for (j=1;j<=m;j++) { if (j>=A[i]) dp[j]+=dp[j-A[i]]; } printf("%d\n",dp[m]); return 0; }
#include <iostream> #include <cstdio> using namespace std; int ans=0; int A[10]; int m,n; void dfs(int total,int pos) { if (pos>n-1) return; while (1) { if (total==m) { ans++; break; } dfs(total,pos+1); if (total+A[pos]<=m) total+=A[pos]; else break; } } int main() { scanf("%d%d",&m,&n); int i; for (i=0;i<=n-1;i++) scanf("%d",&A[i]); dfs(0,0); printf("%d\n",ans); return 0; }