http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1201
一样的思路
http://www.cnblogs.com/liuweimingcprogram/p/6072869.html
由于其是先算dp[1][1--n]的,故需要用ans数组存
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #define IOS ios::sync_with_stdio(false) using namespace std; #define inf (0x3f3f3f3f) typedef long long int LL; #include <iostream> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <string> #include <time.h> const int MOD = 1e9 + 7; const int maxn = 50000 + 20; int dp[2][maxn]; const int N = maxn - 20; int ans[maxn]; void work() { dp[0][0] = 1; int now = 0; int all = 0; for (int i = 1; i <= N; ++i) { if (i * (i + 1) / 2 > N) break; for (int j = i * (i + 1) / 2; j <= N; ++j) { dp[now][j] = (dp[now][j - i] + dp[!now][j - i]) % MOD; ans[j] += dp[now][j]; ans[j] %= MOD; all++; } memset(dp[!now], 0, sizeof dp[!now]); // all += maxn; now = !now; } // cout << all << endl; //// cout << ans[N] << endl; // for (int i = 1; i <= 20; ++i) { // cout << ans[i] << endl; // } int n; scanf("%d", &n); printf("%d ", ans[n]); } int main() { #ifdef local freopen("data.txt","r",stdin); #endif // LL t = time(NULL); // cout << t << endl; work(); // LL tt = time(NULL); // cout << tt << endl; return 0; }