This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define DEBUG false
using namespace std;
const int MAX_N = 105;
const int MAX_L = 1005;
const int MOD = 1e9 + 7;
int A[MAX_N];
long long dp[MAX_N][MAX_N][MAX_L][3];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, L;
cin >> N >> L;
for(int i = 1; i <= N; i++) {
cin >> A[i];
}
if(N == 1) { // the answer will be 0, so 0 is strictly less than or equal to L
cout << 0;
return 0;
}
sort(A + 1, A + N + 1);
dp[1][1][0][0] = 1;
dp[1][1][0][1] = 2;
for(int i = 1; i < N; i++) {
for(int j = 1; j <= i; j++) {
for(int k = 0; k <= L; k++) {
for(int z = 0; z <= 2; z++) {
int nk = k + (2 * j - z) * (A[i + 1] - A[i]);
if(nk > L) {
continue;
}
if(z < 2) { // add i to end point
dp[i + 1][j][nk][z + 1] += (2 - z) * dp[i][j][k][z]; // add i to the left or right most component to be an end point
dp[i + 1][j][nk][z + 1] %= MOD;
dp[i + 1][j + 1][nk][z + 1] = (2 - z) * dp[i][j][k][z]; // create new component to the left or right most
dp[i + 1][j + 1][nk][z + 1] %= MOD;
}
dp[i + 1][j][nk][z] += (2 * j - z) * dp[i][j][k][z]; // add i to left or right most of some components
dp[i + 1][j][nk][z] %= MOD;
dp[i + 1][j + 1][nk][z] += (j + 1 - z) * dp[i][j][k][z]; // create new component
dp[i + 1][j + 1][nk][z] %= MOD;
dp[i + 1][j - 1][nk][z] += (j - 1) * dp[i][j][k][z]; // merge two components
dp[i + 1][j - 1][nk][z] %= MOD;
}
}
}
}
long long ans = 0;
for(int k = 0; k <= L; k++) {
ans += dp[N][1][k][2];
ans %= MOD;
}
cout << ans;
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... |