結果

問題 No.1068 #いろいろな色 / Red and Blue and more various colors (Hard)
ユーザー stoqstoq
提出日時 2020-11-04 09:10:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,369 ms / 3,500 ms
コード長 6,291 bytes
コンパイル時間 2,414 ms
コンパイル使用メモリ 214,328 KB
実行使用メモリ 103,796 KB
最終ジャッジ日時 2023-09-29 15:39:50
合計ジャッジ時間 30,907 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 21 ms
5,452 KB
testcase_04 AC 14 ms
4,568 KB
testcase_05 AC 16 ms
4,604 KB
testcase_06 AC 12 ms
4,512 KB
testcase_07 AC 11 ms
4,376 KB
testcase_08 AC 15 ms
4,592 KB
testcase_09 AC 16 ms
4,784 KB
testcase_10 AC 8 ms
4,380 KB
testcase_11 AC 11 ms
4,396 KB
testcase_12 AC 7 ms
4,376 KB
testcase_13 AC 1,368 ms
103,704 KB
testcase_14 AC 1,364 ms
103,644 KB
testcase_15 AC 1,363 ms
103,600 KB
testcase_16 AC 1,360 ms
103,796 KB
testcase_17 AC 1,358 ms
103,592 KB
testcase_18 AC 1,356 ms
103,712 KB
testcase_19 AC 1,363 ms
103,720 KB
testcase_20 AC 1,362 ms
103,724 KB
testcase_21 AC 1,358 ms
103,668 KB
testcase_22 AC 1,363 ms
103,764 KB
testcase_23 AC 1,359 ms
103,564 KB
testcase_24 AC 1,360 ms
103,624 KB
testcase_25 AC 1,365 ms
103,620 KB
testcase_26 AC 1,358 ms
103,708 KB
testcase_27 AC 1,364 ms
103,636 KB
testcase_28 AC 1,369 ms
103,720 KB
testcase_29 AC 1,348 ms
103,764 KB
testcase_30 AC 1,353 ms
103,588 KB
testcase_31 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define MOD_TYPE 2

#pragma region Macros

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

#if 0
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using Int = boost::multiprecision::cpp_int;
using lld = boost::multiprecision::cpp_dec_float_100;
#endif
#if 0
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif
using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;

constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
constexpr int INF = (int)1e9 + 10;
constexpr ll LINF = (ll)4e18;
constexpr double PI = acos(-1.0);
constexpr double EPS = 1e-7;
constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)
#define repi(i, n) REPI(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << "\n"
#define Yes(n) cout << ((n) ? "Yes" : "No") << "\n"
#define possible(n) cout << ((n) ? "possible" : "impossible") << "\n"
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n"
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << "\n";

struct io_init
{
  io_init()
  {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(30) << setiosflags(ios::fixed);
  };
} io_init;
template <typename T>
inline bool chmin(T &a, T b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}
inline ll CEIL(ll a, ll b)
{
  return (a + b - 1) / b;
}
template <typename A, size_t N, typename T>
inline void Fill(A (&array)[N], const T &val)
{
  fill((T *)array, (T *)(array + N), val);
}
template <typename T, typename U>
constexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept
{
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
constexpr ostream &operator<<(ostream &os, pair<T, U> &p) noexcept
{
  os << p.first << " " << p.second;
  return os;
}
#pragma endregion

ll mod_pow(ll a, ll n, ll m)
{
  ll res = 1, p = a % m;
  while (n)
  {
    if (n & 1)
      res = res * p % m;
    p = p * p % m;
    n >>= 1;
  }
  return res;
}

template <ll mod = 998244353, ll root = 3> //特殊な素数と原始根 998244353のとき3
class NTT
{
private:
  template <typename T>
  inline void bit_reverse(vector<T> &a)
  {
    int n = a.size();
    int i = 0;
    for (int j = 1; j < n - 1; ++j)
    {
      for (int k = n >> 1; k > (i ^= k); k >>= 1)
        ;
      if (j < i)
        swap(a[i], a[j]);
    }
  }
  void _ntt(vector<long long> &a, int sign)
  {
    const int n = a.size();
    assert((n ^ (n & -n)) == 0); //n = 2^k

    long long tmp = (mod - 1) * mod_pow((ll)n, mod - 2, mod) % mod; // -1/n
    long long h = mod_pow(root, tmp, mod);                          // ^n√g
    if (sign == -1)
      h = mod_pow(h, mod - 2, mod);

    bit_reverse(a);

    for (ll m = 1; m < n; m <<= 1)
    {
      const ll m2 = 2 * m;
      long long _base = mod_pow((ll)h, (ll)(n / m2), mod);
      long long _w = 1;
      for (int x = 0; x < m; ++x)
      {
        for (ll s = x; s < n; s += m2)
        {
          long long u = a[s];
          long long d = (a[s + m] * _w) % mod;
          a[s] = (u + d) % mod;
          a[s + m] = (u - d + mod) % mod;
        }
        _w = (_w * _base) % mod;
      }
    }
  }

  void ntt(vector<long long> &input) { _ntt(input, 1); }

  void intt(vector<long long> &input)
  {
    _ntt(input, -1);
    const long long n_inv = mod_pow((ll)input.size(), mod - 2, mod);
    for (auto &x : input)
      x = (x * n_inv) % mod;
  }

public:
  // 畳み込み演算を行う
  vector<long long> convolution(const vector<long long> &a, const vector<long long> &b)
  {
    int result_size = a.size() + b.size() - 1;
    int n = 1;
    while (n < result_size)
      n <<= 1;

    vector<long long> _a = a, _b = b;
    _a.resize(n, 0);
    _b.resize(n, 0);

    ntt(_a);
    ntt(_b);
    for (int i = 0; i < n; ++i)
      _a[i] = (_a[i] * _b[i]) % mod;
    intt(_a);

    _a.resize(result_size);
    return _a;
  }
};

template <typename T>
class SegmentTree
{
private:
  using Fn = function<T(T &, T &)>;
  int N;
  vector<T> dat;
  T unit;
  Fn func;

public:
  SegmentTree() {}
  SegmentTree(int n_, Fn func_, T unit_) : func(func_), unit(unit_)
  {
    N = 1;
    while (N < n_)
      N *= 2;
    dat.assign(2 * N - 1, unit);
  }
  SegmentTree(const vector<T> &v, Fn func_, T unit_) : func(func_), unit(unit_)
  {
    N = 1;
    int sz = v.size();
    while (N < sz)
      N *= 2;
    dat.resize(2 * N - 1);
    for (int i = 0; i < N; ++i)
      dat[i + N - 1] = (i < sz ? v[i] : unit);
    for (int i = N - 2; i >= 0; --i)
      dat[i] = func(dat[i * 2 + 1], dat[i * 2 + 2]);
  }

  void update(int k, T a)
  {
    k += N - 1;
    dat[k] = a;
    while (k > 0)
    {
      k = (k - 1) / 2;
      dat[k] = func(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  T get(int k) { return dat[k + N - 1]; }

  T query(int l, int r)
  {
    T vl = unit, vr = unit;
    for (l += (N - 1), r += (N - 1); l < r; l >>= 1, r >>= 1)
    {
      if ((l & 1) == 0)
        vl = func(vl, dat[l]);
      if ((r & 1) == 0)
        vr = func(vr, dat[--r]);
    }
    return func(vl, vr);
  }
};

void solve()
{
  NTT<> ntt;
  int n, q;
  cin >> n >> q;
  vector<ll> a(n);
  rep(i, n)
  {
    cin >> a[i];
    a[i] %= MOD;
  }
  auto prod = [&](vector<ll> &f, vector<ll> &g) {
    if (f.size() == 1 and f[0] == 1)
      return g;
    if (g.size() == 1 and g[0] == 1)
      return f;
    return ntt.convolution(f, g);
  };
  vector<ll> unit = {1};
  vector<vector<ll>> f(n, vector<ll>(2));
  rep(i, n)
  {
    f[i][0] = a[i] - 1, f[i][1] = 1;
  }
  SegmentTree<vector<ll>> sg(f, prod, unit);
  vector<ll> g = sg.query(0, n);
  rep(qi, q)
  {
    int b;
    cin >> b;
    cout << g[b] << "\n";
  }
}

int main()
{
  solve();
}
0