input:map.in output:map.out

时间限制: 1000 ms 空间限制: 262144 KB

题目描述

输入

输出

样例输入

1
2
4
2 1 1 2

样例输出

1
2

数据范围限制

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<bits/stdc++.h>
#define ll long long
#define maxn 2010
#define mod 998244353
using namespace std;
ll f[maxn][maxn];
int main()
{
freopen("map.in", "r", stdin);
freopen("map.out", "w", stdout);
int n, s1 = 0, s2 = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
int x;
scanf("%d", &x);
if (x == 1) s1++;
else s2++;
}
f[0][0] = 1;
for (int tot = 0; tot <= n; tot++)
for (int i = tot; i >= 0; i--)
{
int j = tot - i;
if (j < 0) continue;
if (j == 0 && i) f[i + 1][j] = (f[i + 1][j] + f[i - 1][j] * i % mod) % mod;
if (i >= 2)
{
ll tmp = f[i - 2][j] * ((ll)i * (i - 1) / 2 % mod) % mod;
f[i][j + 1] = (f[i][j + 1] + tmp) % mod;
}
if (j >= 2)
{
ll tmp = f[i + 2][j - 2] * ((ll)j * (j - 1) / 2 % mod) % mod;
f[i][j + 1] = (f[i][j + 1] + tmp) % mod;
}
if (i && j)
{
ll tmp = f[i][j - 1] * i % mod * j % mod;
f[i][j + 1] = (f[i][j + 1] + tmp) % mod;
}
}
printf("%lld\n", f[s1][s2]);
return 0;
}