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 = 1e9 + 7;
const int MAXN = 105;
int n, L, a[MAXN];
long long dp[MAXN][MAXN][1005][3];
int main(){
cin >> n >> L;
for(int i = 1; i <= n; ++i) cin >> a[i];
sort(a + 1, a + n + 1);
if(n == 1){
cout << 1; return 0;
}
dp[0][0][0][0] = 1;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= i; ++j){
for(int k = 0; k <= L; ++k){
for(int m = 0; m < 3; ++m){
// first i numbers, j components, k is sum, m endpoints filled
int dif = (a[i + 1] - a[i]) * (2 * j - m);
if((i != n && m > j) || k < dif) continue;
// create new component that isnt endpoint
dp[i][j][k][m] += dp[i - 1][j - 1][k - dif][m];
// create new component that is endpoint
if(m) dp[i][j][k][m] += dp[i - 1][j - 1][k - dif][m - 1] * (3 - m);
// append on edge of component
dp[i][j][k][m] += dp[i - 1][j][k - dif][m] * (2 * j - m);
// append end on component
if(m == 1) dp[i][j][k][m] += dp[i - 1][j][k - dif][m - 1] * 2 * j;
else if(m == 2){
if(i < n) dp[i][j][k][m] += dp[i - 1][j][k - dif][m - 1] * (j - 1);
else dp[i][j][k][m] += dp[i - 1][j][k - dif][m - 1];
}
// merge two components
if(i == n){
dp[i][j][k][m] += dp[i - 1][j + 1][k - dif][m];
} else {
dp[i][j][k][m] += dp[i - 1][j + 1][k - dif][m] * (j * (j + 1) - m * j);
}
dp[i][j][k][m] %= MOD;
}
}
}
}
long long ans = 0;
for(int i = 0; i <= L; ++i) ans += dp[n][1][i][2];
cout << ans % MOD;
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... |