結果
問題 | No.1068 #いろいろな色 / Red and Blue and more various colors (Hard) |
ユーザー | kcvlex |
提出日時 | 2020-05-31 01:16:00 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 14,105 bytes |
コンパイル時間 | 2,094 ms |
コンパイル使用メモリ | 160,956 KB |
実行使用メモリ | 86,760 KB |
最終ジャッジ日時 | 2024-11-14 06:24:14 |
合計ジャッジ時間 | 130,088 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 18 ms
43,248 KB |
testcase_01 | AC | 20 ms
43,176 KB |
testcase_02 | AC | 18 ms
43,104 KB |
testcase_03 | TLE | - |
testcase_04 | TLE | - |
testcase_05 | TLE | - |
testcase_06 | TLE | - |
testcase_07 | TLE | - |
testcase_08 | TLE | - |
testcase_09 | TLE | - |
testcase_10 | TLE | - |
testcase_11 | TLE | - |
testcase_12 | TLE | - |
testcase_13 | TLE | - |
testcase_14 | TLE | - |
testcase_15 | TLE | - |
testcase_16 | TLE | - |
testcase_17 | TLE | - |
testcase_18 | TLE | - |
testcase_19 | TLE | - |
testcase_20 | TLE | - |
testcase_21 | TLE | - |
testcase_22 | TLE | - |
testcase_23 | TLE | - |
testcase_24 | TLE | - |
testcase_25 | TLE | - |
testcase_26 | TLE | - |
testcase_27 | TLE | - |
testcase_28 | TLE | - |
testcase_29 | TLE | - |
testcase_30 | TLE | - |
testcase_31 | WA | - |
ソースコード
#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 (auto &&ele : buf) ele *= 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(2 * n + 10), buf(2 * n + 10), 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); 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; }