#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using PII = pair<int, int>;
const int N = 105, V = 3005;
const int M = 1e9 + 7;
LL dp[N][N][V][2][2], a[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, L;
cin >> n >> L;
for(int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + 1 + n), a[n + 1] = 2004;
int d = a[2] - a[1];
dp[1][1][0][0][0] = 1, dp[1][1][d][1][0] = 1;
dp[1][1][d][0][1] = 1, dp[1][1][d * 2][1][1] = 1;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
for(int sum = 0; sum <= L; sum++) {
for(int l = 0; l <= 1; l++) {
for(int r = 0; r <= 1; r++) {
// place a new component in the middle
int new_sum = sum + (j * 2 + l + r) * (a[i + 2] - a[i + 1]);
dp[i + 1][j + 1][new_sum][l][r] += dp[i][j][sum][l][r] * (j - 1) % M;
dp[i + 1][j + 1][new_sum][l][r] %= M;
//place a new component in the left , don't block it
if(l > 0){
new_sum = sum + (j * 2 + l + r) * (a[i + 2] - a[i + 1]);
dp[i + 1][j + 1][new_sum][l][r] += dp[i][j][sum][l][r];
dp[i + 1][j + 1][new_sum][l][r] %= M;
}
//place a new component in the left and block it
if(l > 0){
new_sum = sum + (j * 2 + r) * (a[i + 2] - a[i + 1]);
dp[i + 1][j + 1][new_sum][0][r] += dp[i][j][sum][l][r];
dp[i + 1][j + 1][new_sum][0][r] %= M;
}
//place a new component in the right, don't block it
if(r > 0){
new_sum = sum + (j * 2 + l + r) * (a[i + 2] - a[i + 1]);
dp[i + 1][j + 1][new_sum][l][r] += dp[i][j][sum][l][r];
dp[i + 1][j + 1][new_sum][l][r] %= M;
}
//place a new component in the right and block it
if(r > 0){
new_sum = sum + (j * 2 + l) * (a[i + 2] - a[i + 1]);
dp[i + 1][j + 1][new_sum][l][0] += dp[i][j][sum][l][r];
dp[i + 1][j + 1][new_sum][l][0] %= M;
}
//add to existing component but not at the end of permutation
new_sum = sum + ((j - 1) * 2 + l + r) * (a[i + 2] - a[i + 1]);
dp[i + 1][j][new_sum][l][r] += dp[i][j][sum][l][r] * (j - 1 ) * 2 % M;
dp[i + 1][j][new_sum][l][r] %= M;
//adding to left of the leftmost component but do not block it
if(l > 0){
new_sum = sum + ((j - 1) * 2 + l + r) * (a[i + 2] - a[i + 1]);
dp[i + 1][j][new_sum][l][r] += dp[i][j][sum][l][r];
dp[i + 1][j][new_sum][l][r] %= M;
}
//adding to left of the leftmost component and block it
if(l > 0){
new_sum = sum + ((j - 1) * 2 + l + r - 1) * (a[i + 2] - a[i + 1]);
dp[i + 1][j][new_sum][0][r] += dp[i][j][sum][l][r];
dp[i + 1][j][new_sum][0][r] %= M;
}
//adding to right of the rightmost component but do not block it
if(r > 0){
new_sum = sum + ((j - 1) * 2 + l + r) * (a[i + 2] - a[i + 1]);
dp[i + 1][j][new_sum][l][r] += dp[i][j][sum][l][r];
dp[i + 1][j][new_sum][l][r] %= M;
}
//adding to right of the rightmost component and block it
if(r > 0){
new_sum = sum + ((j - 1) * 2 + l + r - 1) * (a[i + 2] - a[i + 1]);
dp[i + 1][j][new_sum][l][0] += dp[i][j][sum][l][r];
dp[i + 1][j][new_sum][l][0] %= M;
}
//merging two components
if(j > 1 ) {
new_sum = sum + ((j - 2 ) * 2 + l + r) * (a[i + 2] - a[i + 1]);
dp[i + 1][j - 1][new_sum][l][r] += dp[i][j][sum][l][r] * (j - 1)%M;
dp[i + 1][j - 1][new_sum][l][r] %= M;
}
}
}
}
}
}
LL ans = 0;
for(int sum = 0; sum <= L; sum++) ans = (ans + dp[n][1][sum][0][0]) % M;
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
4 ms |
4952 KB |
Output is correct |
6 |
Correct |
4 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
4 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3164 KB |
Output is correct |
2 |
Correct |
2 ms |
3676 KB |
Output is correct |
3 |
Correct |
1 ms |
3160 KB |
Output is correct |
4 |
Correct |
3 ms |
3676 KB |
Output is correct |
5 |
Correct |
3 ms |
3676 KB |
Output is correct |
6 |
Correct |
2 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3164 KB |
Output is correct |
8 |
Correct |
1 ms |
3164 KB |
Output is correct |
9 |
Correct |
2 ms |
3420 KB |
Output is correct |
10 |
Correct |
2 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
4 ms |
4952 KB |
Output is correct |
6 |
Correct |
4 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
4 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
1 ms |
3164 KB |
Output is correct |
12 |
Correct |
2 ms |
3676 KB |
Output is correct |
13 |
Correct |
1 ms |
3160 KB |
Output is correct |
14 |
Correct |
3 ms |
3676 KB |
Output is correct |
15 |
Correct |
3 ms |
3676 KB |
Output is correct |
16 |
Correct |
2 ms |
3420 KB |
Output is correct |
17 |
Correct |
1 ms |
3164 KB |
Output is correct |
18 |
Correct |
1 ms |
3164 KB |
Output is correct |
19 |
Correct |
2 ms |
3420 KB |
Output is correct |
20 |
Correct |
2 ms |
3420 KB |
Output is correct |
21 |
Correct |
6 ms |
7768 KB |
Output is correct |
22 |
Correct |
240 ms |
100948 KB |
Output is correct |
23 |
Correct |
470 ms |
189268 KB |
Output is correct |
24 |
Correct |
362 ms |
149764 KB |
Output is correct |
25 |
Correct |
465 ms |
191316 KB |
Output is correct |
26 |
Correct |
400 ms |
167216 KB |
Output is correct |
27 |
Correct |
125 ms |
66132 KB |
Output is correct |
28 |
Correct |
149 ms |
75228 KB |
Output is correct |
29 |
Correct |
280 ms |
120756 KB |
Output is correct |
30 |
Correct |
475 ms |
192088 KB |
Output is correct |