結果

問題 No.1068 #いろいろな色 / Red and Blue and more various colors (Hard)
ユーザー leafirbyleafirby
提出日時 2020-08-14 06:02:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,385 bytes
コンパイル時間 2,805 ms
コンパイル使用メモリ 215,992 KB
実行使用メモリ 15,484 KB
最終ジャッジ日時 2024-10-10 11:23:55
合計ジャッジ時間 13,889 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 3 ms
5,248 KB
testcase_02 AC 3 ms
5,248 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 3 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")    
using namespace std;
#define rep(i,m,n) for(long long i = m; i < n; i++)
#define Rev(n) reverse(n.begin(),n.end())
#define Vec(K,L,N,S) vector<L> K(N,S)
#define mod 998244353

long long modpow(long long n, long long k, long long m) {
  if(!k) return 1;
  else if(k & 1) return modpow(n, k - 1, m) * n % m;
  else {
    long long temp = modpow(n, k / 2, m);
    return temp * temp % m;
  }
}

long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

long long G = 3;

void nft(bool type, vector<long long>& a) {
    int n = int(a.size()), s = 0;
    while ((1 << s) < n) s++;
    assert(1 << s == n);

    static vector<long long> ep, iep;
    while (int(ep.size()) <= s) {
        ep.push_back(modpow(G, modinv(-1, mod) * modinv(1 << ep.size(), mod), mod));
        iep.push_back(modinv(ep.back(), mod));
    }
    vector<long long> b(n);
    for (int i = 1; i <= s; i++) {
        int w = 1 << (s - i);
        long long base = type ? iep[i] : ep[i], now = 1;
        for (int y = 0; y < n / 2; y += w) {
            for (int x = 0; x < w; x++) {
                auto l = a[y << 1 | x];
                auto r = now * a[y << 1 | x | w] % mod;
                b[y | x] = (l + r) % mod;
                b[y | x | n >> 1] = (l - r + mod) % mod;
            }
            now = now * base % mod;
        }
        swap(a, b);
    }
}

vector<long long> multiply_nft(const vector<long long>& a, const vector<long long>& b) {
    int n = a.size(), m = b.size();
    if (!n || !m) return {};
    if (min(n, m) <= 8) {
        vector<long long> ans(n + m - 1);
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) ans[i + j] = (ans[i + j] + a[i] * b[j] % mod) % mod;
        return ans;
    }
    int lg = 0;
    while ((1 << lg) < n + m - 1) lg++;
    int z = 1 << lg;
    auto a2 = a, b2 = b;
    a2.resize(z);
    b2.resize(z);
    nft(false, a2);
    nft(false, b2);
    for (int i = 0; i < z; i++) a2[i] = a2[i] * b2[i] % mod;
    nft(true, a2);
    a2.resize(n + m - 1);
    long long iz = modinv(z, mod);
    for (int i = 0; i < n + m - 1; i++) a2[i] = a2[i] * iz % mod;
    return a2;
}

// Cooley-Tukey: input -> butterfly -> bit reversing -> output から
// bit reversingを抜いたもの 直接使うな
void butterfly(bool type, vector<long long>& a) {
    int n = int(a.size()), h = 0;
    while ((1 << h) < n) h++;
    assert(1 << h == n);
    if (n == 1) return;

    static vector<long long> snow, sinow;
    if (snow.empty()) {
        long long sep = 1, siep = 1;
        long long Mod = modinv(-1, mod);
        long long di = 4;
        while (Mod % di == 0) {
            long long ep = modpow(G, Mod * modinv(di, mod), mod);
            long long iep = modinv(ep, mod);
            snow.push_back(siep * ep % mod);
            sinow.push_back(sep * iep % mod);
            sep = sep * ep % mod;
            siep = siep * iep % mod;
            di = di * 2 % mod;
        }
    }

    if (!type) {
        // fft
        for (int ph = 1; ph <= h; ph++) {
            // phase ph: size w -> 2w の FFT, p 並列
            int w = 1 << (ph - 1), p = 1 << (h - ph);
            long long now = 1;
            for (int s = 0; s < w; s++) {
                int offset = s << (h - ph + 1);
                for (int i = 0; i < p; i++) {
                    auto l = a[i + offset];
                    auto r = a[i + offset + p] * now % mod;
                    a[i + offset] = (l + r) % mod;
                    a[i + offset + p] = (l - r) % mod;
                }
                int u = __builtin_ctzll(s);
                now = now * snow[u] % mod;
            }
        }
    } else {
        // ifft
        for (int ph = h; ph >= 1; ph--) {
            int w = 1 << (ph - 1), p = 1 << (h - ph);
            long long inow = 1;
            for (int s = 0; s < w; s++) {
                int offset = s << (h - ph + 1);
                for (int i = 0; i < p; i++) {
                    auto l = a[i + offset];
                    auto r = a[i + offset + p];
                    a[i + offset] = (l + r) % mod;
                    a[i + offset + p] = (l - r) * inow % mod;
                }
                int u = __builtin_ctzll(s);
                inow = inow * sinow[u] % mod;
            }
        }
    }
}
vector<long long> multiply(const vector<long long>& a, const vector<long long>& b) {
    int n = a.size(), m = b.size();
    if (!n || !m) return {};
    if (min(n, m) < 8) {
        vector<long long> ans(n + m - 1);
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) ans[i + j] = (ans[i+j] + a[i] * b[j] % mod) % mod;
        return ans;
    }
    int lg = 0;
    while ((1 << lg) < n + m - 1) lg++;
    int z = 1 << lg;
    auto a2 = a;
    a2.resize(z);
    butterfly(false, a2);
    if (a == b) {
        for (int i = 0; i < z; i++) a2[i] = a2[i] * a2[i] % mod;
    } else {
        auto b2 = b;
        b2.resize(z);
        butterfly(false, b2);
        for (int i = 0; i < z; i++) a2[i] = a2[i] * b2[i] % mod;
    }
    butterfly(true, a2);
    a2.resize(n + m - 1);
    long long iz = modinv(z, mod);
    for (int i = 0; i < n + m - 1; i++) a2[i] = a2[i] * iz % mod;
    return a2;
}
Vec(P, long long, 200000, 0);
vector<long long> divide_and_solve(int left, int right) {//f_l(x)からf_r(x)までの積
  if(right - left == 1) {
    Vec(v, long long, 2, 1);
    v[1] = P[left];
    return v;
  }
  auto vl = divide_and_solve(left, (left + right) / 2);
  auto vr = divide_and_solve((left + right) / 2, right);
  int ls = vl.size(), rs = vr.size();
  if(min(ls, rs) < 264) {
    Vec(marge, long long, ls + rs - 1, 0);
    rep(i, 0, ls)rep(j, 0, rs)marge[i + j] = (marge[i + j] + (vl[i] * vr[j]) % mod) % mod;
    return marge;
  }
  else return multiply_nft(vl, vr);
}
int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long N, M;
  cin >> N >> M;
  Vec(Q, long long, M, 0);
  P.resize(N);
  rep(i, 0, N) {
    cin >> P[i];
    P[i]--;
    P[i] %= mod;
  }
  rep(i, 0, M) cin >> Q[i];
  auto ans = divide_and_solve(0, N);
  Rev(ans);
  rep(i, 0, M) cout << ans[Q[i]] % mod << endl;
}
0