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>
using namespace std;
const int MOD = 1000000007;
int dp[105][105][1005][4];
int Add(int x, int y) {
return (x + y) % MOD;
}
void Increase(int& x, int y) {
x = Add(x, y);
}
int Multiply(int x, int y) {
return 1LL * x * y % MOD;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, L;
cin >> n >> L;
vector<int> a(n + 1);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
sort(a.begin() + 1, a.end());
dp[2][1][0][0] = (n > 2); // middle
dp[2][1][0][1] = 1; // left
dp[2][1][0][2] = 1; // right
dp[2][1][0][3] = (n == 1); // both
for (int i = 2; i <= n; ++i) {
int delta = a[i] - a[i - 1];
for (int components = 1; components < i; ++components) {
for (int balance = 0; balance <= L; ++balance) {
for (int flag = 0; flag <= 3; ++flag) {
if (dp[i][components][balance][flag] == 0) {
continue;
}
int totalBorders = components * 2 - (flag & 1) - ((flag & 2) > 0);
if ((flag & 1) == 0) {
// place at the left side of the first component
int newBalance = balance + totalBorders * delta;
if (newBalance <= L) {
Increase(dp[i + 1][components][newBalance][flag | 1], dp[i][components][balance][flag]);
}
// create a new component at the left side
newBalance = balance + totalBorders * delta;
if (newBalance <= L) {
Increase(dp[i + 1][components + 1][newBalance][flag | 1], dp[i][components][balance][flag]);
}
}
if ((flag & 2) == 0) {
// place at the right side of the last component
int newBalance = balance + totalBorders * delta;
if (newBalance <= L) {
Increase(dp[i + 1][components][newBalance][flag | 2], dp[i][components][balance][flag]);
}
// create a new component at the right side
newBalance = balance + totalBorders * delta;
if (newBalance <= L) {
Increase(dp[i + 1][components + 1][newBalance][flag | 2], dp[i][components][balance][flag]);
}
}
// create a new component
int newBalance = balance + totalBorders * delta;
int validPositions = (components + 1) - (flag & 1) - ((flag & 2) > 0);
if (newBalance <= L) {
Increase(dp[i + 1][components + 1][newBalance][flag], Multiply(validPositions, dp[i][components][balance][flag]));
}
// append to the left side of a component
newBalance = balance + totalBorders * delta;
validPositions = components - (flag & 1);
if (newBalance <= L) {
Increase(dp[i + 1][components][newBalance][flag], Multiply(validPositions, dp[i][components][balance][flag]));
}
// append to the right side of a component
newBalance = balance + totalBorders * delta;
validPositions = components - ((flag & 2) > 0);
if (newBalance <= L) {
Increase(dp[i + 1][components][newBalance][flag], Multiply(validPositions, dp[i][components][balance][flag]));
}
// merge two components
newBalance = balance + totalBorders * delta;
validPositions = components - 1;
if (newBalance <= L) {
Increase(dp[i + 1][components - 1][newBalance][flag], Multiply(validPositions, dp[i][components][balance][flag]));
}
}
}
}
}
int answer = 0;
for (int balance = 0; balance <= L; ++balance) {
Increase(answer, dp[n + 1][1][balance][3]);
}
cout << answer;
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... |