Submission #381803

# Submission time Handle Problem Language Result Execution time Memory
381803 2021-03-26T00:43:19 Z Matrix_code A Huge Tower (CEOI10_tower) C++17
100 / 100
138 ms 11372 KB
#include <bits/stdc++.h>
using namespace std;

template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
    return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<
                                    !is_same<T_container, string>::value,
                                    typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v) {
    os << '{';
    string sep;
    for (const T &x : v) os << sep << x, sep = ", ";
    return os << '}';
}

void dbg_out() { cerr << "]" << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
    cerr << H;
    if (sizeof...(T)) cerr << ", ";
    dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...)                                                       \
    cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", \
        dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

/**
 * Author: Repon Kumar Roy
 * Date: 2021-03-26
 * Task: CEOI10_tower
 */

#include <bits/stdc++.h>
using namespace std;

#define ll        long long
#define REP(i, n) for (int i = 0; i < (n); i++)

#include <bits/stdc++.h>
using namespace std;

template <const int &MOD> struct modint {
    int val;

    modint(int64_t v = 0) {
        if (v < 0) v = v % MOD + MOD;
        if (v >= MOD) v %= MOD;
        val = int(v);
    }

    modint(uint64_t v) {
        if (v >= MOD) v %= MOD;
        val = int(v);
    }

    modint(int v) : modint(int64_t(v)) {}
    modint(unsigned v) : modint(uint64_t(v)) {}

    explicit operator int() const { return val; }
    explicit operator unsigned() const { return val; }
    explicit operator int64_t() const { return val; }
    explicit operator uint64_t() const { return val; }
    explicit operator double() const { return val; }
    explicit operator long double() const { return val; }

    modint &operator+=(const modint &other) {
        val -= MOD - other.val;
        if (val < 0) val += MOD;
        return *this;
    }

    modint &operator-=(const modint &other) {
        val -= other.val;
        if (val < 0) val += MOD;
        return *this;
    }

    static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
#if !defined(_WIN32) || defined(_WIN64)
        return unsigned(x % m);
#endif
        // Optimized mod for Codeforces 32-bit machines.
        // x must be less than 2^32 * m for this to work, so that x / m fits in
        // an unsigned 32-bit int.
        unsigned x_high = unsigned(x >> 32), x_low = unsigned(x);
        unsigned quot, rem;
        asm("divl %4\n"
            : "=a"(quot), "=d"(rem)
            : "d"(x_high), "a"(x_low), "r"(m));
        return rem;
    }

    modint &operator*=(const modint &other) {
        val = fast_mod(uint64_t(val) * other.val);
        return *this;
    }

    modint &operator/=(const modint &other) { return *this *= other.inv(); }

    friend modint operator+(const modint &a, const modint &b) {
        return modint(a) += b;
    }
    friend modint operator-(const modint &a, const modint &b) {
        return modint(a) -= b;
    }
    friend modint operator*(const modint &a, const modint &b) {
        return modint(a) *= b;
    }
    friend modint operator/(const modint &a, const modint &b) {
        return modint(a) /= b;
    }

    modint &operator++() {
        val = val == MOD - 1 ? 0 : val + 1;
        return *this;
    }

    modint &operator--() {
        val = val == 0 ? MOD - 1 : val - 1;
        return *this;
    }

    modint operator++(int) {
        modint before = *this;
        ++*this;
        return before;
    }
    modint operator--(int) {
        modint before = *this;
        --*this;
        return before;
    }

    modint operator-() const { return val == 0 ? 0 : MOD - val; }

    friend bool operator==(const modint &a, const modint &b) {
        return a.val == b.val;
    }
    friend bool operator!=(const modint &a, const modint &b) {
        return a.val != b.val;
    }
    friend bool operator<(const modint &a, const modint &b) {
        return a.val < b.val;
    }
    friend bool operator>(const modint &a, const modint &b) {
        return a.val > b.val;
    }
    friend bool operator<=(const modint &a, const modint &b) {
        return a.val <= b.val;
    }
    friend bool operator>=(const modint &a, const modint &b) {
        return a.val >= b.val;
    }

    static const int SAVE_INV = int(1e6) + 5;
    static modint save_inv[SAVE_INV];

    static void prepare_inv() {
        // Make sure MOD is prime, which is necessary for the inverse algorithm
        // below.
        for (int64_t p = 2; p * p <= MOD; p += p % 2 + 1) assert(MOD % p != 0);

        save_inv[0] = 0;
        save_inv[1] = 1;

        for (int i = 2; i < SAVE_INV; i++)
            save_inv[i] = save_inv[MOD % i] * (MOD - MOD / i);
    }

    modint inv() const {
        if (save_inv[1] == 0) prepare_inv();

        if (val < SAVE_INV) return save_inv[val];

        modint product = 1;
        int v          = val;

        while (v >= SAVE_INV) {
            product *= MOD - MOD / v;
            v = MOD % v;
        }

        return product * save_inv[v];
    }

    modint pow(int64_t p) const {
        if (p < 0) return inv().pow(-p);

        modint a = *this, result = 1;

        while (p > 0) {
            if (p & 1) result *= a;

            p >>= 1;

            if (p > 0) a *= a;
        }

        return result;
    }

    friend ostream &operator<<(ostream &os, const modint &m) {
        return os << m.val;
    }
};
const int mod = 1e9 + 9;
using mint    = modint<mod>;
void solve() {
    int n, d;
    cin >> n >> d;
    vector<int> a(n), cnt(n);
    for (int i = 0; i < n; i++) { cin >> a[i]; }
    sort(a.begin(), a.end());
    for (int i = n - 1, j = n - 1; i >= 0; i--) {
        while (j >= 0 && a[i] - a[j] <= d) j--;
        cnt[i] = i - j - 1;
    }
    mint ans = 1;
    for (int i = 0; i < n; i++) { ans *= (1 + cnt[i]); }
    printf("%d\n", ans.val);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 1260 KB Output is correct
2 Correct 12 ms 1260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 4716 KB Output is correct
2 Correct 52 ms 4716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 116 ms 11372 KB Output is correct
2 Correct 138 ms 10748 KB Output is correct