結果

問題 No.1068 #いろいろな色 / Red and Blue and more various colors (Hard)
ユーザー mfbgjsczmfbgjscz
提出日時 2020-08-14 07:38:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 405 ms / 3,500 ms
コード長 6,220 bytes
コンパイル時間 2,703 ms
コンパイル使用メモリ 211,960 KB
実行使用メモリ 14,252 KB
最終ジャッジ日時 2024-04-18 18:10:39
合計ジャッジ時間 11,308 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 3 ms
6,812 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 14 ms
6,940 KB
testcase_04 AC 10 ms
6,940 KB
testcase_05 AC 12 ms
6,940 KB
testcase_06 AC 9 ms
6,944 KB
testcase_07 AC 8 ms
6,940 KB
testcase_08 AC 12 ms
6,940 KB
testcase_09 AC 12 ms
6,944 KB
testcase_10 AC 7 ms
6,940 KB
testcase_11 AC 8 ms
6,944 KB
testcase_12 AC 6 ms
6,940 KB
testcase_13 AC 403 ms
14,124 KB
testcase_14 AC 402 ms
14,124 KB
testcase_15 AC 401 ms
14,124 KB
testcase_16 AC 396 ms
14,128 KB
testcase_17 AC 405 ms
14,000 KB
testcase_18 AC 403 ms
13,996 KB
testcase_19 AC 404 ms
14,252 KB
testcase_20 AC 400 ms
14,120 KB
testcase_21 AC 404 ms
14,124 KB
testcase_22 AC 402 ms
14,124 KB
testcase_23 AC 405 ms
14,000 KB
testcase_24 AC 403 ms
13,996 KB
testcase_25 AC 403 ms
14,120 KB
testcase_26 AC 395 ms
14,120 KB
testcase_27 AC 396 ms
14,120 KB
testcase_28 AC 342 ms
9,880 KB
testcase_29 AC 322 ms
9,900 KB
testcase_30 AC 321 ms
9,776 KB
testcase_31 AC 3 ms
6,940 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;
}
//from: yosupot(https://github.com/yosupo06/Algorithm/blob/master/src/math/nft.hpp)
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, (mod - 1) / (1 << ep.size()), 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) <= 256) {
        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 = 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 = mod - 1;
        long long di = 4;
        while (Mod % di == 0) {
            long long ep = modpow(G, Mod / di, 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) % 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 + mod) * 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) < 256) {
        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);
  return multiply(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