/*
* DP Component: https://codeforces.com/blog/entry/47764
*
* My code has ORDERED components
*/
#include <bits/stdc++.h>
using namespace std;
#define F0R(i, n) for (int i = 0; i < n; i++)
#define ll long long
void add(ll &a, ll b) {
a = (a + b) % 1000000007;
}
ll A[101];
ll dp[101][101][1001][3];
int main() {
int n, L; cin >> n >> L;
F0R(i, n) cin >> A[i];
sort(A, A+n);
if (n == 1) {
cout << 1 << endl;
return 0;
}
F0R(i, n+1) F0R(j, n+1) F0R(k, L+1) F0R(m, 3) dp[i][j][k][m] = 0;
dp[0][0][0][0] = 1;
A[n] = 2000;
for (int i = 0; i < n; i++) {
ll diff = A[i+1] - A[i];
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= L; k++) {
for (int l = 0; l <= 2; l++) {
if (dp[i][j][k][l] == 0) continue;
// fill in an endpoint
if (l < 2) {
// create new component
int k2 = k + diff*(2*j-l+1);
if (k2 <= L) {
add(dp[i+1][j+1][k2][l+1], dp[i][j][k][l]*(2-l));
}
// merge with a component
if (j > 0) {
k2 = k + diff*(2*j-l-1);
if (k2 <= L) add(dp[i+1][j][k2][l+1], dp[i][j][k][l]*(2-l));
}
}
// create a new component
int k2 = k + diff*(2*j-l+2);
if (k2 <= L) add(dp[i+1][j+1][k2][l], dp[i][j][k][l]*(j-l+1));
// append to a component
k2 = k + diff*(2*j-l);
if (k2 <= L) add(dp[i+1][j][k2][l], dp[i][j][k][l]*(2*j-l));
// merge two components
if (j >= 2) {
k2 = k + diff*(2*j-l-2);
if (k2 <= L) add(dp[i+1][j-1][k2][l], dp[i][j][k][l]*(j-1));
}
}
}
}
}
ll ans = 0;
for (int i = 0; i <= L; i++) {
add(ans, dp[n][1][i][2]);
}
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
6 ms |
2432 KB |
Output is correct |
6 |
Correct |
6 ms |
2304 KB |
Output is correct |
7 |
Correct |
5 ms |
1024 KB |
Output is correct |
8 |
Correct |
5 ms |
896 KB |
Output is correct |
9 |
Correct |
6 ms |
2304 KB |
Output is correct |
10 |
Correct |
5 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1280 KB |
Output is correct |
2 |
Correct |
5 ms |
1792 KB |
Output is correct |
3 |
Correct |
5 ms |
1536 KB |
Output is correct |
4 |
Correct |
5 ms |
1792 KB |
Output is correct |
5 |
Correct |
6 ms |
1792 KB |
Output is correct |
6 |
Correct |
5 ms |
1792 KB |
Output is correct |
7 |
Correct |
6 ms |
1408 KB |
Output is correct |
8 |
Correct |
5 ms |
1536 KB |
Output is correct |
9 |
Correct |
5 ms |
1664 KB |
Output is correct |
10 |
Correct |
6 ms |
1664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
6 ms |
2432 KB |
Output is correct |
6 |
Correct |
6 ms |
2304 KB |
Output is correct |
7 |
Correct |
5 ms |
1024 KB |
Output is correct |
8 |
Correct |
5 ms |
896 KB |
Output is correct |
9 |
Correct |
6 ms |
2304 KB |
Output is correct |
10 |
Correct |
5 ms |
896 KB |
Output is correct |
11 |
Correct |
5 ms |
1280 KB |
Output is correct |
12 |
Correct |
5 ms |
1792 KB |
Output is correct |
13 |
Correct |
5 ms |
1536 KB |
Output is correct |
14 |
Correct |
5 ms |
1792 KB |
Output is correct |
15 |
Correct |
6 ms |
1792 KB |
Output is correct |
16 |
Correct |
5 ms |
1792 KB |
Output is correct |
17 |
Correct |
6 ms |
1408 KB |
Output is correct |
18 |
Correct |
5 ms |
1536 KB |
Output is correct |
19 |
Correct |
5 ms |
1664 KB |
Output is correct |
20 |
Correct |
6 ms |
1664 KB |
Output is correct |
21 |
Correct |
10 ms |
8832 KB |
Output is correct |
22 |
Correct |
136 ms |
146104 KB |
Output is correct |
23 |
Correct |
196 ms |
240248 KB |
Output is correct |
24 |
Correct |
165 ms |
219128 KB |
Output is correct |
25 |
Correct |
189 ms |
240108 KB |
Output is correct |
26 |
Correct |
165 ms |
240120 KB |
Output is correct |
27 |
Correct |
72 ms |
100088 KB |
Output is correct |
28 |
Correct |
87 ms |
113400 KB |
Output is correct |
29 |
Correct |
140 ms |
178556 KB |
Output is correct |
30 |
Correct |
175 ms |
240120 KB |
Output is correct |