結果

問題 No.1068 #いろいろな色 / Red and Blue and more various colors (Hard)
ユーザー kcvlexkcvlex
提出日時 2020-05-31 01:40:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 14,195 bytes
コンパイル時間 1,992 ms
コンパイル使用メモリ 160,808 KB
実行使用メモリ 58,388 KB
最終ジャッジ日時 2024-11-14 07:07:38
合計ジャッジ時間 30,273 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
36,216 KB
testcase_01 AC 11 ms
36,096 KB
testcase_02 AC 11 ms
36,168 KB
testcase_03 AC 30 ms
36,468 KB
testcase_04 AC 20 ms
36,540 KB
testcase_05 AC 21 ms
36,724 KB
testcase_06 AC 20 ms
36,652 KB
testcase_07 AC 20 ms
36,452 KB
testcase_08 AC 21 ms
36,552 KB
testcase_09 AC 21 ms
36,656 KB
testcase_10 AC 15 ms
36,408 KB
testcase_11 AC 19 ms
36,384 KB
testcase_12 AC 15 ms
36,424 KB
testcase_13 AC 1,412 ms
58,276 KB
testcase_14 AC 1,418 ms
58,276 KB
testcase_15 AC 1,419 ms
58,304 KB
testcase_16 AC 1,413 ms
58,280 KB
testcase_17 AC 1,430 ms
58,284 KB
testcase_18 AC 1,398 ms
58,372 KB
testcase_19 AC 1,433 ms
58,372 KB
testcase_20 AC 1,411 ms
58,304 KB
testcase_21 AC 1,411 ms
58,332 KB
testcase_22 AC 1,398 ms
58,336 KB
testcase_23 AC 1,420 ms
58,312 KB
testcase_24 AC 1,408 ms
58,316 KB
testcase_25 AC 1,410 ms
58,336 KB
testcase_26 AC 1,413 ms
58,388 KB
testcase_27 AC 1,423 ms
58,284 KB
testcase_28 AC 1,409 ms
58,280 KB
testcase_29 AC 1,399 ms
58,320 KB
testcase_30 AC 1,374 ms
58,324 KB
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define CPP17
#include <limits>
#include <initializer_list>
#include <utility>
#include <bitset>
#include <tuple>
#include <type_traits>
#include <functional>
#include <string>
#include <array>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <iterator>
#include <algorithm>
#include <complex>
#include <random>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <regex>
#include <cassert>
#include <cstddef>
#ifdef CPP17
#include <variant>
#endif

// Yay!!
#define endl codeforces

// macros for iterator
#define ALL(v) std::begin(v), std::end(v)
#define ALLR(v) std::rbegin(v), std::rend(v)

// alias
using ll = std::int64_t;
using ull = std::uint64_t;
using pii = std::pair<int, int>;
using tii = std::tuple<int, int, int>;
using pll = std::pair<ll, ll>;
using tll = std::tuple<ll, ll, ll>;
template <typename T> using vec = std::vector<T>;
template <typename T> using vvec = vec<vec<T>>;

// variadic min/max
template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); }

// variadic chmin/chmax
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }

// multi demension array
template <typename T, std::size_t Head, std::size_t... Tail> struct multi_dim_array { using type = std::array<typename multi_dim_array<T, Tail...>::type, Head>; };
template <typename T, std::size_t Head> struct multi_dim_array<T, Head> { using type = std::array<T, Head>; };
template <typename T, std::size_t... Args> using mdarray = typename multi_dim_array<T, Args...>::type;

#ifdef CPP17
// fill container
template <typename T, typename F, typename... Args> 
void fill_seq(T &t, F f, Args... args) { if constexpr (std::is_invocable<F, Args...>::value) { t = f(args...); } else { for (ssize_t i = 0; i < t.size(); i++) fill_seq(t[i], f, args..., i); } }
#endif

// make multi dimension vector
template <typename T> vec<T> make_v(ssize_t sz) { return vec<T>(sz); }
template <typename T, typename... Tail> auto make_v(ssize_t hs, Tail&&... ts) { auto v = std::move(make_v<T>(std::forward<Tail>(ts)...)); return vec<decltype(v)>(hs, v); }

// init
namespace init__ { 
struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; 
}


namespace math {

template <typename T>
constexpr T pow(const T &n, ll k) {
    T ret = n.mul_id_ele();
    T cur = n;
    while (k) {
        if (k & 1) ret *= cur;
        cur *= cur;
        k /= 2;
    }
    return ret;
}

}

namespace math {

template <ll Mod>
struct Modint {

    constexpr Modint(ll x) noexcept : x((Mod + x % Mod) % Mod) { }
    
    constexpr Modint() noexcept : Modint(0) { }
    
    constexpr Modint<Mod> add_id_ele() const noexcept { 
        return Modint<Mod>(0); 
    }
    
    constexpr Modint<Mod> mul_id_ele() const noexcept {
        return Modint<Mod>(1); 
    }
    
    constexpr ll& value() noexcept { 
        return x; 
    }
    
    constexpr ll value() const noexcept {
        return x; 
    }

    constexpr Modint& operator +=(const Modint &oth) noexcept {
        x += oth.value();
        if (Mod <= x) x -= Mod;
        return *this;
    }

    constexpr Modint& operator -=(const Modint &oth) noexcept {
        x += Mod - oth.value();
        if (Mod <= x) x -= Mod;
        return *this;
    }

    constexpr Modint& operator *=(const Modint &oth) noexcept {
        x *= oth.value();
        x %= Mod;
        return *this;
    }

    constexpr Modint& operator /=(const Modint &oth) noexcept {
        x *= oth.inv().value();
        x %= Mod;
        return *this;
    }

    constexpr Modint operator +(const Modint &oth) const noexcept {
        return Modint(x) += oth;
    }

    constexpr Modint operator -(const Modint &oth) const noexcept {
        return Modint(x) -= oth;
    }

    constexpr Modint operator *(const Modint &oth) const noexcept {
        return Modint(x) *= oth;
    }

    constexpr Modint operator /(const Modint &oth) const noexcept {
        return Modint(x) /= oth;
    }

    constexpr Modint operator -() const noexcept {
        return Modint((x != 0) * (Mod - x)); 
    }

    template <typename T>
    constexpr typename std::enable_if<std::is_integral<T>::value, const Modint&>::type
    operator =(T t) noexcept {
        (*this) = Modint(std::forward<T>(t)); 
        return *this;
    }

    constexpr Modint inv() const noexcept {
        return ::math::pow(*this, Mod - 2);
    }

    constexpr ll mod() const noexcept {
        return Mod;
    }

private:
    ll x;
};

}
ssize_t ceil_pow2(ssize_t s) {
    ssize_t ret = 1;
    while (ret < s) ret *= 2;
    return ret;
}

namespace utility {

template <typename T, T... Args>
constexpr std::array<T, sizeof...(Args)> 
make_array(std::integer_sequence<T, Args...>) {
    return {{ Args... }};
}

template <typename, typename>
struct tuple_concat;

template <typename... Args1, typename... Args2>
struct tuple_concat<std::tuple<Args1...>, std::tuple<Args2...>> {
    using type = std::tuple<Args1..., Args2...>;
};

template <ll... N1, ll... N2>
constexpr auto concat_integer_sequence(std::integer_sequence<ll, N1...>, std::integer_sequence<ll, N2...>) {
    return std::integer_sequence<ll, N1..., N2...>();
}

template <ll Head, ll... Tail>
struct reverse_sequence {
    using head_type = std::integer_sequence<ll, Head>;
    using tail_type = typename reverse_sequence<Tail...>::type;
    using type = decltype(concat_integer_sequence(std::declval<tail_type>(), std::declval<head_type>()));
};

template <ll Head>
struct reverse_sequence<Head> {
    using type = std::integer_sequence<ll, Head>;
};

template <ll... N>
constexpr auto reverse_ns(std::integer_sequence<ll, N...>) {
    return typename reverse_sequence<N...>::type();
}

}

namespace math {

namespace ntt_helper {

class reverse_bit {
    vvec<ll> bits;

    // len = 2^n
    void build_rev_bit(std::size_t len, vec<ll> &v) {
        if (!v.empty()) return;
        uint64_t r = 0, s = 0, max_v = len;
        v.resize(len);
        for (auto &&ele : v) {
            // assert(r < max_v * 2);
            ele = s;
            r += 2;
            s ^= max_v - (max_v / (r & -r));
        }
        // assert(max_v * 2 <= r);
    }

    ll get_idx(std::size_t pow2) {
        ll i;
        for (i = 0; i < 64; i++) if (pow2 & (1ll << i)) break;
        return i;
    }

    void resize(ll idx) {
        if (bits.size() <= idx) bits.resize(idx + 1);
    }

public:
    const vec<ll>& get(std::size_t len) {
        const auto idx = get_idx(len);
        resize(idx);
        build_rev_bit(len, bits[idx]);
        return bits[idx];
    }
};

reverse_bit rb__;

template <ll Mod>
constexpr bool is_primitive_root(ll r) {
    Modint<Mod> mr(r);
    for (ll d = 2; d * d <= Mod; d++) {
        if ((Mod - 1) % d == 0) {
            if (pow(mr, d).value() == 1) return false;
            if (pow(mr, (Mod - 1) / d).value() == 1) return false;
        }
    }
    return true;
}

template <ll Mod>
constexpr ll find_primitive_root(ll r) {
    return (is_primitive_root<Mod>(r) ? r : find_primitive_root<Mod>(r + 1));
}

template <ll Mod>
constexpr ll find_primitive_root() {
    return find_primitive_root<Mod>(2);
}

template <ll Mod, ll Root, std::size_t Size>
struct root_pows_calculator {
    using mint = Modint<Mod>;
    using seq = std::make_integer_sequence<ll, Size>;
    using rseq = decltype(utility::reverse_ns(std::declval<seq>()));

    struct aux {
        mint m;
        ll k;

        constexpr aux(mint m, ll k) : m(m), k(k) { }

        constexpr mint solve() {
            return pow(m, 1ll << k); 
        }
    };

    constexpr static mint root = mint(Root);

    constexpr mint apply(ll k) {
        return aux(root, k).solve(); 
    }

    template <ll... K>
    constexpr auto calc(std::integer_sequence<ll, K...>) -> std::array<mint, Size> {
        return {{ apply(K)... }};
    }

    constexpr auto calc() {
        return calc(rseq());
    }
};

template <ll Mod, ll Root, std::size_t Size>
constexpr auto calc_root_pows() {
    return root_pows_calculator<Mod, Root, Size>().calc();
}

}  // anonymous

template <ll Mod, ll PrimitiveRoot, std::size_t MaxSizeLog>
class ntt__ {
    static constexpr ssize_t max_size = 1ll << MaxSizeLog;
    static constexpr ssize_t max_conv_size = max_size * 2;

public:
    using mint = Modint<Mod>;
    using poly = std::array<mint, max_conv_size>;

    constexpr ntt__() { }

    template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
    void convolution(const InputIterator1 a_begin, const InputIterator1 a_end,
                     const InputIterator2 b_begin, const InputIterator2 b_end,
                     OutputIterator out) 
    {
        auto asz = std::distance(a_begin, a_end);
        auto bsz = std::distance(b_begin, b_end);
        auto lower_size = asz + bsz - 1;
        auto conv_size = ceil_pow2(lower_size);
        decltype(auto) rev_bit = ntt_helper::rb__.get(conv_size);
        ntt(a_begin, a_end, false, rev_bit, ntt_a.begin());
        ntt(b_begin, b_end, false, rev_bit);
        for (ll i = 0; i < conv_size; i++) ntt_a[i] *= buf[i];
        ntt(ntt_a.begin(), ntt_a.begin() + conv_size, true, rev_bit, out);
    }

    template <typename InputIterator1, typename InputIterator2>
    const poly& convolution(const InputIterator1 a_begin, const InputIterator1 a_end,
                            const InputIterator2 b_begin, const InputIterator2 b_end)
    {
        convolution(a_begin, a_end, b_begin, b_end, buf.begin());
        return buf;
    }

private:
    using pows_type = std::array<mint, MaxSizeLog>;
    constexpr static ll root_max_pow = pow(mint(PrimitiveRoot), (Mod - 1) / (1ll << MaxSizeLog)).value();
    constexpr static ll root_max_inv = mint(root_max_pow).inv().value();
    constexpr static pows_type root_pow_lis = ntt_helper::calc_root_pows<Mod, root_max_pow, MaxSizeLog>();
    constexpr static pows_type root_inv_lis = ntt_helper::calc_root_pows<Mod, root_max_inv, MaxSizeLog>();
    poly buf, ntt_a;

    template <typename Iterator>
    const poly& ntt(const Iterator begin, const Iterator end, 
                    bool inverse, const vec<ll> &rev_bit) 
    {
        const auto len = rev_bit.size();
        
        {
            ssize_t arr_idx = 0;
            auto sz = std::distance(begin, end);
            for (auto &&idx : rev_bit) {
                buf[idx] = (arr_idx < sz ? *(begin + arr_idx) : 0);
                arr_idx++;
            }
        }

        ssize_t unit_size = 2;
        ssize_t root_pow_idx = 0;
        const auto &root_lis = (inverse ? root_inv_lis : root_pow_lis);
        while (unit_size <= len) {
            mint root = root_lis[root_pow_idx];
            mint root_pow = 1;
            auto unit_cnt = len / unit_size;
            for (ll offset = 0; offset < unit_size / 2; offset++) {
                for (ll unit_counter = 0; unit_counter < unit_cnt; unit_counter++) {
                    auto i = unit_counter * unit_size + offset;
                    auto j = i + unit_size / 2;
                    auto cur_val_i = buf[i], cur_val_j = buf[j];
                    cur_val_j *= root_pow;
                    buf[i] = cur_val_i + cur_val_j;
                    buf[j] = cur_val_i - cur_val_j;
                }
                root_pow *= root;
            }
            unit_size *= 2;
            root_pow_idx++;
        }
        if (inverse) {
            auto inv_len = mint(len).inv();
            for (ssize_t i = 0; i < rev_bit.size(); i++) buf[i] *= inv_len;
        }

        return buf;
    }

    template <typename InputIterator, typename OutputIterator>
    const void ntt(const InputIterator begin, const InputIterator end, 
                   bool inverse, const vec<ll> &rev_bit, OutputIterator out) 
    {
        ntt(begin, end, inverse, rev_bit);
        auto len = rev_bit.size();
        bool copy = true;
        if constexpr (std::is_same<OutputIterator, typename decltype(buf)::iterator>::value) {
            if (out == buf.begin()) copy = false;
        } else {
        }
        if (copy) std::copy(buf.cbegin(), buf.cbegin() + len, out);
    }
};

template <ll Mod, std::size_t MaxSizeLog>
using NTT = ntt__<Mod, ntt_helper::find_primitive_root<Mod>(), MaxSizeLog>;

}

constexpr ll mod = 998244353;
using mint = math::Modint<mod>;

struct Solver {
    math::NTT<mod, 20> ntt;
    ll n, q;
    vec<mint> v1, buf;
    vec<ll> av, bv;

    Solver(ll n, ll q) : n(n), q(q), v1(4 * n), buf(4 * n), av(n), bv(q) {
        for (ll &e : av) std::cin >> e;
        for (ll &e : bv) std::cin >> e;
    }

    void calc(ll l, ll r) {
        if (r - l == 1) {
            v1[2 * l] = av[l] - 1;
            v1[2 * l + 1] = 1;
            return;
        } else if (r - l == 0) {
            v1[2 * l] = 1;
            return;
        }

        ll m = (l + r) / 2;
        calc(l, m);
        calc(m, r);
        auto sz1 = std::max<ll>(1, m - l), sz2 = std::max<ll>(1, r - m);
        assert(0<=m-l&&0<=r-m);
        assert(2*(l+sz1+sz2)<=buf.size());
        ntt.convolution(v1.begin() + 2 * l, v1.begin() + 2 * (l + sz1),
                        v1.begin() + 2 * (l + sz1), v1.begin() + 2 * (l + sz1 + sz2),
                        buf.begin() + 2 * l);
        std::copy(buf.begin() + 2 * l, buf.begin() + 2 * (l + sz1 + sz2), v1.begin() + 2 * l);
    }

    void solve() {
        calc(0, n);
        for (ll e : bv) {
            std::cout << buf[e].value() << "\n";
        }
    }
};

int main() {
    ll n, q;
    std::cin >> n >> q;
    Solver solver(n, q);
    solver.solve();
    return 0;
}
0