#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
typedef long long ll;
const int MOD = 1e9 + 7;
int add (int a, int b) {
a += b; if (a >= MOD) a -= MOD;
return a;
}
int sub (int a, int b) {
a -= b; if (a < 0) a += MOD;
return a;
}
int mul (int a, int b) {
return (a * 1ll * b) % MOD;
}
int power (int a, int b) {
if (!b) return 1;
int u = power(a, b >> 1);
u = mul(u, u);
if (b & 1) u = mul(u, a);
return u;
}
int n, l, a[101];
const int B = 1000;
int dp[102][102][B + 1][3];
int ans (int x, int y, int z, int c) {
if (y < 1 || c < 0 || c > 2 || z < 0 || z > B) {
return 0;
}
if (x == n + 1) {
return y == 1 && z >= 0 && z <= l && c == 0;
}
int &ret = dp[x][y][z][c];
if (ret != -1) {
return ret;
}
ret = 0;
if (y == 1) {
ret = add(ret, mul(c, ans(x + 1, y, z + c * a[x], c))); //add to end, and leave open
ret = add(ret, mul(c, ans(x + 1, y, z + c * a[x], c - 1))); //add to end, and close
ret = add(ret, mul(c, ans(x + 1, y + 1, z + c * a[x], c))); //add new open component
ret = add(ret, mul(c, ans(x + 1, y + 1, z + c * a[x], c - 1))); //add new closed component
} else {
int u = 2 * y - 2 + c;
ret = add(ret, mul(c, ans(x + 1, y + 1, z + u * a[x], c))); //add new open component to end
ret = add(ret, mul(c, ans(x + 1, y + 1, z + u * a[x], c - 1))); //add new closed component to end
ret = add(ret, mul(c, ans(x + 1, y, z + u * a[x], c - 1))); //add to end and close
ret = add(ret, mul(y - 1, ans(x + 1, y + 1, z + u * a[x], c))); //insert new component in the middle
ret = add(ret, mul(y - 1, ans(x + 1, y - 1, z + u * a[x], c))); //merge two components
ret = add(ret, mul(u, ans(x + 1, y, z + u * a[x], c))); //add to end of any component
}
return ret;
}
void solve () {
memset(dp, -1, sizeof(dp));
cin >> n >> l;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
if (n == 1) {
cout << 1 << '\n';
return;
}
sort(a + 1, a + n + 1);
for (int i = n; i >= 1; i--) {
a[i] = a[i] - a[i - 1];
}
a[1] = 0;
int sum = ans(2, 1, 0, 1);
sum = mul(sum, 2);
sum = add(sum, ans(2, 1, 0, 2));
cout << sum << '\n';
}
signed main () {
ios::sync_with_stdio(0); cin.tie(0);
int tc = 1; //cin >> tc;
while (tc--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
122656 KB |
Output is correct |
2 |
Correct |
57 ms |
122684 KB |
Output is correct |
3 |
Correct |
57 ms |
122708 KB |
Output is correct |
4 |
Correct |
57 ms |
122704 KB |
Output is correct |
5 |
Correct |
53 ms |
122708 KB |
Output is correct |
6 |
Correct |
56 ms |
122708 KB |
Output is correct |
7 |
Correct |
53 ms |
122708 KB |
Output is correct |
8 |
Correct |
58 ms |
122708 KB |
Output is correct |
9 |
Correct |
60 ms |
122704 KB |
Output is correct |
10 |
Correct |
58 ms |
122708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
122692 KB |
Output is correct |
2 |
Correct |
58 ms |
122704 KB |
Output is correct |
3 |
Correct |
58 ms |
122640 KB |
Output is correct |
4 |
Correct |
61 ms |
122708 KB |
Output is correct |
5 |
Correct |
60 ms |
122708 KB |
Output is correct |
6 |
Correct |
57 ms |
122508 KB |
Output is correct |
7 |
Correct |
54 ms |
122712 KB |
Output is correct |
8 |
Correct |
56 ms |
122684 KB |
Output is correct |
9 |
Correct |
59 ms |
122704 KB |
Output is correct |
10 |
Correct |
56 ms |
122488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
122656 KB |
Output is correct |
2 |
Correct |
57 ms |
122684 KB |
Output is correct |
3 |
Correct |
57 ms |
122708 KB |
Output is correct |
4 |
Correct |
57 ms |
122704 KB |
Output is correct |
5 |
Correct |
53 ms |
122708 KB |
Output is correct |
6 |
Correct |
56 ms |
122708 KB |
Output is correct |
7 |
Correct |
53 ms |
122708 KB |
Output is correct |
8 |
Correct |
58 ms |
122708 KB |
Output is correct |
9 |
Correct |
60 ms |
122704 KB |
Output is correct |
10 |
Correct |
58 ms |
122708 KB |
Output is correct |
11 |
Correct |
53 ms |
122692 KB |
Output is correct |
12 |
Correct |
58 ms |
122704 KB |
Output is correct |
13 |
Correct |
58 ms |
122640 KB |
Output is correct |
14 |
Correct |
61 ms |
122708 KB |
Output is correct |
15 |
Correct |
60 ms |
122708 KB |
Output is correct |
16 |
Correct |
57 ms |
122508 KB |
Output is correct |
17 |
Correct |
54 ms |
122712 KB |
Output is correct |
18 |
Correct |
56 ms |
122684 KB |
Output is correct |
19 |
Correct |
59 ms |
122704 KB |
Output is correct |
20 |
Correct |
56 ms |
122488 KB |
Output is correct |
21 |
Correct |
87 ms |
122704 KB |
Output is correct |
22 |
Correct |
149 ms |
122708 KB |
Output is correct |
23 |
Correct |
97 ms |
122704 KB |
Output is correct |
24 |
Correct |
129 ms |
122708 KB |
Output is correct |
25 |
Correct |
92 ms |
122964 KB |
Output is correct |
26 |
Correct |
94 ms |
122704 KB |
Output is correct |
27 |
Correct |
199 ms |
122704 KB |
Output is correct |
28 |
Correct |
183 ms |
122704 KB |
Output is correct |
29 |
Correct |
157 ms |
122708 KB |
Output is correct |
30 |
Correct |
89 ms |
122636 KB |
Output is correct |