HDU 5151 sit sit sit (interval dp)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.