#include <bits/stdc++.h>
#define ll long long
#define ar array
using namespace std;
const int mod = 1e9+7;
void fadd(int &a, int b) {
a += b;
if (a >= mod) a -= mod;
}
int mul(int a, int b) {
return (ll)a*b%mod;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, L; cin >> n >> L;
vector<int> t(n);
for (int &x : t) cin >> x;
if (n == 1) {
cout << 1 << '\n';
return 0;
}
sort(t.begin(), t.end());
vector<map<int, ar<int, 4>>> dp(n+1);
// [# of components][total sum rn][(leftmost element filled) + (rightmost element filled)*2]
dp[0][0][0] = 1;
vector<int> pref(n);
for (int i = 0; i < n; i++) pref[i] = t[i] + (i?pref[i-1]:0);
for (int i = 0; i < n; i++) {
vector<map<int, ar<int, 4>>> dp2(n+1);
for (int j = 0; j <= n; j++) {
for (const auto &[l, val] : dp[j]) {
int need_merge = j-1;
if (need_merge > 0 && (i+need_merge-1 >= n || (pref[i+need_merge-1] - (i?pref[i-1]:0))*2 + l > L)) {
continue;
}
for (int tp = 0; tp < 4; tp++) {
int num = val[tp];
if (!num) continue;
if (j > 1) { // merge
fadd(dp2[j-1][l+2*t[i]][tp], mul(num, j-1));
}
if (j > 0) { // add to comp
if (2*j-(tp&1)-(tp >> 1 & 1) > 0) fadd(dp2[j][l][tp], mul(num, 2*j-(tp&1)-(tp >> 1 & 1)));
if (!(tp & 1)) fadd(dp2[j][l+t[i]][tp|1], num);
if (!(tp >> 1 & 1)) fadd(dp2[j][l+t[i]][tp|2], num);
}
if (j+1 <= n) { // new comp
if (j+1-(tp&1)-(tp >> 1 & 1) > 0) fadd(dp2[j+1][l-2*t[i]][tp], mul(j+1-(tp&1)-(tp >> 1 & 1), num));
if (!(tp & 1)) fadd(dp2[j+1][l-t[i]][tp|1], num);
if (!(tp >> 1 & 1)) fadd(dp2[j+1][l-t[i]][tp|2], num);
}
}
}
}
swap(dp, dp2);
}
int ans = 0;
for (int i = 0; i <= L; i++) {
fadd(ans, dp[1][i][3]);
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
383 ms |
1936 KB |
Output is correct |
23 |
Correct |
467 ms |
2340 KB |
Output is correct |
24 |
Correct |
336 ms |
1808 KB |
Output is correct |
25 |
Correct |
381 ms |
2252 KB |
Output is correct |
26 |
Correct |
350 ms |
2092 KB |
Output is correct |
27 |
Correct |
104 ms |
652 KB |
Output is correct |
28 |
Correct |
142 ms |
852 KB |
Output is correct |
29 |
Correct |
296 ms |
1424 KB |
Output is correct |
30 |
Correct |
397 ms |
2340 KB |
Output is correct |