POJ-2279-Mr. Young's Picture Permutations(DP)

Mr. Young’s Picture Permutations

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.
X X X X X

X X X

X X X

X

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.):
1 2 3 4 5 1 5 8 11 12

6 7 8 2 6 9

9 10 11 3 7 10

12 4

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 (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] 表示每排安排i,j,k,l,m个人时的方案数。
那么f[i,j,k,l,m]肯定是包含在f[i+1,j,k,l,m]里的。
同理也包含在f[i,j+1,k,l,m],f[i,j,k+1,l,m],f[i,j,k,l+1,m],f[i,j,k,l,m+1]中。
但是需要注意的是状态转移的时候,第 x 排人数要小于前面所有排的人数。
不然是没办法计算的。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#define int long long
using namespace std;

const int mod = 1e9+7;
const int N = 1e6+10;

int dp[31][16][11][9][6];
int a[10];
signed main()
{
     int n;
     while(cin>>n && n)
     {
        memset(dp,0,sizeof(dp));
        memset(a,0,sizeof(a));
        for(int i = 1 ; i <= n ; i ++)
            cin>>a[i];
        dp[0][0][0][0][0] = 1;
        for(int i = 0 ; i <= a[1] ; i ++)
            for(int j = 0 ; j <= a[2] ; j ++)
                for(int k = 0; k <= a[3] ; k ++)
                    for(int l = 0; l <= a[4] ; l ++)
                        for(int m = 0 ; m <= a[5] ; m ++)
                        {
                            if(i+1 <= a[1]) dp[i+1][j][k][l][m] += dp[i][j][k][l][m];
                            if(j+1 <= a[2] && j < i) dp[i][j+1][k][l][m] += dp[i][j][k][l][m];
                            if(k+1 <= a[3] && k < i && k < j) dp[i][j][k+1][l][m] += dp[i][j][k][l][m];
                            if(l+1 <= a[4] && l < i && l < j && l < k) dp[i][j][k][l+1][m] += dp[i][j][k][l][m];
                            if(m+1 <= a[5] && m < i && m < j && m < k && m < l) dp[i][j][k][l][m+1] += dp[i][j][k][l][m];
                        }
        cout<<dp[a[1]][a[2]][a[3]][a[4]][a[5]]<<endl;
     }
}

发布了104 篇原创文章 · 获赞 7 · 访问量 4095

猜你喜欢

转载自blog.csdn.net/qq_43461168/article/details/103190978