結果
| 問題 |
No.1066 #いろいろな色 / Red and Blue and more various colors (Easy)
|
| コンテスト | |
| ユーザー |
KoD
|
| 提出日時 | 2020-05-29 21:34:55 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 135 ms / 2,000 ms |
| コード長 | 4,536 bytes |
| コンパイル時間 | 3,034 ms |
| コンパイル使用メモリ | 116,296 KB |
| 最終ジャッジ日時 | 2025-01-10 16:37:48 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 24 |
ソースコード
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <numeric>
#include <array>
template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) {
lhs = rhs;
return true;
}
return false;
}
template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) {
lhs = rhs;
return true;
}
return false;
}
// [l, r) from l to r
struct range {
struct itr {
int i;
constexpr itr(int i_): i(i_) { }
constexpr void operator ++ () { ++i; }
constexpr int operator * () const { return i; }
constexpr bool operator != (itr x) const { return i != x.i; }
};
const itr l, r;
constexpr range(int l_, int r_): l(l_), r(std::max(l_, r_)) { }
constexpr itr begin() const { return l; }
constexpr itr end() const { return r; }
};
// [l, r) from r to l
struct revrange {
struct itr {
int i;
constexpr itr(int i_): i(i_) { }
constexpr void operator ++ () { --i; }
constexpr int operator * () const { return i; }
constexpr bool operator != (itr x) const { return i != x.i; }
};
const itr l, r;
constexpr revrange(int l_, int r_): l(l_ - 1), r(std::max(l_, r_) - 1) { }
constexpr itr begin() const { return r; }
constexpr itr end() const { return l; }
};
template <uint32_t Modulus>
class modular {
public:
using value_type = uint32_t;
using max_type = uint64_t;
static constexpr value_type mod = Modulus;
static constexpr value_type mod_min = 1;
static constexpr value_type mod_max = 2147483647;
static_assert(mod >= mod_min, "invalid mod :: too small");
static_assert(mod <= mod_max, "invalid mod :: too big");
template <class T>
static constexpr value_type normalize(T value_) {
if (value_ < 0) {
value_ = -value_;
value_ %= mod;
if (value_ == 0) return 0;
return mod - value_;
}
return value_ % mod;
}
private:
value_type value;
public:
constexpr modular(): value(0) { }
template <class T>
explicit constexpr modular(T value_): value(normalize(value_)) { }
template <class T>
explicit constexpr operator T() { return static_cast<T>(value); }
constexpr value_type operator () () const { return value; }
constexpr modular operator - () const { return modular(mod - value); }
constexpr modular operator ~ () const { return inverse(); }
constexpr value_type &extract() { return value; }
constexpr modular inverse() const { return power(mod - 2); }
constexpr modular power(max_type exp) const {
modular res(1), mult(*this);
while (exp > 0) {
if (exp & 1) res *= mult;
mult *= mult;
exp >>= 1;
}
return res;
}
constexpr modular operator + (const modular &rhs) const { return modular(*this) += rhs; }
constexpr modular& operator += (const modular &rhs) {
if ((value += rhs.value) >= mod) value -= mod;
return *this;
}
constexpr modular operator - (const modular &rhs) const { return modular(*this) -= rhs; }
constexpr modular& operator -= (const modular &rhs) {
if ((value += mod - rhs.value) >= mod) value -= mod;
return *this;
}
constexpr modular operator * (const modular &rhs) const { return modular(*this) *= rhs; }
constexpr modular& operator *= (const modular &rhs) {
value = (max_type) value * rhs.value % mod;
return *this;
}
constexpr modular operator / (const modular &rhs) const { return modular(*this) /= rhs; }
constexpr modular& operator /= (const modular &rhs) { return (*this) *= rhs.inverse(); }
constexpr bool zero() const { return value == 0; }
constexpr bool operator == (const modular &rhs) const { return value == rhs.value; }
constexpr bool operator != (const modular &rhs) const { return value != rhs.value; }
friend std::ostream& operator << (std::ostream &stream, const modular &rhs) {
return stream << rhs.value;
}
};
using modint = modular<998244353>;
int main() {
int N, Q;
std::cin >> N >> Q;
std::vector<int> A(N);
for (auto &x: A) {
std::cin >> x;
}
std::array<std::vector<modint>, 2> dp{};
dp.fill(std::vector<modint>(N + 1));
dp[0][0] = modint(1);
for (int i: range(0, N)) {
auto &cur = dp[i & 1];
auto &next = dp[(i + 1) & 1];
for (int j: range(0, N + 1)) {
if (j + 1 <= N) {
next[j + 1] += cur[j];
}
next[j] += cur[j] * modint(A[i] - 1);
cur[j] = modint(0);
}
}
const auto &ans = dp[N & 1];
while (Q--) {
int x;
std::cin >> x;
std::cout << ans[x] << '\n';
}
return 0;
}
KoD