結果

問題 No.1574 Swap and Repaint
ユーザー KoDKoD
提出日時 2021-06-26 20:17:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,916 ms / 10,000 ms
コード長 5,391 bytes
コンパイル時間 4,159 ms
コンパイル使用メモリ 246,140 KB
実行使用メモリ 21,264 KB
最終ジャッジ日時 2024-04-14 13:48:37
合計ジャッジ時間 26,537 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 2 ms
6,816 KB
testcase_04 AC 2 ms
6,812 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 5 ms
6,944 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 3 ms
6,940 KB
testcase_12 AC 3 ms
6,940 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 AC 3 ms
6,940 KB
testcase_15 AC 4 ms
6,944 KB
testcase_16 AC 3 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 303 ms
8,624 KB
testcase_19 AC 1,807 ms
20,284 KB
testcase_20 AC 477 ms
9,240 KB
testcase_21 AC 271 ms
7,992 KB
testcase_22 AC 224 ms
7,616 KB
testcase_23 AC 38 ms
6,944 KB
testcase_24 AC 1,504 ms
18,356 KB
testcase_25 AC 671 ms
12,160 KB
testcase_26 AC 12 ms
6,940 KB
testcase_27 AC 177 ms
7,132 KB
testcase_28 AC 1,909 ms
21,136 KB
testcase_29 AC 1,903 ms
21,132 KB
testcase_30 AC 1,915 ms
21,140 KB
testcase_31 AC 1,907 ms
21,264 KB
testcase_32 AC 1,916 ms
21,132 KB
testcase_33 AC 1,898 ms
21,132 KB
testcase_34 AC 1,903 ms
21,056 KB
testcase_35 AC 1,912 ms
21,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/convolution>
#include <atcoder/modint>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

class rep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { ++itr; }
        constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
        constexpr usize operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr rep(const usize first, const usize last) noexcept : first(first), last(std::max(first, last)) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

constexpr u64 ceil_log2(const u64 x) {
    u64 e = 0;
    while (((u64)1 << e) < x) ++e;
    return e;
}

template <class F> class AutoReallocation {
    using R = typename decltype(std::declval<F>()((usize)0))::value_type;

    F func;
    mutable std::vector<R> data;

  public:
    explicit AutoReallocation(F&& f) : func(std::forward<F>(f)), data() {}

    void reserve(const usize size) const {
        if (data.size() < size) data = func(((usize)1 << ceil_log2(size)));
    }
    R operator[](const usize i) const {
        reserve(i + 1);
        return data[i];
    }
};

template <class F> decltype(auto) auto_realloc(F&& f) {
    using G = std::decay_t<F>;
    return AutoReallocation<G>(std::forward<G>(f));
}

template <class M> struct ModintUtil {
    static inline const auto fact = auto_realloc([](const usize n) {
        std::vector<M> ret(n);
        ret[0] = M(1);
        for (const usize i : rep(1, n)) {
            ret[i] = ret[i - 1] * M(i);
        }
        return ret;
    });
    static inline const auto inv = auto_realloc([](const usize n) {
        std::vector<M> ret(n);
        if (n == 1) return ret;
        ret[1] = M(1);
        for (const usize i : rep(2, n)) {
            ret[i] = -M(M::mod() / i) * ret[M::mod() % i];
        }
        return ret;
    });
    static inline const auto inv_fact = auto_realloc([](const usize n) {
        std::vector<M> ret(n);
        ret[0] = M(1);
        for (const usize i : rep(1, n)) {
            ret[i] = ret[i - 1] * inv[i];
        }
        return ret;
    });
    static M binom(const usize n, const usize k) {
        assert(k <= n);
        return fact[n] * inv_fact[n - k] * inv_fact[k];
    }
    static M factpow(const usize n, const usize k) {
        assert(k <= n);
        return fact[n] * inv_fact[n - k];
    }
    static M homo(const usize n, const usize k) {
        if (n == 0 and k == 0) return M(1);
        return binom(n + k - 1, k);
    }
};

template <class T> using Vec = std::vector<T>;

using Fp = atcoder::modint998244353;
using Util = ModintUtil<Fp>;

const auto INV2 = Fp(1) / Fp(2);

void main_() {
    usize N;
    std::cin >> N;
    Vec<Fp> A(N);
    for (auto& x : A) {
        u32 y;
        std::cin >> y;
        x = y;
    }
    Vec<Fp> C(N);
    {
        Vec<Fp> more(N);
        for (const auto i : rep(0, N)) {
            more[i] = Util::factpow(N - 1, (N - 1) - i);
        }
        Vec<Fp> pow2(2 * N);
        pow2[0] = 1;
        for (const auto i : rep(1, 2 * N)) {
            pow2[i] = pow2[i - 1] * 2;
        }
        Fp sum;
        for (const auto i : rep(0, N - 1)) {
            sum += (more[i] - more[i + 1] * INV2) * pow2[2 * ((N - 1) - i) + i];
            C[i] = sum;
        }
        for (const auto i : rep(0, N)) {
            C[N - 1] += more[i] * pow2[2 * ((N - 1) - i) + i];
        }
    }
    A.resize(2 * N);
    C.resize(2 * N);
    for (const auto i : rep(0, N)) {
        A[2 * N - i - 1] = A[i];
        C[2 * N - i - 1] = C[i];
    }
    const auto N2 = 2 * N;
    std::reverse(C.begin(), C.end());
    auto F = atcoder::convolution(A, C);
    for (const auto i : rep(N2, F.size())) {
        F[i - N2] += F[i];
    }
    F.resize(N2);
    std::reverse(F.begin(), F.end());
    const usize K = std::min(N2, (usize)(5 * std::sqrt(N)));
    Vec<Vec<Fp>> D_base(K + 1);
    D_base[0] = {Fp(1)};
    for (const auto i : rep(1, K + 1)) {
        D_base[i].resize(D_base[i - 1].size() + 2);
        for (const auto j : rep(0, D_base[i - 1].size())) {
            D_base[i][j] += D_base[i - 1][j];
            D_base[i][j + 1] += D_base[i - 1][j] * Fp((isize)N - 3);
            D_base[i][j + 2] += D_base[i - 1][j];
        }
    }
    Vec<typename Vec<Fp>::const_iterator> D(K);
    for (const auto i : rep(0, K)) {
        D[i] = D_base[i].cbegin() + (D_base[i].size() / 2);
    }
    for (const auto i : rep(0, N + 1)) {
        if (i % K == 0 and i != 0) {
            const auto tmp = atcoder::convolution(F, D_base[K]);
            F.assign(N2, 0);
            for (const auto i : rep(0, tmp.size())) {
                F[(i + N2 - K) % N2] += tmp[i];
            }
        }
        Fp ans;
        const isize width = i % K;
        for (isize i = -width; i <= width; i += 1) {
            ans += F[i < 0 ? i + N2 : i] * D[width][i];
        }
        std::cout << (ans * INV2).val() << '\n';
    }
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    main_();
    return 0;
}
0