#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define vt vector
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
#define ub upper_bound
#define lb lower_bound
#define db double
#define ld long db
#define ll long long
#define ull unsigned long long
#define vi vt<int>
#define vvi vt<vi>
#define vvvi vt<vvi>
#define pii pair<int, int>
#define vpii vt<pii>
#define vvpii vt<vpii>
#define vll vt<ll>
#define vvll vt<vll>
#define pll pair<ll, ll>
#define vpll vt<pll>
#define vvpll vt<vpll>
#define ar(x) array<int, x>
#define var(x) vt<ar(x)>
#define vvar(x) vt<var(x)>
#define al(x) array<ll, x>
#define vall(x) vt<al(x)>
#define vvall(x) vt<vall(x)>
#define vs vt<string>
#define pb push_back
#define ff first
#define ss second
#define rsz resize
#define sum(x) (ll)accumulate(all(x), 0LL)
#define srt(x) sort(all(x))
#define srtR(x) sort(allr(x))
#define srtU(x) sort(all(x)), (x).erase(unique(all(x)), (x).end())
#define rev(x) reverse(all(x))
#define MAX(a) *max_element(all(a))
#define MIN(a) *min_element(all(a))
#define SORTED(x) is_sorted(all(x))
#define ROTATE(a, p) rotate(begin(a), begin(a) + p, end(a))
#define i128 __int128
#define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#if defined(LOCAL) && __has_include("debug.h")
#include "debug.h"
#else
#define debug(...)
#define startClock
#define endClock
inline void printMemoryUsage() {}
#endif
template<class T> using max_heap = priority_queue<T>; template<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename T, size_t N> istream& operator>>(istream& is, array<T, N>& arr) { for (size_t i = 0; i < N; i++) { is >> arr[i]; } return is; }
template<typename T, size_t N> istream& operator>>(istream& is, vector<array<T, N>>& vec) { for (auto &arr : vec) { is >> arr; } return is; }
template<typename T1, typename T2> istream &operator>>(istream& in, pair<T1, T2>& input) { return in >> input.ff >> input.ss; }
template<typename T> istream &operator>>(istream &in, vector<T> &v) { for (auto &el : v) in >> el; return in; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const static ll INF = 4e18 + 10;
const static int inf = 1e9 + 100;
const static int MX = 1e5 + 5;
template <int MOD>
struct mod_int {
int value;
mod_int(ll v = 0) { value = int(v % MOD); if (value < 0) value += MOD; }
mod_int& operator+=(const mod_int &other) { value += other.value; if (value >= MOD) value -= MOD; return *this; }
mod_int& operator-=(const mod_int &other) { value -= other.value; if (value < 0) value += MOD; return *this; }
mod_int& operator*=(const mod_int &other) { value = int((ll)value * other.value % MOD); return *this; }
mod_int pow(ll p) const { mod_int ans(1), a(*this); while (p) { if (p & 1) ans *= a; a *= a; p /= 2; } return ans; }
mod_int inv() const { return pow(MOD - 2); }
mod_int& operator/=(const mod_int &other) { return *this *= other.inv(); }
friend mod_int operator+(mod_int a, const mod_int &b) { a += b; return a; }
friend mod_int operator-(mod_int a, const mod_int &b) { a -= b; return a; }
friend mod_int operator*(mod_int a, const mod_int &b) { a *= b; return a; }
friend mod_int operator/(mod_int a, const mod_int &b) { a /= b; return a; }
bool operator==(const mod_int &other) const { return value == other.value; }
bool operator!=(const mod_int &other) const { return value != other.value; }
bool operator<(const mod_int &other) const { return value < other.value; }
bool operator>(const mod_int &other) const { return value > other.value; }
bool operator<=(const mod_int &other) const { return value <= other.value; }
bool operator>=(const mod_int &other) const { return value >= other.value; }
mod_int operator&(const mod_int &other) const { return mod_int((ll)value & other.value); }
mod_int& operator&=(const mod_int &other) { value &= other.value; return *this; }
mod_int operator|(const mod_int &other) const { return mod_int((ll)value | other.value); }
mod_int& operator|=(const mod_int &other) { value |= other.value; return *this; }
mod_int operator^(const mod_int &other) const { return mod_int((ll)value ^ other.value); }
mod_int& operator^=(const mod_int &other) { value ^= other.value; return *this; }
mod_int operator<<(int shift) const { return mod_int(((ll)value << shift) % MOD); }
mod_int& operator<<=(int shift) { value = int(((ll)value << shift) % MOD); return *this; }
mod_int operator>>(int shift) const { return mod_int(value >> shift); }
mod_int& operator>>=(int shift) { value >>= shift; return *this; }
mod_int& operator++() { ++value; if (value >= MOD) value = 0; return *this; }
mod_int operator++(int) { mod_int temp = *this; ++(*this); return temp; }
mod_int& operator--() { if (value == 0) value = MOD - 1; else --value; return *this; }
mod_int operator--(int) { mod_int temp = *this; --(*this); return temp; }
explicit operator ll() const { return (ll)value; }
explicit operator int() const { return value; }
explicit operator db() const { return (db)value; }
friend mod_int operator-(const mod_int &a) { return mod_int(0) - a; }
friend ostream& operator<<(ostream &os, const mod_int &a) { os << a.value; return os; }
friend istream& operator>>(istream &is, mod_int &a) { ll v; is >> v; a = mod_int(v); return is; }
};
const static int MOD = 1e9 + 7;
using mint = mod_int<MOD>;
using vmint = vt<mint>;
using vvmint = vt<vmint>;
using vvvmint = vt<vvmint>;
using pmm = pair<mint, mint>;
using vpmm = vt<pmm>;
int dp[101][101][1005][3];
void solve() {
int n, l; cin >> n >> l;
vi a(n); cin >> a;
srt(a);
if(n == 1) {
cout << 1 << '\n';
return;
}
if(n == 2) {
cout << (a[1] - a[0] <= l ? 2 : 0) << '\n';
return;
}
memset(dp, -1, sizeof(dp));
auto dfs = [&](auto& dfs, ll i = 0, ll j = 0, ll k = 0, ll s = 0) -> int {
if(j < 1 || j > n || k > l || s > 2) return 0;
if(i == n - 1) return j == 1 && s == 2;
auto& res = dp[i][j][k][s];
if(res != -1) return res;
res = 0;
mint now = 0;
ll edge = (2 * j - s);
k += edge * (a[i + 1] - a[i]);
if(k <= l) {
if(s == 0) {
now += (j - 1) * dfs(dfs, i + 1, j - 1, k, 0); // j - 1 choices to close the component
now += (j + 1) * dfs(dfs, i + 1, j + 1, k, 0); // j + 1 position to insert this in
now += (mint)edge * dfs(dfs, i + 1, j, k, 0); // edge choices to keep the same component
now += 2 * dfs(dfs, i + 1, j, k, 1); // ?comp1_comp2_comp3_comp4? 2 choices each left_most and right_most
now += 2 * dfs(dfs, i + 1, j + 1, k, 1);
} else if(s == 1) {
now += (j - 1) * dfs(dfs, i + 1, j - 1, k, 1);
now += j * dfs(dfs, i + 1, j + 1, k, 1);
now += (mint)edge * dfs(dfs, i + 1, j, k, 1);
now += dfs(dfs, i + 1, j, k, 2);
now += dfs(dfs, i + 1, j + 1, k, 2);
} else {
now += (j - 1) * dfs(dfs, i + 1, j - 1, k, 2);
now += (j - 1) * dfs(dfs, i + 1, j + 1, k, 2);
now += edge * dfs(dfs, i + 1, j, k, 2);
}
}
res = int(now);
return res;
};
mint res = dfs(dfs, 0, 1, 0, 0);
res += 2 * dfs(dfs, 0, 1, 0, 1);
cout << res << '\n';
}
signed main() {
IOS;
startClock
int t = 1;
//cin >> t;
for(int i = 1; i <= t; i++) {
//cout << "Case #" << i << ": ";
solve();
}
endClock;
printMemoryUsage();
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... |