結果

問題 No.1068 #いろいろな色 / Red and Blue and more various colors (Hard)
ユーザー risujirohrisujiroh
提出日時 2020-05-29 21:49:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 442 ms / 3,500 ms
コード長 2,953 bytes
コンパイル時間 3,454 ms
コンパイル使用メモリ 209,836 KB
実行使用メモリ 52,532 KB
最終ジャッジ日時 2023-10-20 08:51:02
合計ジャッジ時間 12,939 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 8 ms
4,360 KB
testcase_04 AC 7 ms
4,348 KB
testcase_05 AC 7 ms
4,348 KB
testcase_06 AC 6 ms
4,348 KB
testcase_07 AC 5 ms
4,348 KB
testcase_08 AC 7 ms
4,348 KB
testcase_09 AC 8 ms
4,348 KB
testcase_10 AC 5 ms
4,348 KB
testcase_11 AC 5 ms
4,348 KB
testcase_12 AC 4 ms
4,348 KB
testcase_13 AC 442 ms
52,532 KB
testcase_14 AC 439 ms
52,532 KB
testcase_15 AC 439 ms
52,532 KB
testcase_16 AC 437 ms
52,532 KB
testcase_17 AC 434 ms
52,532 KB
testcase_18 AC 438 ms
52,532 KB
testcase_19 AC 439 ms
52,532 KB
testcase_20 AC 438 ms
52,532 KB
testcase_21 AC 437 ms
52,532 KB
testcase_22 AC 436 ms
52,532 KB
testcase_23 AC 440 ms
52,532 KB
testcase_24 AC 434 ms
52,532 KB
testcase_25 AC 434 ms
52,532 KB
testcase_26 AC 431 ms
52,532 KB
testcase_27 AC 430 ms
52,532 KB
testcase_28 AC 430 ms
52,532 KB
testcase_29 AC 415 ms
52,532 KB
testcase_30 AC 418 ms
52,532 KB
testcase_31 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#ifdef LOCAL
#include "debug.h"
#else
#define DEBUG(...)
#endif

template <class T, class Op = multiplies<T>>
constexpr T power(T a, long long n, Op op = Op(), T e = {1}) {
  assert(n >= 0);
  while (n) {
    if (n & 1) e = op(e, a);
    if (n >>= 1) a = op(a, a);
  }
  return e;
}

template <class T> void ntt(vector<T>& a, bool inverse) {
  int n = size(a);
  assert((n & (n - 1)) == 0);
  if (n < 2) return;
  assert((T::mod - 1) % n == 0);
  static vector<T> w{1}, iw{1};
  for (int m = size(w); m < n / 2; m *= 2) {
    static T root = 2;
    while (power(root, (T::mod - 1) / 2) == 1) root += 1;
    T dw = power(root, (T::mod - 1) / (4 * m)), idw = 1 / dw;
    w.resize(2 * m), iw.resize(2 * m);
    for (int i = 0; i < m; ++i) w[m + i] = w[i] * dw, iw[m + i] = iw[i] * idw;
  }
  if (not inverse) {
    for (int m = n; m >>= 1; ) {
      for (int s = 0, k = 0; s < n; s += 2 * m, ++k) {
        for (int i = s, j = s + m; i < s + m; ++i, ++j) {
          T x = a[i], y = a[j] * w[k];
          a[i] = x + y, a[j] = x - y;
        }
      }
    }
  } else {
    for (int m = 1; m < n; m *= 2) {
      for (int s = 0, k = 0; s < n; s += 2 * m, ++k) {
        for (int i = s, j = s + m; i < s + m; ++i, ++j) {
          T x = a[i], y = a[j];
          a[i] = x + y, a[j] = (x - y) * iw[k];
        }
      }
    }
    auto inv = 1 / T(n);
    for (auto&& e : a) e *= inv;
  }
}
template <class T> vector<T> operator*(vector<T> a, vector<T> b) {
  if (a.empty() or b.empty()) return {};
  int n = size(a), m = size(b), sz = 1 << __lg(2 * (n + m - 1) - 1);
  a.resize(sz), ntt(a, false);
  b.resize(sz), ntt(b, false);
  for (int i = 0; i < sz; ++i) a[i] *= b[i];
  ntt(a, true), a.resize(n + m - 1);
  return a;
}

template <unsigned M> struct modular {
  using m = modular;
  static constexpr unsigned mod = M;
  unsigned v;
  modular(long long x = 0) : v((x %= mod) < 0 ? x + mod : x) {}
  m operator-() const { return m() -= *this; }
  m& operator+=(m b) { if ((int)(v += b.v - mod) < 0) v += mod; return *this; }
  m& operator-=(m b) { if ((int)(v -= b.v) < 0) v += mod; return *this; }
  m& operator*=(m b) { v = (uint64_t)v * b.v % mod; return *this; }
  m& operator/=(m b) { return *this *= power(b, mod - 2); }
  friend m operator+(m a, m b) { return a += b; }
  friend m operator-(m a, m b) { return a -= b; }
  friend m operator*(m a, m b) { return a *= b; }
  friend m operator/(m a, m b) { return a /= b; }
  friend bool operator==(m a, m b) { return a.v == b.v; }
};

using mint = modular<998244353>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  using poly = vector<mint>;
  vector<poly> t(2 * n);
  for (int i = 0; i < n; ++i) {
    long long a;
    cin >> a;
    t[n + i] = {a - 1, 1};
  }
  for (int i = n; i--; ) {
    t[i] = t[2 * i] * t[2 * i + 1];
  }
  while (q--) {
    int b;
    cin >> b;
    cout << t[1][b].v << '\n';
  }
}
0