Submission #521487

#TimeUsernameProblemLanguageResultExecution timeMemory
521487tengiz05Skyscraper (JOI16_skyscraper)C++17
100 / 100
329 ms2892 KiB
#include <bits/stdc++.h>

constexpr int P = 1E9 + 7;
using i64 = long long;
// assume -P <= x < 2P
int norm(int x) {
    if (x < 0) {
        x += P;
    }
    if (x >= P) {
        x -= P;
    }
    return x;
}
template<class T>
T power(T a, int b) {
    T res = 1;
    for (; b; b /= 2, a *= a) {
        if (b % 2) {
            res *= a;
        }
    }
    return res;
}
struct Z {
    int x;
    Z(int x = 0) : x(norm(x)) {}
    int val() const {
        return x;
    }
    Z operator-() const {
        return Z(norm(P - x));
    }
    Z inv() const {
        assert(x != 0);
        return power(*this, P - 2);
    }
    Z &operator*=(const Z &rhs) {
        x = i64(x) * rhs.x % P;
        return *this;
    }
    Z &operator+=(const Z &rhs) {
        x = norm(x + rhs.x);
        return *this;
    }
    Z &operator-=(const Z &rhs) {
        x = norm(x - rhs.x);
        return *this;
    }
    Z &operator/=(const Z &rhs) {
        return *this *= rhs.inv();
    }
    friend Z operator*(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res *= rhs;
        return res;
    }
    friend Z operator+(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res += rhs;
        return res;
    }
    friend Z operator-(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res -= rhs;
        return res;
    }
    friend Z operator/(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res /= rhs;
        return res;
    }
};

constexpr int N = 1005;
Z dp[2][105][3][N + 1];

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n, L;
    std::cin >> n >> L;
    
    std::vector<int> a(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }
    std::sort(a.begin(), a.end());
    
    if (n == 1) {
        std::cout << 1 << "\n";
        return 0;
    }
    
    int cur = 0;
    dp[cur][0][0][0] = 1;
    for (int i = 0; i < n; i++) {
        int x = i == 0 ? 0 : a[i] - a[i - 1];
        cur ^= 1;
        std::memset(dp[cur], 0, sizeof dp[cur]);
        for (int c = 0; c < n; c++) {
            for (int j = 0; j <= N; j++) {
                if (j + 2 * c * x <= N) {
                    int to = j + 2 * c * x;
                    dp[cur][c + 1][0][to] += (c + 1) * dp[cur ^ 1][c][0][j];
                    dp[cur][c + 1][1][to] += 2 * dp[cur ^ 1][c][0][j];
                    if (c > 0) {
                        dp[cur][c][1][to] += 2 * dp[cur ^ 1][c][0][j];
                        dp[cur][c][0][to] += 2 * c * dp[cur ^ 1][c][0][j];
                    }
                    if (c > 1) {
                        dp[cur][c - 1][0][to] += (c - 1) * dp[cur ^ 1][c][0][j];
                    }
                }
                
                if (c > 0 && j + (2 * c - 1) * x <= N) {
                    int to = j + (2 * c - 1) * x;
                    dp[cur][c + 1][1][to] += c * dp[cur ^ 1][c][1][j];
                    dp[cur][c + 1][2][to] += dp[cur ^ 1][c][1][j];
                    if (c > 0) {
                        dp[cur][c][2][to] += dp[cur ^ 1][c][1][j];
                        dp[cur][c][1][to] += (2 * c - 1) * dp[cur ^ 1][c][1][j];
                    }
                    if (c > 1) {
                        dp[cur][c - 1][1][to] += (c - 1) * dp[cur ^ 1][c][1][j];
                    }
                }
                
                if (c > 1 && j + (2 * c - 2) * x <= N) {
                    int to = j + (2 * c - 2) * x;
                    dp[cur][c + 1][2][to] += (c - 1) * dp[cur ^ 1][c][2][j];
                    if (c > 0) {
                        dp[cur][c][2][to] += (2 * c - 2) * dp[cur ^ 1][c][2][j];
                    }
                    if (c > 1) {
                        dp[cur][c - 1][2][to] += (c - 1) * dp[cur ^ 1][c][2][j];
                    }
                }
            }
        }
    }
    
    std::cout << std::accumulate(dp[cur][1][2], dp[cur][1][2] + L + 1, Z(0)).val() << "\n";
    
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...