ural1017 Staircases (动态规划)

设dp[i][j]表示使用i个砖块,最后一个台阶的方块数量为j的不同方案数(先不考虑条件Every staircase consists of at least two steps)

那么状态转移方程即为dp[i][j] = ∑dp[i-j][k]  (0 <= k <= min{i - j, j - 1})

最后考虑每个楼梯都必须包含至少两层,这时只需要最终答案减1便可

#include 
using namespace std;

typedef long long LL;

const int N = 505;
LL dp[N][N];

int main() {
    int n; scanf("%d", &n);
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            for (int k = 0; k <= min(i - j, j - 1); k++) {
                dp[i][j] += dp[i-j][k];
            }
        }
    }
    LL ans = 0;
    for (int i = 0; i <= n; i++)
        ans += dp[n][i];
    printf("%lld\n", ans - 1);
    return 0;
} 


你可能感兴趣的:(acm,ural,动态规划)