Submission #1248310

#TimeUsernameProblemLanguageResultExecution timeMemory
1248310ll931110Skyscraper (JOI16_skyscraper)C++20
15 / 100
1 ms840 KiB
#ifdef ONLINE_JUDGE
    #include <bits/stdc++.h>
#endif

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <stack>
#include <map>
#include <queue>
#include <vector>
#define maxn 105
#define maxk 1005
using namespace std;

template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
 
constexpr unsigned MOD = 1'000'000'007;
using Mint = ModInt<MOD>;

Mint dp[maxn][maxn][maxk][3];
int n, K;
int a[maxn];
int inf = 1e4;

Mint solve() {
    // dp[i][j][k][m] = number of ways to fill i numbers, there are j segments, diff of k and l ends have been filled so far.
    a[n + 1] = inf;

    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 <= K; k++) {
                for (int m = 0; m <= 2; m++) {
                    int cost_diff = (2 * j - m) * (a[i + 1] - a[i]);
                    if (cost_diff > k) {
                        continue;
                    }

                    // Case 1: put a[i] in a new segment
                    dp[i][j][k][m] += dp[i - 1][j - 1][k - cost_diff][m];

                    // Case 2: put a[i] in a new segment at the endpoint
                    if (m > 0) {
                        dp[i][j][k][m] += (3 - m) * dp[i - 1][j - 1][k - cost_diff][m - 1];
                    }

                    // Case 3: extending a[i] from a segment, not at endpoint
                    dp[i][j][k][m] += (2 * j - m) * dp[i - 1][j][k - cost_diff][m];

                    // Case 4: extending a[i] from a segment, using endpoint
                    if (m == 1) {
                        dp[i][j][k][m] += 2 * j * dp[i - 1][j][k - cost_diff][m - 1];
                    } else if (m == 2) {
                        if (i == n) {
                            dp[i][j][k][m] += dp[i - 1][j][k - cost_diff][m - 1];
                        } else if (j > 1) {
                            dp[i][j][k][m] += (j - 1) * dp[i - 1][j][k - cost_diff][m - 1];
                        }
                    }

                    // Case 5: join two segments together
                    if (m == 2) {
                        if (i == n) {
                            dp[i][j][k][m] += dp[i - 1][j + 1][k - cost_diff][m];
                        } else {
                            dp[i][j][k][m] += j * (j - 1) * dp[i - 1][j + 1][k - cost_diff][m];
                        }
                    } else if (m == 1) {
                        dp[i][j][k][m] += j * j * dp[i - 1][j + 1][k - cost_diff][m];
                    } else {
                        dp[i][j][k][m] += j * (j + 1) * dp[i - 1][j + 1][k - cost_diff][m];
                    }
                }
            }
        }
    }

    Mint ans = 0;
    for (int k = 0; k <= K; k++) {
        ans += dp[n][1][k][2];
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> K;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    cout << solve() << endl;

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...