POJ2279 Mr. Young‘s Picture Permutations(线性dp)

Description

Mr. Young wishes to take a picture of his class. The students will stand in rows with each row no longer than the row behind it and the left ends of the rows aligned. For instance, 12 students could be arranged in rows (from back to front) of 5, 3, 3 and 1 students.

In addition, Mr. Young wants the students in each row arranged so that heights decrease from left to right. Also, student heights should decrease from the back to the front. Thinking about it, Mr. Young sees that for the 12-student example, there are at least two ways to arrange the students (with 1 as the tallest etc.):

Mr. Young wonders how many different arrangements of the students there might be for a given arrangement of rows. He tries counting by hand starting with rows of 3, 2 and 1 and counts 16 arrangements:

123 123 124 124 125 125 126 126 134 134 135 135 136 136 145 146
45 46 35 36 34 36 34 35 25 26 24 26 24 25 26 25
6 5 6 5 6 4 5 4 6 5 6 4 5 4 3 3

Mr. Young sees that counting by hand is not going to be very effective for any reasonable number of students so he asks you to help out by writing a computer program to determine the number of different arrangements of students for a given set of rows.

Input

The input for each problem instance will consist of two lines. The first line gives the number of rows, k, as a decimal integer. The second line contains the lengths of the rows from back to front ( n 1 , n 2 , . . . , n k n_1, n_2,..., n_k n1,n2,...,nk) as decimal integers separated by a single space. The problem set ends with a line with a row count of 0. There will never be more than 5 rows and the total number of students, N, (sum of the row lengths) will be at most 30.

Output

The output for each problem instance shall be the number of arrangements of the N students into the given rows so that the heights decrease along each row from left to right and along each column from back to front as a decimal integer. (Assume all heights are distinct.) The result of each problem instance should be on a separate line. The input data will be chosen so that the result will always fit in an unsigned 32 bit integer.

Sample Input

1
30
5
1 1 1 1 1
3
3 2 1
4
5 3 3 1
5
6 5 4 3 2
2
15 15
0

Sample Output

1
1
16
4158
141892608
9694845

f [ i ] [ j ] [ k ] [ l ] [ m ] f[i][j][k][l][m] f[i][j][k][l][m] 代表第一行站着 i i i 个人,第二行 j j j 个人…第五行 m m m个人。

第二行的状态转移方程为: f [ i ] [ j ] [ k ] [ l ] [ m ] + = f [ i ] [ j − 1 ] [ k ] [ l ] [ m ] , ( i > = j ) f[i][j][k][l][m]+=f[i][j-1][k][l][m],(i>=j) f[i][j][k][l][m]+=f[i][j1][k][l][m](i>=j)

其他行的转移方程类似。

#include<iostream>
#include<cstring>
#define int long long
#define rep(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
int n,a[5],f[32][18][12][10][8];

signed main(){
	while(cin>>n,n){
		memset(a,0,sizeof(a));
		memset(f,0,sizeof(f)); f[0][0][0][0][0]=1;
		rep(i,0,n-1) cin>>a[i];
		rep(i,0,a[0]) rep(j,0,a[1]) rep(k,0,a[2]) rep(l,0,a[3]) rep(m,0,a[4]){
			if(i) f[i][j][k][l][m]+=f[i-1][j][k][l][m];
			if(j&&j<=i) f[i][j][k][l][m]+=f[i][j-1][k][l][m];
			if(k&&k<=j) f[i][j][k][l][m]+=f[i][j][k-1][l][m];
			if(l&&l<=k) f[i][j][k][l][m]+=f[i][j][k][l-1][m];
			if(m&&m<=l) f[i][j][k][l][m]+=f[i][j][k][l][m-1];
		}
		cout<<f[a[0]][a[1]][a[2]][a[3]][a[4]]<<endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_51864047

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值