結果

問題 No.1066 #いろいろな色 / Red and Blue and more various colors (Easy)
ユーザー iiljjiiljj
提出日時 2020-07-13 23:57:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 17,975 bytes
コンパイル時間 3,043 ms
コンパイル使用メモリ 238,188 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-08 19:49:58
合計ジャッジ時間 4,340 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 1 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 16 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 27 ms
5,248 KB
testcase_11 AC 13 ms
5,248 KB
testcase_12 AC 12 ms
5,248 KB
testcase_13 AC 14 ms
5,248 KB
testcase_14 AC 13 ms
5,248 KB
testcase_15 AC 26 ms
5,248 KB
testcase_16 AC 8 ms
5,248 KB
testcase_17 AC 8 ms
5,248 KB
testcase_18 AC 24 ms
5,248 KB
testcase_19 AC 13 ms
5,248 KB
testcase_20 AC 2 ms
5,248 KB
testcase_21 AC 17 ms
5,248 KB
testcase_22 AC 2 ms
5,248 KB
testcase_23 AC 2 ms
5,248 KB
testcase_24 AC 2 ms
5,248 KB
testcase_25 AC 28 ms
5,248 KB
testcase_26 AC 29 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* #region Head */

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pll = pair<ll, ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vc<vc<T>>;
using vll = vc<ll>;
using vvll = vvc<ll>;
using vld = vc<ld>;
using vvld = vvc<ld>;
using vs = vc<string>;
using vvs = vvc<string>;
template <class T, class U> using um = unordered_map<T, U>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqa = priority_queue<T, vc<T>, greater<T>>;
template <class T> using us = unordered_set<T>;

#define REP(i, m, n) for (ll i = (m), i##_len = (ll)(n); i < i##_len; ++(i))
#define REPM(i, m, n) for (ll i = (m), i##_max = (ll)(n); i <= i##_max; ++(i))
#define REPR(i, m, n) for (ll i = (m), i##_min = (ll)(n); i >= i##_min; --(i))
#define REPD(i, m, n, d) for (ll i = (m), i##_len = (ll)(n); i < i##_len; i += (d))
#define REPMD(i, m, n, d) for (ll i = (m), i##_max = (ll)(n); i <= i##_max; i += (d))
#define REPI(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define ALL(x) begin(x), end(x)
#define SIZE(x) ((ll)(x).size())
#define PERM(c)                                                                                                        \
    sort(ALL(c));                                                                                                      \
    for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
#define UNIQ(v) v.erase(unique(ALL(v)), v.end());

#define endl '\n'
#define sqrt sqrtl
#define floor floorl
#define log2 log2l

constexpr ll INF = 1'010'000'000'000'000'017LL;
constexpr ll MOD = 1'000'000'007LL; // 1e9 + 7
constexpr ld EPS = 1e-12;
constexpr ld PI = 3.14159265358979323846;

template <typename T> istream &operator>>(istream &is, vc<T> &vec) { // vector 入力
    for (T &x : vec) is >> x;
    return is;
}
template <typename T> ostream &operator<<(ostream &os, vc<T> &vec) { // vector 出力 (for dump)
    os << "{";
    REP(i, 0, SIZE(vec)) os << vec[i] << (i == i_len - 1 ? "" : ", ");
    os << "}";
    return os;
}
template <typename T> ostream &operator>>(ostream &os, vc<T> &vec) { // vector 出力 (inline)
    REP(i, 0, SIZE(vec)) os << vec[i] << (i == i_len - 1 ? "\n" : " ");
    return os;
}

template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &pair_var) { // pair 入力
    is >> pair_var.first >> pair_var.second;
    return is;
}
template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { // pair 出力
    os << "(" << pair_var.first << ", " << pair_var.second << ")";
    return os;
}

// map, um, set, us 出力
template <class T> ostream &out_iter(ostream &os, T &map_var) {
    os << "{";
    REPI(itr, map_var) {
        os << *itr;
        auto itrcp = itr;
        if (++itrcp != map_var.end()) os << ", ";
    }
    return os << "}";
}
template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { return out_iter(os, map_var); }
template <typename T, typename U> ostream &operator<<(ostream &os, um<T, U> &map_var) {
    os << "{";
    REPI(itr, map_var) {
        auto [key, value] = *itr;
        os << "(" << key << ", " << value << ")";
        auto itrcp = itr;
        if (++itrcp != map_var.end()) os << ", ";
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { return out_iter(os, set_var); }
template <typename T> ostream &operator<<(ostream &os, us<T> &set_var) { return out_iter(os, set_var); }
template <typename T> ostream &operator<<(ostream &os, pq<T> &pq_var) {
    pq<T> pq_cp(pq_var);
    os << "{";
    if (!pq_cp.empty()) {
        os << pq_cp.top(), pq_cp.pop();
        while (!pq_cp.empty()) os << ", " << pq_cp.top(), pq_cp.pop();
    }
    return os << "}";
}

// dump
#define DUMPOUT cerr
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail> void dump_func(Head &&head, Tail &&... tail) {
    DUMPOUT << head;
    if (sizeof...(Tail) > 0) DUMPOUT << ", ";
    dump_func(move(tail)...);
}

// chmax (更新「される」かもしれない値が前)
template <typename T, typename U, typename Comp = less<>> bool chmax(T &xmax, const U &x, Comp comp = {}) {
    if (comp(xmax, x)) {
        xmax = x;
        return true;
    }
    return false;
}

// chmin (更新「される」かもしれない値が前)
template <typename T, typename U, typename Comp = less<>> bool chmin(T &xmin, const U &x, Comp comp = {}) {
    if (comp(x, xmin)) {
        xmin = x;
        return true;
    }
    return false;
}

// ローカル用
#define DEBUG_

#ifdef DEBUG_
#define DEB
#define dump(...)                                                                                                      \
    DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                                                                    \
            << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl                                        \
            << "    ",                                                                                                 \
        dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif

struct AtCoderInitialize {
    static constexpr int IOS_PREC = 15;
    static constexpr bool AUTOFLUSH = false;
    AtCoderInitialize() {
        ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
        cout << fixed << setprecision(IOS_PREC);
        if (AUTOFLUSH) cout << unitbuf;
    }
} ATCODER_INITIALIZE;

void Yn(bool p) { cout << (p ? "Yes" : "No") << endl; }
void YN(bool p) { cout << (p ? "YES" : "NO") << endl; }

/* #endregion */

/* #region ConvWithMint */
#define rep(i, b) REP(i, 0, b)
#define si(x) int(x.size())

// size of input must be a power of 2
// output of forward fmt is bit-reversed
// output elements are in the range [0,mod*4)
// input of inverse fmt should be bit-reversed
template <class mint> void inplace_fmt(vector<mint> &f, bool inv) {
    const int n = si(f);
    static const int L = 30;
    static mint g[L], ig[L], p2[L];
    if (g[0].v == 0) {
        rep(i, L) {
            mint w = -mint::root().pow(((mint::mod - 1) >> (i + 2)) * 3);
            g[i] = w;
            ig[i] = w.inv();
            p2[i] = mint(1 << i).inv();
        }
    }
    static constexpr uint mod2 = mint::mod * 2;
    if (!inv) {
        int b = n;
        if (b >>= 1) { // input:[0,mod)
            rep(i, b) {
                uint x = f[i + b].v;
                f[i + b].v = f[i].v + mint::mod - x;
                f[i].v += x;
            }
        }
        if (b >>= 1) { // input:[0,mod*2)
            mint p = 1;
            for (int i = 0, k = 0; i < n; i += b * 2) {
                REP(j, i, i + b) {
                    uint x = (f[j + b] * p).v;
                    // f[j].v=(f[j].v<mint::mod?f[j].v:f[j].v-mint::mod);
                    f[j + b].v = f[j].v + mint::mod - x;
                    f[j].v += x;
                }
                p *= g[__builtin_ctz(++k)];
            }
        }
        while (b) {
            if (b >>= 1) { // input:[0,mod*3)
                mint p = 1;
                for (int i = 0, k = 0; i < n; i += b * 2) {
                    REP(j, i, i + b) {
                        uint x = (f[j + b] * p).v;
                        // f[j].v=(f[j].v<mint::mod?f[j].v:f[j].v-mint::mod);
                        f[j + b].v = f[j].v + mint::mod - x;
                        f[j].v += x;
                    }
                    p *= g[__builtin_ctz(++k)];
                }
            }
            if (b >>= 1) { // input:[0,mod*4)
                mint p = 1;
                for (int i = 0, k = 0; i < n; i += b * 2) {
                    REP(j, i, i + b) {
                        uint x = (f[j + b] * p).v;
                        f[j].v = (f[j].v < mod2 ? f[j].v : f[j].v - mod2);
                        f[j + b].v = f[j].v + mint::mod - x;
                        f[j].v += x;
                    }
                    p *= g[__builtin_ctz(++k)];
                }
            }
        }
    } else {
        int b = 1;
        if (b < n / 2) { // input:[0,mod)
            mint p = 1;
            for (int i = 0, k = 0; i < n; i += b * 2) {
                REP(j, i, i + b) {
                    ull x = f[j].v + mint::mod - f[j + b].v;
                    f[j].v += f[j + b].v;
                    f[j + b].v = x * p.v % mint::mod;
                }
                p *= ig[__builtin_ctz(++k)];
            }
            b <<= 1;
        }
        for (; b < n / 2; b <<= 1) {
            mint p = 1;
            for (int i = 0, k = 0; i < n; i += b * 2) {
                REP(j, i, i + b / 2) { // input:[0,mod*2)
                    ull x = f[j].v + mod2 - f[j + b].v;
                    f[j].v += f[j + b].v;
                    f[j].v = (f[j].v) < mod2 ? f[j].v : f[j].v - mod2;
                    f[j + b].v = x * p.v % mint::mod;
                }
                REP(j, i + b / 2, i + b) { // input:[0,mod)
                    ull x = f[j].v + mint::mod - f[j + b].v;
                    f[j].v += f[j + b].v;
                    // f[j].v=(f[j].v)<mod2?f[j].v:f[j].v-mod2;
                    f[j + b].v = x * p.v % mint::mod;
                }
                p *= ig[__builtin_ctz(++k)];
            }
        }
        if (b < n) { // input:[0,mod*2)
            rep(i, b) {
                uint x = f[i + b].v;
                f[i + b].v = f[i].v + mod2 - x;
                f[i].v += x;
            }
        }
        mint z = p2[__lg(n)];
        rep(i, n) f[i] *= z;
    }
}

struct modinfo {
    uint mod, root;
};
template <modinfo const &ref> struct modular {
    static constexpr uint const &mod = ref.mod;
    static modular root() { return modular(ref.root); }
    uint v;
    // modular(initializer_list<uint>ls):v(*ls.bg){}
    modular(ll vv = 0) { s(vv % mod + mod); }
    modular &s(uint vv) {
        v = vv < mod ? vv : vv - mod;
        return *this;
    }
    modular operator-() const { return modular() - *this; }
    modular &operator+=(const modular &rhs) { return s(v + rhs.v); }
    modular &operator-=(const modular &rhs) { return s(v + mod - rhs.v); }
    modular &operator*=(const modular &rhs) {
        v = ull(v) * rhs.v % mod;
        return *this;
    }
    modular &operator/=(const modular &rhs) { return *this *= rhs.inv(); }
    modular operator+(const modular &rhs) const { return modular(*this) += rhs; }
    modular operator-(const modular &rhs) const { return modular(*this) -= rhs; }
    modular operator*(const modular &rhs) const { return modular(*this) *= rhs; }
    modular operator/(const modular &rhs) const { return modular(*this) /= rhs; }
    modular pow(int n) const {
        modular res(1), x(*this);
        while (n) {
            if (n & 1) res *= x;
            x *= x;
            n >>= 1;
        }
        return res;
    }
    modular inv() const { return pow(mod - 2); }
    /*modular inv()const{
                int x,y;
                int g=extgcd(v,mod,x,y);
                assert(g==1);
                if(x<0)x+=mod;
                return modular(x);
        }*/
    friend modular operator+(int x, const modular &y) { return modular(x) + y; }
    friend modular operator-(int x, const modular &y) { return modular(x) - y; }
    friend modular operator*(int x, const modular &y) { return modular(x) * y; }
    friend modular operator/(int x, const modular &y) { return modular(x) / y; }
    friend ostream &operator<<(ostream &os, const modular &m) { return os << m.v; }
    friend istream &operator>>(istream &is, modular &m) {
        ll x;
        is >> x;
        m = modular(x);
        return is;
    }
    bool operator<(const modular &r) const { return v < r.v; }
    bool operator==(const modular &r) const { return v == r.v; }
    bool operator!=(const modular &r) const { return v != r.v; }
    explicit operator bool() const { return v; }
};

// 59501818244292734739283969=5.95*10^25 までの値を正しく計算
//最終的な列の大きさが 2^24 までなら動く
//最終的な列の大きさが 2^20 以下のときは,下の 3 つの素数を使ったほうが速い(は?)
// VERIFY: yosupo
namespace arbitrary_convolution {
constexpr modinfo base0{167772161, 3};  // 2^25 * 5 + 1
constexpr modinfo base1{469762049, 3};  // 2^26 * 7 + 1
constexpr modinfo base2{754974721, 11}; // 2^24 * 45 + 1
// constexpr modinfo base0{1045430273,3};//2^20 * 997 + 1
// constexpr modinfo base1{1051721729,6};//2^20 * 1003 + 1
// constexpr modinfo base2{1053818881,7};//2^20 * 1005 + 1
using mint0 = modular<base0>;
using mint1 = modular<base1>;
using mint2 = modular<base2>;
template <class t, class mint> vc<t> sub(const vc<mint> &x, const vc<mint> &y, bool same = false) {
    int n = si(x) + si(y) - 1;
    int s = 1;
    while (s < n) s *= 2;
    vc<t> z(s);
    rep(i, si(x)) z[i] = x[i].v;
    inplace_fmt(z, false);
    if (!same) {
        vc<t> w(s);
        rep(i, si(y)) w[i] = y[i].v;
        inplace_fmt(w, false);
        rep(i, s) z[i] *= w[i];
    } else {
        rep(i, s) z[i] *= z[i];
    }
    inplace_fmt(z, true);
    z.resize(n);
    return z;
}
template <class mint> vc<mint> multiply(const vc<mint> &x, const vc<mint> &y, bool same = false) {
    auto d0 = sub<mint0>(x, y, same);
    auto d1 = sub<mint1>(x, y, same);
    auto d2 = sub<mint2>(x, y, same);
    int n = si(d0);
    vc<mint> res(n);
    static const mint1 r01 = mint1(mint0::mod).inv();
    static const mint2 r02 = mint2(mint0::mod).inv();
    static const mint2 r12 = mint2(mint1::mod).inv();
    static const mint2 r02r12 = r02 * r12;
    static const mint w1 = mint(mint0::mod);
    static const mint w2 = w1 * mint(mint1::mod);
    rep(i, n) {
        ull a = d0[i].v;
        ull b = (d1[i].v + mint1::mod - a) * r01.v % mint1::mod;
        ull c = ((d2[i].v + mint2::mod - a) * r02r12.v + (mint2::mod - b) * r12.v) % mint2::mod;
        res[i].v = (a + b * w1.v + c * w2.v) % mint::mod;
    }
    return res;
}
} // namespace arbitrary_convolution
using arbitrary_convolution::multiply;

template <typename T> vector<T> add_to_vector(vector<T> &z, T v) {
    z.push_back(v);
    return z;
}

template <typename T, typename... Args> vector<T> add_to_vector(vector<T> &z, T v, Args... args) {
    z.push_back(v);
    add_to_vector<T>(z, args...);
    return z;
}

template <typename T> vector<T> make_vector(T v) {
    vector<T> z;
    z.push_back(v);
    return z;
}

template <typename T, typename... Args> vector<T> make_vector(T v, Args... args) {
    vector<T> z;
    z.push_back(v);
    add_to_vector<T>(z, args...);
    return z;
}

constexpr modinfo base{998244353, 0};
using mint = modular<base>;
/* #endregion */

/* #region SegTree */

template <typename T> // T: 要素
struct SegmentTree {
    using F = function<T(T, T)>; // 要素と要素をマージする関数.max とか.

    ll n; // 木のノード数
    F f; // 区間クエリで使う演算,結合法則を満たす演算.区間最大値のクエリを投げたいなら max 演算.
    T ti; // 値配列の初期値.演算 f に関する単位元.区間最大値なら単位元は 0. (a>0 なら max(a,0)=max(0,a)=a)
    vc<T> dat; // 1-indexed 値配列 (index は木の根から順に 1 | 2 3 | 4 5 6 7 | 8 9 10 11 12 13 14 15 | ...)

    // コンストラクタ.
    SegmentTree() {}
    // コンストラクタ.
    SegmentTree(F f, T ti) : f(f), ti(ti) {}

    // 指定要素数のセグメント木を初期化する
    void init(ll n_) {
        n = 1;
        while (n < n_) n <<= 1;
        dat.assign(n << 1, ti);
    }

    // ベクトルからセグメント木を構築する
    void build(const vc<T> &v) {
        ll n_ = v.size();
        init(n_);
        REP(i, 0, n_) dat[n + i] = v[i];
        REPR(i, n - 1, 1) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
    }

    // インデックス k の要素の値を x にする.
    void set_val(ll k, T x) {
        dat[k += n] = x;
        while (k >>= 1) dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]); // 上へ登って更新していく
    }

    // インデックス k の要素の値を取得する.
    T get_val(ll k) { return dat[k + n]; }

    // 半開区間 [a, b) に対するクエリを実行する
    T query(ll a, ll b) {
        if (a >= b) return ti;
        // assert(a<b)

        T vl = ti, vr = ti;
        for (ll l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) vl = f(vl, dat[l++]);
            if (r & 1) vr = f(dat[--r], vr);
        }
        return f(vl, vr);
    }

    // セグメント木上の二分探索
    template <typename C> int find(ll st, C &check, T &acc, ll k, ll l, ll r) {
        if (l + 1 == r) {
            acc = f(acc, dat[k]);
            return check(acc) ? k - n : -1;
        }
        ll m = (l + r) >> 1;
        if (m <= st) return find(st, check, acc, (k << 1) | 1, m, r);
        if (st <= l && !check(f(acc, dat[k]))) {
            acc = f(acc, dat[k]);
            return -1;
        }
        ll vl = find(st, check, acc, (k << 1) | 0, l, m);
        if (~vl) return vl;
        return find(st, check, acc, (k << 1) | 1, m, r);
    }

    // セグメント木上の二分探索.check(query(st, idx)) が真となる idx を返す.
    template <typename C> int find(ll st, C &check) {
        T acc = ti;
        return find(st, check, acc, 1, 0, n);
    }
};
/* #endregion */

// Problem
void solve() {
    ll n, q;
    cin >> n >> q;
    vll a(n), b(q);
    cin >> a >> b;

    using vm = vc<mint>;
    auto f = [](vm a, vm b) -> vm { return arbitrary_convolution::multiply(a, b); };
    SegmentTree<vm> seg(f, vm({1}));
    vc<vm> data(n);
    REP(i, 0, n) data[i] = {a[i] - 1, 1};
    seg.build(data);

    vm ls = seg.query(0, n);
    REP(i, 0, q) cout << ls[b[i]] << endl;
}

// entry point
int main() {
    solve();
    return 0;
}
0