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 ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
using namespace std;
const int mxN = 5e5 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
int n;
int lim;
int a[mxN];
int diff[mxN];
int dp[107][107][1007][3];
void add(int &a, int b)
{
a += b;
if(a >= mod)
a -= mod;
}
void solve()
{
cin >> n >> lim;
for(int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + 1 + n);
for(int i = 1; i <= n; i++) diff[i] = a[i] - a[i - 1];
dp[1][1][0][0] = 1;
dp[1][1][0][1] = 2;
dp[1][1][0][2] = 1;
for(int i = 1; i < n; i++) {
for(int j = 0; j <= i; j++) {
for(int sum = 0; sum <= lim; sum++) {
for(int k = 0; k < 3; k++) {
if(dp[i][j][sum][k] == 0) continue;
int cur = dp[i][j][sum][k];
int newsum = sum + (j * 2 - k) * diff[i + 1];
if(newsum > lim) continue;
// case 1 create comp
add(dp[i + 1][j + 1][newsum][k], 1LL * cur * (j + 1 - k) % mod);
// fill begin or end
if(k < 2) {
add(dp[i + 1][j + 1][newsum][k + 1], 1LL * (2 - k) * cur % mod);
add(dp[i + 1][j][newsum][k + 1], 1LL * (2 - k) * cur % mod);
}
// case 2 merge comp
if(j > 1) {
add(dp[i + 1][j - 1][newsum][k], 1LL * cur * (j - 1) % mod);
}
// case 3 add
add(dp[i + 1][j][newsum][k], 1LL * cur * (j * 2 - k) % mod);
}
}
}
}
int res = 0;
for(int i = 0; i <= lim; i++) add(res, dp[n][1][i][2]);
cout << res;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen(TASK".inp" , "r" , stdin);
//freopen(TASK".out" , "w" , stdout);
int tc = 1;
//cin >> tc;
while(tc--) {
solve();
}
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... |