D. Red-Green Towers Dp
题意:有两种颜色的积木 向上垒,每层只能是同一种颜色,且每层的个数等于层数。问有多少种垒法。
Dp滚动数组搞下就好了。
#include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <set> #include <queue> #include <stack> #include <math.h> using namespace std; typedef long long LL; const int mod = 1000000000 + 7; const int maxn = 2 * 100000 + 2; int dp[10][maxn]; int main() { int r, g; cin >> r >> g; int h = (int)sqrt((r + g) * 2); dp[0][0] = 1; for (int i = 1; i <= h; i++){ for (int j = 0; j <= r; j++){ int ans1 = 0; int ans2 = 0;int gg = i % 2; if (j >= i) ans1 = dp[gg ^ 1][j - i]; int g1 = (i*(i - 1) / 2 - j); g1 = g - g1; if (g1 >= i) ans2 = dp[gg ^ 1][j]; ans1 %= mod; ans2 %= mod; dp[i % 2][j] = ans1 + ans2; dp[i % 2][j] %= mod; } } int flag = 0; int sum = 0; for (int i = h; i >= 1; i--){ for (int j = 0; j <= r; j++){ if (dp[i % 2][j]) flag = 1; sum += dp[i % 2][j]; sum %= mod; } if (flag) break; } printf("%d\n", sum); return 0; }