推导过程:
f(i, j)表示使用前i个数组成和为j的方案数
f(i, j) = f(i - 1, j) + f(i - 1, j - i) + f(i - 1, j - 2 * i) + ,..., + f(i - 1, j - k * i);
f(i, j - i) = f(i - 1, j - i) + f(i - 1, j - 2 * i) + ,..., + f(i - 1, j - k * i);
f(i, j) = f(i - 1, j) + f(i, j - i);
f(j) = f(j) + f(j - i);
C++代码:
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int mod = 2147483648;
int main()
{
int n;
cin >> n;
long long f[n + 1];
memset(f, 0, sizeof f);
f[0] = 1;
for (int i = 1; i < n; i++)
for (int j = i; j <= n; j++)
f[j] = (f[j] + f[j - i]) % mod;
cout << f[n] << endl;
return 0;
}