Link:http://acm.hdu.edu.cn/showproblem.php?pid=1284
题意:给定1 2 3 与 n,问有多少种组成n的方法。
用dp[i][j]表示前i种前兑换成j的方案数
则
dp[i][j]=sum(dp[i−1][j])+sum(dp[i][j−a[i])
即i一个都不选和i至少选一个
用滚动数组进行优化
dp[j]=dp[j]+sum(dp[j−i])
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
const int N = 4E4;
typedef long long ll;
ll dp[N]; //前i种钱兑换成j的方法 dp[i][j] -> dp[j]
int main(){
int n;
while(~scanf("%d", &n)){
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for(int i = 1; i <= 3; i++){
for(int j = i; j < N; j++){
dp[j] += dp[j - i];
}
}
printf("%lld\n", dp[n]);
}
}