#include<bits/stdc++.h>
#define int long long
#define ld long double
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define fi first
#define se second
#define TII tuple<int, int, int>
#define MT make_tuple
#define mp make_pair
#define ts to_string
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define lb lower_bound
#define ub upper_bound
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
const int MOD = 1e9 + 7, N = 105, K = 4'005;
int dp[N][N][K][3];
bool interval(int a, int b, int c) {
return a <= b && b <= c;
}
int32_t main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n, m; cin >> n >> m;
vi a(n + 2); for(int i = 1; i <= n; i++) cin >> a[i]; a[n + 1] = K;
if(n == 1) {
cout << "1\n";
return 0;
}
sort(all(a));
dp[0][0][0][0] = 1;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i < j) continue;
for(int k = 0; k < K; k++) {
for(int l = 0; l <= 2; l++) {
if(l > j) continue;
dp[i][j][k][l] %= MOD;
int d = (2 * j - l) * (a[i + 2] - a[i + 1]);
// add new CC in the middle of the permutation
if(k + d + 2 * (a[i + 2] - a[i + 1]) < K)
dp[i + 1][j + 1][k + d + 2 * (a[i + 2] - a[i + 1])][l] += dp[i][j][k][l];
// add new CC in the end of the permutation
if(k + d + (a[i + 2] - a[i + 1]) < K && l <= 1)
dp[i + 1][j + 1][k + d + (a[i + 2] - a[i + 1])][l + 1] += (2 - l) * dp[i][j][k][l];
// connect i + 1 to the end of any CC but still in the middle of the permutation
if(k + d < K)
dp[i + 1][j][k + d][l] += (2 * j - l) * dp[i][j][k][l];
// connect i + 1 to the end of any CC and at the end of the permutation
if(interval(0, k + d - (a[i + 2] - a[i + 1]), K - 1) && l <= 1) {
dp[i + 1][j][k + d - (a[i + 2] - a[i + 1])][l + 1] += (2 - l) * (j - l) * dp[i][j][k][l];
// special case : i + 1 connect to a CC that has an end of a permutation
if(i == n - 1 && j == 1 && l == 1)
dp[i + 1][j][k + d - (a[i + 2] - a[i + 1])][l + 1] += dp[i][j][k][l];
}
// connect 2 CC
int mi = j - l, en = l;
int conv = mi * en + mi * (mi - 1);
if(i == n - 1 && mi == 0 && en == 2) conv++;
if(interval(0, k + d - 2 * (a[i + 2] - a[i + 1]), K - 1) && j >= 2)
dp[i + 1][j - 1][k + d - 2 * (a[i + 2] - a[i + 1])][l] += conv * dp[i][j][k][l];
}
}
}
}
int ans = 0;
for(int k = 0; k <= m; k++) ans = (ans + dp[n][1][k][2] % MOD) % MOD;
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |