結果

問題 No.1068 #いろいろな色 / Red and Blue and more various colors (Hard)
ユーザー tonyu0tonyu0
提出日時 2020-08-01 20:13:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,149 ms / 3,500 ms
コード長 1,929 bytes
コンパイル時間 796 ms
コンパイル使用メモリ 81,752 KB
実行使用メモリ 15,400 KB
最終ジャッジ日時 2023-09-22 13:59:47
合計ジャッジ時間 25,353 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 28 ms
4,376 KB
testcase_04 AC 19 ms
4,380 KB
testcase_05 AC 22 ms
4,376 KB
testcase_06 AC 17 ms
4,376 KB
testcase_07 AC 15 ms
4,380 KB
testcase_08 AC 20 ms
4,376 KB
testcase_09 AC 23 ms
4,376 KB
testcase_10 AC 11 ms
4,380 KB
testcase_11 AC 15 ms
4,376 KB
testcase_12 AC 9 ms
4,380 KB
testcase_13 AC 1,137 ms
15,344 KB
testcase_14 AC 1,137 ms
15,260 KB
testcase_15 AC 1,142 ms
15,256 KB
testcase_16 AC 1,142 ms
15,304 KB
testcase_17 AC 1,142 ms
15,400 KB
testcase_18 AC 1,138 ms
15,252 KB
testcase_19 AC 1,140 ms
15,260 KB
testcase_20 AC 1,136 ms
15,348 KB
testcase_21 AC 1,136 ms
15,336 KB
testcase_22 AC 1,141 ms
15,216 KB
testcase_23 AC 1,143 ms
15,344 KB
testcase_24 AC 1,147 ms
15,256 KB
testcase_25 AC 1,137 ms
15,332 KB
testcase_26 AC 1,143 ms
15,212 KB
testcase_27 AC 1,143 ms
15,192 KB
testcase_28 AC 1,149 ms
15,204 KB
testcase_29 AC 1,092 ms
15,344 KB
testcase_30 AC 1,091 ms
15,344 KB
testcase_31 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)
constexpr ll MOD = 998244353;

int64_t pow(int64_t a, int64_t b) {
  int64_t x = 1, step = 1 << 30;
  while (step > 0) {
    x = x * x % MOD;
    if (step & b) x = x * a % MOD;
    step >>= 1;
  }
  return x;
}

void fft(vector<int64_t>& a) {
  int n = a.size();
  for (int i = 0; i < n; i++) {
    int j = 0;
    int x = i, y = n - 1;
    while (y > 0) {
      j = (j << 1) + (x & 1);
      x >>= 1;
      y >>= 1;
    }
    if (i < j) swap(a[i], a[j]);
  }
  for (int len = 1; len < n; len *= 2) {
    int64_t root = pow(5, (MOD - 1) / (2 * len));
    for (int i = 0; i < n; i += 2 * len) {
      int64_t w = 1;
      for (int j = 0; j < len; j++) {
        int64_t u = a[i + j];
        int64_t v = a[i + j + len] * w % MOD;
        a[i + j] = (u + v) % MOD;
        a[i + j + len] = (MOD + u - v) % MOD;
        w = w * root % MOD;
      }
    }
  }
}

vector<int64_t> multiply(vector<int64_t> a, vector<int64_t> b) {
  int an = a.size();
  int bn = b.size();
  int need = an + bn - 1;
  int nn = 1;
  while (nn < 2 * an || nn < 2 * bn) nn <<= 1;
  a.resize(nn);
  b.resize(nn);
  fft(a);
  fft(b);
  for (int i = 0; i < nn; i++) a[i] = a[i] * b[i] % MOD;
  reverse(++a.begin(), a.end());
  fft(a);
  int64_t inv = pow(nn, MOD - 2);
  for (int i = 0; i < nn; i++) a[i] = a[i] * inv % MOD;
  a.resize(need);
  return a;
}

vector<ll> solve(vector<ll> a) {
  int n = a.size();
  if (n == 1) return {1, a[0] - 1};

  vector<ll> b(a.begin(), a.begin() + n / 2);
  vector<ll> c(a.begin() + n / 2, a.end());
  return multiply(solve(b), solve(c));
}

int main() {
  int n, q, b;
  cin >> n >> q;
  vector<ll> a(n);
  rep(i, 0, n) cin >> a[i];

  vector<ll> c = solve(a);

  reverse(c.begin(), c.end());
  rep(i, 0, q) {
    cin >> b;
    cout << c[b] << '\n';
  }
  return 0;
}
0