1
+ /* *
2
+ * @file 2066D2.cpp
3
+ * @author Macesuted ([email protected] )
4
+ * @date 2025-02-17
5
+ *
6
+ * @copyright Copyright (c) 2025
7
+ *
8
+ */
9
+
10
+ #include < bits/stdc++.h>
11
+ using namespace std ;
12
+
13
+ #ifndef LOCAL
14
+ #define endl ' \n '
15
+ #endif
16
+
17
+ bool mem1;
18
+
19
+ #define maxn 105
20
+ #define maxm 10005
21
+ #define mod 1'000'000'007
22
+
23
+ int a[maxm], pre[maxm], cnt[maxn], lst[maxn], f[maxn][maxm];
24
+ int64_t fac[maxm], ifac[maxm];
25
+
26
+ int64_t qpow (int64_t a, int64_t x) {
27
+ int64_t ans = 1 ;
28
+ while (x) {
29
+ if (x & 1 ) ans = ans * a % mod;
30
+ a = a * a % mod, x >>= 1 ;
31
+ }
32
+ return ans;
33
+ }
34
+ int64_t inv (int64_t a) { return qpow (a, mod - 2 ); }
35
+ int64_t C (int n, int m) { return fac[n] * ifac[m] % mod * ifac[n - m] % mod; }
36
+
37
+ void solve (void ) {
38
+ int n, c, m;
39
+ cin >> n >> c >> m;
40
+
41
+ for (int i = 1 ; i <= n; i++) cnt[i] = lst[i] = 0 ;
42
+ for (int i = 1 ; i <= m; i++) {
43
+ cin >> a[i];
44
+ if (a[i]) cnt[a[i]]++, lst[a[i]] = i;
45
+ }
46
+
47
+ if (cnt[n] > c) return cout << 0 << endl, void ();
48
+
49
+ for (int i = 1 ; i <= n; i++)
50
+ for (int j = 0 ; j <= m; j++) f[i][j] = 0 ;
51
+
52
+ f[0 ][0 ] = 1 ;
53
+ for (int i = 1 ; i <= n; i++) {
54
+ for (int j = 1 ; j <= m; j++) pre[j] = pre[j - 1 ] + (a[j] <= i);
55
+ for (int j = 0 ; j <= m; j++)
56
+ if (f[i - 1 ][j])
57
+ for (int k = (i == n ? c : cnt[i]); k <= c; k++)
58
+ if (lst[i] <= c + j && j + k <= m && k <= pre[min (m, c + j)] - j)
59
+ f[i][j + k] = (f[i][j + k] + f[i - 1 ][j] * C (pre[min (m, c + j)] - j - cnt[i], k - cnt[i])) % mod;
60
+ }
61
+
62
+ // for (int i = 1; i <= n; i++) {
63
+ // for (int j = 0; j <= m; j++) cerr << f[i][j] << ' ';
64
+ // cerr << endl;
65
+ // }
66
+
67
+ cout << f[n][m] << endl;
68
+
69
+ return ;
70
+ }
71
+
72
+ bool mem2;
73
+
74
+ int main () {
75
+ ios::sync_with_stdio (false ), cin.tie (nullptr );
76
+ #ifdef LOCAL
77
+ cerr << " Memory Cost: " << abs (&mem1 - &mem2) / 1024 . / 1024 . << " MB" << endl;
78
+ #endif
79
+
80
+ fac[0 ] = ifac[0 ] = 1 ;
81
+ for (int i = 1 ; i < maxm; i++) fac[i] = fac[i - 1 ] * i % mod;
82
+ ifac[maxm - 1 ] = inv (fac[maxm - 1 ]);
83
+ for (int i = maxm - 2 ; i; i--) ifac[i] = ifac[i + 1 ] * (i + 1 ) % mod;
84
+
85
+ int _ = 1 ;
86
+ cin >> _;
87
+ while (_--) solve ();
88
+
89
+ #ifdef LOCAL
90
+ cerr << " Time Cost: " << clock () * 1000 . / CLOCKS_PER_SEC << " MS" << endl;
91
+ #endif
92
+ return 0 ;
93
+ }
0 commit comments