Codeforces Round #273 (Div. 2) D. Red-Green Towers DP

链接:

http://codeforces.com/problemset/problem/478/D

题意:

给出r个红砖,g个绿砖,问有多少种方法搭成最高的塔。

题解:

举红色球的层数,当第i层为红色是,i层上面有[0,r]个 红色的,可推出dp[i+j]=dp[i+j]+dp[j],最后

再统计红色的个数就行了,红色最少为max(h*(h+1)/2-g,0)。

代码:

31 int dp[MAXN];
32 
33 int main() {
34     ios::sync_with_stdio(false), cin.tie(0);
35     int r, g;
36     cin >> r >> g;
37     int s = r + g;
38     int h = sqrt(s * 2);
39     while (h*(h + 1) / 2 > s) h--;
40     dp[0] = 1;
41     rep(i, 1, h + 1) per(j, 0, r + 1)
42         dp[i + j] = (dp[i + j] + dp[j]) % MOD;
43     int ans = 0;
44     rep(i, max(h*(h + 1) / 2 - g, 0), r + 1) ans = (ans + dp[i]) % MOD;
45     cout << ans << endl;
46     return 0;
47 }
原文地址:https://www.cnblogs.com/baocong/p/7345278.html