Submission #509805

# Submission time Handle Problem Language Result Execution time Memory
509805 2022-01-14T10:17:39 Z KoD Magneti (COCI21_magneti) C++17
110 / 110
135 ms 4268 KB
#include <bits/stdc++.h>

using std::vector;
using std::array;
using std::pair;
using std::tuple;

constexpr int MOD = 1000000007;

struct Fp {
    int x;
    Fp(int x = 0) : x(x) {}
    Fp& operator+=(const Fp& t) {
        if ((x += t.x) >= MOD) x -= MOD;
        return *this;
    }
    Fp& operator*=(const Fp& t) {
        x = (long long)x * t.x % MOD;
        return *this;
    }
    Fp operator+(const Fp& t) const {
        return Fp(*this) += t;
    }
    Fp operator*(const Fp& t) const {
        return Fp(*this) *= t;
    }
    Fp pow(int exp) const {
        Fp ret(1), mul(*this);
        while (exp > 0) {
            if (exp & 1) ret *= mul;
            mul *= mul;
            exp >>= 1;
        }
        return ret;
    }
    Fp inv() const {
        return pow(MOD - 2);
    }
};

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N, L;
    std::cin >> N >> L;
    vector<int> R(N);
    for (auto& x : R) {
        std::cin >> x;
    }
    std::sort(R.begin(), R.end());
    vector dp(1, vector<Fp>(L));
    dp[0][0] = 1;
    for (int k = 1; k < N; ++k) {
        vector next(k + 1, vector<Fp>(L));
        for (int i = 0; i < k; ++i) {
            for (int j = 0; j < L; ++j) {
                if (j + R[k] < L) {
                    next[i][j + R[k]] += dp[i][j] * (i * 2 + 2);
                }
                if (i > 0 and j + 2 * R[k] < L) {
                    next[i - 1][j + 2 * R[k]] += dp[i][j] * i;
                }
                next[i + 1][j] += dp[i][j] * (i + 2);
            }
        }
        dp = std::move(next);
    }
    vector<Fp> fact(L + 1);
    fact[0] = 1;
    for (int i = 1; i <= L; ++i) {
        fact[i] = fact[i - 1] * i;
    }
    vector<Fp> inv_fact(L + 1);
    inv_fact[L] = fact[L].inv();
    for (int i = L; i >= 1; --i) {
        inv_fact[i - 1] = inv_fact[i] * i;
    }
    const auto binom = [&](const int n, const int r) {
        if (n < r) return Fp(0);
        return fact[n] * inv_fact[r] * inv_fact[n - r];
    };
    Fp ans = 0;
    for (int i = N - 1; i < L; ++i) {
        const int n = L - (i - (N - 1));
        ans += dp[0][i] * binom(n, N);
    }
    std::cout << ans.x << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 122 ms 4268 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 4 ms 972 KB Output is correct
4 Correct 1 ms 588 KB Output is correct
5 Correct 6 ms 688 KB Output is correct
6 Correct 33 ms 2220 KB Output is correct
7 Correct 30 ms 2376 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 11 ms 1484 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1072 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 4 ms 1100 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 4 ms 1084 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 5 ms 1100 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 316 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 4268 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 4 ms 972 KB Output is correct
4 Correct 1 ms 588 KB Output is correct
5 Correct 6 ms 688 KB Output is correct
6 Correct 33 ms 2220 KB Output is correct
7 Correct 30 ms 2376 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 11 ms 1484 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 4 ms 1072 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 4 ms 1100 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 4 ms 1084 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 5 ms 1100 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 2 ms 316 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 308 KB Output is correct
26 Correct 0 ms 204 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 123 ms 4240 KB Output is correct
32 Correct 78 ms 2828 KB Output is correct
33 Correct 127 ms 4184 KB Output is correct
34 Correct 24 ms 1716 KB Output is correct
35 Correct 124 ms 4192 KB Output is correct
36 Correct 8 ms 1100 KB Output is correct
37 Correct 135 ms 4220 KB Output is correct
38 Correct 20 ms 972 KB Output is correct
39 Correct 120 ms 4136 KB Output is correct
40 Correct 31 ms 2284 KB Output is correct
41 Correct 116 ms 4164 KB Output is correct
42 Correct 1 ms 460 KB Output is correct
43 Correct 109 ms 4164 KB Output is correct
44 Correct 10 ms 1228 KB Output is correct
45 Correct 110 ms 4244 KB Output is correct
46 Correct 1 ms 460 KB Output is correct
47 Correct 16 ms 2148 KB Output is correct
48 Correct 8 ms 956 KB Output is correct
49 Correct 1 ms 716 KB Output is correct
50 Correct 0 ms 332 KB Output is correct