HDU 5151 sit sit sit band DP + permutation combination

Source: Internet
Author: User

Sit sit sit question description
In an XX university there areNN chairs lined up, no one in the chair, each chair coloured, blue or red, respectively. Here we go, then.Nn pupils, numbered in the order of three-,..., N. For each student, he would sit down with a chair that no one was sitting on. But if the chair satisfies the following three conditions, he will not sit. 1. There are two adjacent chairs on both sides of this chair 2. The chairs on the left and right side of the chair are not empty, that is, someone sat down 3. The color of the chair on the left and right side of the chair is different if the current student cannot find a chair to sit on, he will go away. For one of the current students, he may have a choice of chairs to sit on. Your task is to calculate how many different all the students are sitting down in the situation. The result may be large and the output answer is1000000007 ({10}^{9}+7)< Span class= "Mord" >10000 00007  (1 0   ? 9  ?? +7) modulo.  
Enter a description
N (1\leq n\leq)n(1≤n≤100nn integers representing the color of a chair, The number ranges from 0 to 1, 0 for Blue, and 1 for red. 
Output description
For each set of test data, the output answer1000000007 ({10}^{9}+7)< Span class= "Mord" >10000 00007  (1 0   ? 9  ?? +7) modulo.  
Input sample
31 0 041 0 0 1
Output sample
48

Problem : interval dp+ Combinatorial counting problems, the transfer equation is, each time you select the last position of the current interval, and then multiply the combined number c[interval length [left interval length]

Note the initialization of the DP array-1

//Meek///#include <bits/stdc++.h>#include <cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<iostream>#include<bitset>using namespacestd;#defineMem (a) memset (A,0,sizeof (a))#definePB Push_back#defineFi first#defineSe Second#defineMP Make_pairtypedefLong Longll;Const intN = the;Const intM =1000001;Const intINF =0x3f3f3f3f;Const intMOD =1000000007;Const DoubleEPS =0.000001;intdp[n][n],a[n],n,c[n+5][n+5];intDfsintLintr) {if(dp[l][r]!=-1)returnDp[l][r]; if(L > R)return 1; int& ret = Dp[l][r] =0; if(L = =r) {if(L = =1|| r = = n| | a[l-1] = = a[r+1])returnRET =1; Else returnRET =0; }     for(inti=l;i<=r;i++) {ret+ = 1ll*dfs (l,i-1) *dfs (i+1, R)%mod*c[r-l][i-l]%MOD; RET%=MOD; }    returnret;}voidinit () { for(intI=0; i<= -; i++) {c[i][0] =1;  for(intj=1; j<=i;j++) {C[i][j]= (c[i-1][J] + c[i-1][j-1])%MOD; } }}intMain () {init ();  while(SCANF ("%d", &n)!=-1) {memset (DP,-1,sizeof(DP));  for(intI=1; i<=n;i++) scanf ("%d",&A[i]); printf ("%d\n", DFS (1, N)); }    return 0;}
Code

HDU 5151 sit sit sit band DP + permutation combination

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.