题目: 传送门
思路: 易知1必须排在(1,1)处,且如果第一排已经排了a个人,第二排b个人,第三排c个人,第四排d个人,第五排e个人时(必须满足a>=b>=c>=d>=e),编号为a+b+c+d+e的人一定排在了这五排某一排的最右边,那么如果用
d
p
[
a
]
[
b
]
[
c
]
[
d
]
[
e
]
dp[a][b][c][d][e]
dp[a][b][c][d][e]表示第一排排了a个人,第二排排了b个人,第三排排了c个人,第四排排了d个人,第五排排了e个人的总方案数,就可以写出状态转移方程了
d
p
[
a
]
[
b
]
[
c
]
[
d
]
[
e
]
=
d
p
[
a
−
1
]
[
b
]
[
c
]
[
d
]
[
e
]
+
d
p
[
a
]
[
b
−
1
]
[
c
]
[
d
]
[
e
]
+
d
p
[
a
]
[
b
]
[
c
−
1
]
[
d
]
[
e
]
+
d
p
[
a
]
[
b
]
[
c
]
[
d
−
1
]
[
e
]
+
d
p
[
a
]
[
b
]
[
c
]
[
d
]
[
e
−
1
]
dp[a][b][c][d][e] = dp[a-1][b][c][d][e] + dp[a][b-1][c][d][e] + dp[a][b][c-1][d][e] + dp[a][b][c][d-1][e] + dp[a][b][c][d][e-1]
dp[a][b][c][d][e]=dp[a−1][b][c][d][e]+dp[a][b−1][c][d][e]+dp[a][b][c−1][d][e]+dp[a][b][c][d−1][e]+dp[a][b][c][d][e−1],注意下标要大于等于0才有意义
Code:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <sstream>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 32;
const int mod = 1000000009;
typedef pair<int, int> P;
ll dp[maxn][maxn][maxn][maxn][maxn];//dp[a][b][c][d][e]表示第一排有a个人,第二排有b个人,...的安排方案
int arr[maxn];
int main()
{
int k;
while (scanf("%d", &k) != EOF) {
if (!k) break;
memset(dp, 0, sizeof (dp));
memset(arr, 0, sizeof (arr));
for (int i=1;i<=k;i++) {
scanf("%d", &arr[i]);
}
dp[0][0][0][0][0] = 1;
for (int a=1;a<=arr[1];a++)
for (int b=0;b<=min(a, arr[2]);b++)
for (int c=0;c<=min(b, arr[3]);c++)
for (int d=0;d<=min(c, arr[4]);d++)
for (int e=0;e<=min(d, arr[5]);e++) {
ll sum = dp[a-1][b][c][d][e];
if (b) sum += dp[a][b-1][c][d][e];
if (c) sum += dp[a][b][c-1][d][e];
if (d) sum += dp[a][b][c][d-1][e];
if (e) sum += dp[a][b][c][d][e-1];
dp[a][b][c][d][e] = sum;
}
printf("%lld\n", dp[arr[1]][arr[2]][arr[3]][arr[4]][arr[5]]);
}
return 0;
}