結果
| 問題 |
No.1068 #いろいろな色 / Red and Blue and more various colors (Hard)
|
| コンテスト | |
| ユーザー |
risujiroh
|
| 提出日時 | 2020-05-29 21:49:43 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 531 ms / 3,500 ms |
| コード長 | 2,953 bytes |
| コンパイル時間 | 3,032 ms |
| コンパイル使用メモリ | 203,648 KB |
| 最終ジャッジ日時 | 2025-01-10 16:58:02 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
#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';
}
}
risujiroh