HDU 5151 sit sit sit
Topic links
Interval dp+ Combinatorial counting problem, the transfer equation is, each time we select the last position of the current interval, then multiply the combined number c[interval length [left interval length]
Code:
#include <cstdio> #include <cstring>typedef long long ll;const ll MOD = 1000000007;const int N = 105;int N, a[n ];ll Dp[n][n], C[n][n];int main () { c[1][0] = c[1][1] = 1; for (int i = 2; I <=; i++) {c[i][0] = c[i][i] = 1;for (int j = 1; j < I; J + +) { C[i][j] = (C[i-1][j] + c[i -1][j-1])% MOD;} } while (~SCANF ("%d", &n)) {for (int i = 1; I <= n; i++) scanf ("%d", &a[i]), memset (DP, 0, sizeof (DP)); for (int i = 1; I <= N; i++) Dp[i][i] = 1;for (int len = 1; len < n; len++) {for (int l = 1; l + len <= N; l++) {int r = l + len;for (i NT k = l; K <= R; k++) { if (l = = k) Dp[l][r] = (Dp[l][r] + dp[k + 1][r])% MOD; else if (r = = k) Dp[l][r] = (Dp[l][r] + dp[l][k-1])% MOD; else if (a[k-1] = = a[k + 1]) dp[l][r] = (Dp[l][r] + (dp[l][k-1] * dp[k + 1][r]% mod * c[r-l][k-l])% mod)% MoD;} }} printf ("%lld\n", Dp[1][n]); } return 0;}
HDU 5151 sit sit sit (interval dp)