HDU 5151 sit sit sit zone DP

Source: Internet
Author: User

Topic Links:

http://acm.hdu.edu.cn/showproblem.php?pid=5151

Exercises

There are n chairs, numbered 1 to N.

Now there are n students, numbered 1 to N, starting from the first classmate to choose the seat to sit, and this classmate can not sit at the same time meet the following three conditions of the Chair.

1. There are adjacent seats around

2, around the adjacent seats are empty.

3, the left and right sides of the seat color is different.

Ask how many kinds of sitting methods are in total.

Exercises

DP[I][J] Indicates the number of classes of chairs numbered I to J.

Now the first person to start the seat chooses the T-seat, then Dp[i][j]+=dp[i][t-1]*dp[t+1][j]*c[j-i][t-i], where the combination number C represents the number of combinations that arrange the t-i individual on the left.

Code:
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 using namespacestd;6 7typedefLong LongLL;8 Const intMoD = 1e9 +7;9 Const intMAXN =111;Ten  One intN; A intARR[MAXN]; - LL DP[MAXN][MAXN],C[MAXN][MAXN]; -  the voidPre () { -c[0][0] =1; -      for(inti =1; i < MAXN; i++) { -c[i][0] =1; +          for(intj =1; J <= I; J + +) { -C[I][J] = (c[i-1][j-1] + c[i-1][J])%MoD; +         } A     } at } -  -LL DFS (intIintj) { -     if(i = = j)returnDP[I][J] =1; -     if(i > J)return 0; -     if(Dp[i][j]! =-1)returnDp[i][j]; inLL &ret = dp[i][j] =0; -ret= (Ret+dfs (i +1, j) +dfs (I, J-1))%MoD; to      for(intMID = i +1; Mid <= J-1; mid++) { +         if(Arr[mid-1] ^ Arr[mid +1])Continue; -ret = (Ret+dfs (i, Mid-1) *dfs (Mid +1, j)%mod*c[j-i][mid-i])%MoD; the     } *     returnret; $ }Panax Notoginseng  - voidinit () { theMemset (DP,-1,sizeof(DP)); + } A  the intMain () { + pre (); -      while(SCANF ("%d", &n) = =1&&N) { $ init (); $          for(inti =1; I <= N; i++) scanf ("%d", arr +i); -LL Ans=dfs (1, n); -printf"%lld\n", ans); the     } -     return 0;Wuyi}

HDU 5151 sit sit sit zone 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.