結果
問題 | No.2062 Sum of Subset mod 999630629 |
ユーザー | tomo0608 |
提出日時 | 2023-08-22 22:43:02 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 49,530 bytes |
コンパイル時間 | 5,243 ms |
コンパイル使用メモリ | 270,232 KB |
実行使用メモリ | 26,840 KB |
最終ジャッジ日時 | 2024-05-10 04:20:50 |
合計ジャッジ時間 | 8,638 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 1 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 9 ms
5,376 KB |
testcase_09 | AC | 7 ms
5,376 KB |
testcase_10 | AC | 6 ms
5,376 KB |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | AC | 6 ms
5,376 KB |
testcase_24 | AC | 7 ms
5,376 KB |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
ソースコード
#pragma region competitive_programming #ifdef __LOCAL #define _GLIBCXX_DEBUG #endif #pragma GCC optimize("Ofast") #include<bits/stdc++.h> //#include<atcoder/dsu> //#include "Rollback_dsu.hpp" //#include "Partial_Persistent_DSU.hpp" //#include "Number_Theory.hpp" #include <cassert> #include <numeric> #include <type_traits> #ifdef _MSC_VER #include <intrin.h> #endif #include <utility> #ifdef _MSC_VER #include <intrin.h> #endif namespace atcoder { namespace internal { constexpr long long safe_mod(long long x, long long m) { x %= m; if (x < 0) x += m; return x; } struct barrett { unsigned int _m; unsigned long long im; explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {} unsigned int umod() const { return _m; } unsigned int mul(unsigned int a, unsigned int b) const { unsigned long long z = a; z *= b; #ifdef _MSC_VER unsigned long long x; _umul128(z, im, &x); #else unsigned long long x = (unsigned long long)(((unsigned __int128)(z)*im) >> 64); #endif unsigned int v = (unsigned int)(z - x * _m); if (_m <= v) v += _m; return v; } }; constexpr long long pow_mod_constexpr(long long x, long long n, int m) { if (m == 1) return 0; unsigned int _m = (unsigned int)(m); unsigned long long r = 1; unsigned long long y = safe_mod(x, m); while (n) { if (n & 1) r = (r * y) % _m; y = (y * y) % _m; n >>= 1; } return r; } constexpr bool is_prime_constexpr(int n) { if (n <= 1) return false; if (n == 2 || n == 7 || n == 61) return true; if (n % 2 == 0) return false; long long d = n - 1; while (d % 2 == 0) d /= 2; constexpr long long bases[3] = {2, 7, 61}; for (long long a : bases) { long long t = d; long long y = pow_mod_constexpr(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = y * y % n; t <<= 1; } if (y != n - 1 && t % 2 == 0) { return false; } } return true; } template <int n> constexpr bool is_prime = is_prime_constexpr(n); constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) { a = safe_mod(a, b); if (a == 0) return {b, 0}; long long s = b, t = a; long long m0 = 0, m1 = 1; while (t) { long long u = s / t; s -= t * u; m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b auto tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } if (m0 < 0) m0 += b / s; return {s, m0}; } constexpr int primitive_root_constexpr(int m) { if (m == 2) return 1; if (m == 167772161) return 3; if (m == 469762049) return 3; if (m == 754974721) return 11; if (m == 998244353) return 3; int divs[20] = {}; divs[0] = 2; int cnt = 1; int x = (m - 1) / 2; while (x % 2 == 0) x /= 2; for (int i = 3; (long long)(i)*i <= x; i += 2) { if (x % i == 0) { divs[cnt++] = i; while (x % i == 0) { x /= i; } } } if (x > 1) { divs[cnt++] = x; } for (int g = 2;; g++) { bool ok = true; for (int i = 0; i < cnt; i++) { if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) { ok = false; break; } } if (ok) return g; } } template <int m> constexpr int primitive_root = primitive_root_constexpr(m); unsigned long long floor_sum_unsigned(unsigned long long n, unsigned long long m, unsigned long long a, unsigned long long b) { unsigned long long ans = 0; while (true) { if (a >= m) { ans += n * (n - 1) / 2 * (a / m); a %= m; } if (b >= m) { ans += n * (b / m); b %= m; } unsigned long long y_max = a * n + b; if (y_max < m) break; n = (unsigned long long)(y_max / m); b = (unsigned long long)(y_max % m); std::swap(m, a); } return ans; } } // namespace internal } // namespace atcoder #include <cassert> #include <numeric> #include <type_traits> namespace atcoder { namespace internal { #ifndef _MSC_VER template <class T> using is_signed_int128 = typename std::conditional<std::is_same<T, __int128_t>::value || std::is_same<T, __int128>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int128 = typename std::conditional<std::is_same<T, __uint128_t>::value || std::is_same<T, unsigned __int128>::value, std::true_type, std::false_type>::type; template <class T> using make_unsigned_int128 = typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t, unsigned __int128>; template <class T> using is_integral = typename std::conditional<std::is_integral<T>::value || is_signed_int128<T>::value || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_signed_int = typename std::conditional<(is_integral<T>::value && std::is_signed<T>::value) || is_signed_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<(is_integral<T>::value && std::is_unsigned<T>::value) || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional< is_signed_int128<T>::value, make_unsigned_int128<T>, typename std::conditional<std::is_signed<T>::value, std::make_unsigned<T>, std::common_type<T>>::type>::type; #else template <class T> using is_integral = typename std::is_integral<T>; template <class T> using is_signed_int = typename std::conditional<is_integral<T>::value && std::is_signed<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<is_integral<T>::value && std::is_unsigned<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional<is_signed_int<T>::value, std::make_unsigned<T>, std::common_type<T>>::type; #endif template <class T> using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>; template <class T> using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>; template <class T> using to_unsigned_t = typename to_unsigned<T>::type; } // namespace internal } // namespace atcoder namespace atcoder { namespace internal { struct modint_base {}; struct static_modint_base : modint_base {}; template <class T> using is_modint = std::is_base_of<modint_base, T>; template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>; } // namespace internal template <int m, std::enable_if_t<(1 <= m)>* = nullptr> struct static_modint : internal::static_modint_base { using mint = static_modint; public: static constexpr int mod() { return m; } static mint raw(int v) { mint x; x._v = v; return x; } static_modint() : _v(0) {} template <class T, internal::is_signed_int_t<T>* = nullptr> static_modint(T v) { long long x = (long long)(v % (long long)(umod())); if (x < 0) x += umod(); _v = (unsigned int)(x); } template <class T, internal::is_unsigned_int_t<T>* = nullptr> static_modint(T v) { _v = (unsigned int)(v % umod()); } unsigned int val() const { return _v; } mint& operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint& operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint& operator+=(const mint& rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint& operator-=(const mint& rhs) { _v -= rhs._v; if (_v >= umod()) _v += umod(); return *this; } mint& operator*=(const mint& rhs) { unsigned long long z = _v; z *= rhs._v; _v = (unsigned int)(z % umod()); return *this; } mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { if (prime) { assert(_v); return pow(umod() - 2); } else { auto eg = internal::inv_gcd(_v, m); assert(eg.first == 1); return eg.second; } } friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint& lhs, const mint& rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint& lhs, const mint& rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static constexpr unsigned int umod() { return m; } static constexpr bool prime = internal::is_prime<m>; }; template <int id> struct dynamic_modint : internal::modint_base { using mint = dynamic_modint; public: static int mod() { return (int)(bt.umod()); } static void set_mod(int m) { assert(1 <= m); bt = internal::barrett(m); } static mint raw(int v) { mint x; x._v = v; return x; } dynamic_modint() : _v(0) {} template <class T, internal::is_signed_int_t<T>* = nullptr> dynamic_modint(T v) { long long x = (long long)(v % (long long)(mod())); if (x < 0) x += mod(); _v = (unsigned int)(x); } template <class T, internal::is_unsigned_int_t<T>* = nullptr> dynamic_modint(T v) { _v = (unsigned int)(v % mod()); } unsigned int val() const { return _v; } mint& operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint& operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint& operator+=(const mint& rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint& operator-=(const mint& rhs) { _v += mod() - rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint& operator*=(const mint& rhs) { _v = bt.mul(_v, rhs._v); return *this; } mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { auto eg = internal::inv_gcd(_v, mod()); assert(eg.first == 1); return eg.second; } friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint& lhs, const mint& rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint& lhs, const mint& rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static internal::barrett bt; static unsigned int umod() { return bt.umod(); } }; template <int id> internal::barrett dynamic_modint<id>::bt(998244353); using modint998244353 = static_modint<998244353>; using modint1000000007 = static_modint<1000000007>; using modint = dynamic_modint<-1>; namespace internal { template <class T> using is_static_modint = std::is_base_of<internal::static_modint_base, T>; template <class T> using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>; template <class> struct is_dynamic_modint : public std::false_type {}; template <int id> struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {}; template <class T> using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>; } // namespace internal } // namespace atcoder namespace tomo0608 { std::istream& operator>>(std::istream& is, atcoder::modint998244353& a) { long long v; is >> v; a = v; return is; } std::ostream& operator<<(std::ostream& os, const atcoder::modint998244353& a) { return os << a.val(); } std::istream& operator>>(std::istream& is, atcoder::modint1000000007& a) { long long v; is >> v; a = v; return is; } std::ostream& operator<<(std::ostream& os, const atcoder::modint1000000007& a) { return os << a.val(); } template<int m> std::istream& operator>>(std::istream& is, atcoder::static_modint<m>& a) { long long v; is >> v; a = v; return is; } template<int m> std::ostream& operator<<(std::ostream& os, const atcoder::static_modint<m>& a) { return os << a.val(); } template<int m> std::istream& operator>>(std::istream& is, atcoder::dynamic_modint<m>& a) { long long v; is >> v; a = v; return is; } template<int m> std::ostream& operator<<(std::ostream& os, const atcoder::dynamic_modint<m>& a) { return os << a.val(); } // Binomial Coefficient of modint template<class mint> struct BiCoef { std::vector<mint> fact_, inv_, finv_; constexpr BiCoef() {} constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) { init(n); } constexpr void init(int n) noexcept { fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1); int MOD = fact_[0].mod(); for (int i = 2; i < n; i++) { fact_[i] = fact_[i - 1] * i; inv_[i] = -inv_[MOD % i] * (MOD / i); finv_[i] = finv_[i - 1] * inv_[i]; } } constexpr mint com(int n, int k) const noexcept { if (n < k || n < 0 || k < 0)return 0; return fact_[n] * finv_[k] * finv_[n - k]; } constexpr mint perm(int n, int k) const noexcept { if (n < k || n < 0 || k < 0)return 0; return fact_[n] * finv_[n - k]; } constexpr mint homo(int n, int r) { // The number of cases where k indistinguishable balls are put into n distinct boxes if (n < 0 || r < 0)return 0; return r == 0 ? 1 : com(n + r - 1, r); } constexpr mint second_stirling_number(int n, int r) { // The number of cases where n distinct balls are put into k indistinguishable boxes, with at least one ball in each box mint ret = 0; for (int i = 0; i <= r; i++) { mint tmp = com(r, i) * mint(i).pow(n); ret += ((r - i) & 1) ? -tmp : tmp; } return ret * finv_[r]; } constexpr mint bell_number(int n, int r) { // The number of cases where n distinct balls are put into k indistinguishable boxes if (n == 0) return 1; r = std::min(r, n); std::vector<mint> pref(r + 1); pref[0] = 1; for (int i = 1; i <= r; i++) { if (i & 1) { pref[i] = pref[i - 1] - finv_[i]; } else { pref[i] = pref[i - 1] + finv_[i]; } } mint ret = 0; for (int i = 1; i <= r; i++) ret += mint(i).pow(n) * fact_[i] * pref[r - i]; return ret; } constexpr mint fact(int n) const noexcept { if (n < 0)return 0; return fact_[n]; } constexpr mint inv(int n) const noexcept { if (n < 0)return 0; return inv_[n]; } constexpr mint finv(int n) const noexcept { if (n < 0)return 0; return finv_[n]; } inline mint operator()(int n, int k) { return com(n, k); } constexpr mint com_naive(long long n, long long k) { if (n < k || n < 0 || k < 0)return 0; mint res = 1; k = std::min(k, n - k); for (int i = 1; i <= k; i++)res *= inv(i) * (n--); return res; } }; } // namespace tomo0608 //typedef atcoder::modint1000000007 mint; typedef atcoder::modint998244353 mint; //#include "Matrix.hpp" //#include<atcoder/convolution> #include <algorithm> #include <array> #include <cassert> #include <type_traits> #include <vector> #ifdef _MSC_VER #include <intrin.h> #endif namespace atcoder { namespace internal { int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } constexpr int bsf_constexpr(unsigned int n) { int x = 0; while (!(n & (1 << x))) x++; return x; } int bsf(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } } // namespace internal } // namespace atcoder namespace atcoder { namespace internal { template <class mint, int g = internal::primitive_root<mint::mod()>, internal::is_static_modint_t<mint>* = nullptr> struct fft_info { static constexpr int rank2 = bsf_constexpr(mint::mod() - 1); std::array<mint, rank2 + 1> root; // root[i]^(2^i) == 1 std::array<mint, rank2 + 1> iroot; // root[i] * iroot[i] == 1 std::array<mint, std::max(0, rank2 - 2 + 1)> rate2; std::array<mint, std::max(0, rank2 - 2 + 1)> irate2; std::array<mint, std::max(0, rank2 - 3 + 1)> rate3; std::array<mint, std::max(0, rank2 - 3 + 1)> irate3; fft_info() { root[rank2] = mint(g).pow((mint::mod() - 1) >> rank2); iroot[rank2] = root[rank2].inv(); for (int i = rank2 - 1; i >= 0; i--) { root[i] = root[i + 1] * root[i + 1]; iroot[i] = iroot[i + 1] * iroot[i + 1]; } { mint prod = 1, iprod = 1; for (int i = 0; i <= rank2 - 2; i++) { rate2[i] = root[i + 2] * prod; irate2[i] = iroot[i + 2] * iprod; prod *= iroot[i + 2]; iprod *= root[i + 2]; } } { mint prod = 1, iprod = 1; for (int i = 0; i <= rank2 - 3; i++) { rate3[i] = root[i + 3] * prod; irate3[i] = iroot[i + 3] * iprod; prod *= iroot[i + 3]; iprod *= root[i + 3]; } } } }; template <class mint, internal::is_static_modint_t<mint>* = nullptr> void butterfly(std::vector<mint>& a) { int n = int(a.size()); int h = internal::ceil_pow2(n); static const fft_info<mint> info; int len = 0; // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed while (len < h) { if (h - len == 1) { int p = 1 << (h - len - 1); mint rot = 1; for (int s = 0; s < (1 << len); s++) { int offset = s << (h - len); for (int i = 0; i < p; i++) { auto l = a[i + offset]; auto r = a[i + offset + p] * rot; a[i + offset] = l + r; a[i + offset + p] = l - r; } if (s + 1 != (1 << len)) rot *= info.rate2[bsf(~(unsigned int)(s))]; } len++; } else { int p = 1 << (h - len - 2); mint rot = 1, imag = info.root[2]; for (int s = 0; s < (1 << len); s++) { mint rot2 = rot * rot; mint rot3 = rot2 * rot; int offset = s << (h - len); for (int i = 0; i < p; i++) { auto mod2 = 1ULL * mint::mod() * mint::mod(); auto a0 = 1ULL * a[i + offset].val(); auto a1 = 1ULL * a[i + offset + p].val() * rot.val(); auto a2 = 1ULL * a[i + offset + 2 * p].val() * rot2.val(); auto a3 = 1ULL * a[i + offset + 3 * p].val() * rot3.val(); auto a1na3imag = 1ULL * mint(a1 + mod2 - a3).val() * imag.val(); auto na2 = mod2 - a2; a[i + offset] = a0 + a2 + a1 + a3; a[i + offset + 1 * p] = a0 + a2 + (2 * mod2 - (a1 + a3)); a[i + offset + 2 * p] = a0 + na2 + a1na3imag; a[i + offset + 3 * p] = a0 + na2 + (mod2 - a1na3imag); } if (s + 1 != (1 << len)) rot *= info.rate3[bsf(~(unsigned int)(s))]; } len += 2; } } } template <class mint, internal::is_static_modint_t<mint>* = nullptr> void butterfly_inv(std::vector<mint>& a) { int n = int(a.size()); int h = internal::ceil_pow2(n); static const fft_info<mint> info; int len = h; // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed while (len) { if (len == 1) { int p = 1 << (h - len); mint irot = 1; for (int s = 0; s < (1 << (len - 1)); s++) { int offset = s << (h - len + 1); for (int i = 0; i < p; i++) { auto l = a[i + offset]; auto r = a[i + offset + p]; a[i + offset] = l + r; a[i + offset + p] = (unsigned long long)(mint::mod() + l.val() - r.val()) * irot.val(); ; } if (s + 1 != (1 << (len - 1))) irot *= info.irate2[bsf(~(unsigned int)(s))]; } len--; } else { int p = 1 << (h - len); mint irot = 1, iimag = info.iroot[2]; for (int s = 0; s < (1 << (len - 2)); s++) { mint irot2 = irot * irot; mint irot3 = irot2 * irot; int offset = s << (h - len + 2); for (int i = 0; i < p; i++) { auto a0 = 1ULL * a[i + offset + 0 * p].val(); auto a1 = 1ULL * a[i + offset + 1 * p].val(); auto a2 = 1ULL * a[i + offset + 2 * p].val(); auto a3 = 1ULL * a[i + offset + 3 * p].val(); auto a2na3iimag = 1ULL * mint((mint::mod() + a2 - a3) * iimag.val()).val(); a[i + offset] = a0 + a1 + a2 + a3; a[i + offset + 1 * p] = (a0 + (mint::mod() - a1) + a2na3iimag) * irot.val(); a[i + offset + 2 * p] = (a0 + a1 + (mint::mod() - a2) + (mint::mod() - a3)) * irot2.val(); a[i + offset + 3 * p] = (a0 + (mint::mod() - a1) + (mint::mod() - a2na3iimag)) * irot3.val(); } if (s + 1 != (1 << (len - 2))) irot *= info.irate3[bsf(~(unsigned int)(s))]; } len -= 2; } } } template <class mint, internal::is_static_modint_t<mint>* = nullptr> std::vector<mint> convolution_naive(const std::vector<mint>& a, const std::vector<mint>& b) { int n = int(a.size()), m = int(b.size()); std::vector<mint> ans(n + m - 1); if (n < m) { for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { ans[i + j] += a[i] * b[j]; } } } else { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ans[i + j] += a[i] * b[j]; } } } return ans; } template <class mint, internal::is_static_modint_t<mint>* = nullptr> std::vector<mint> convolution_fft(std::vector<mint> a, std::vector<mint> b) { int n = int(a.size()), m = int(b.size()); int z = 1 << internal::ceil_pow2(n + m - 1); a.resize(z); internal::butterfly(a); b.resize(z); internal::butterfly(b); for (int i = 0; i < z; i++) { a[i] *= b[i]; } internal::butterfly_inv(a); a.resize(n + m - 1); mint iz = mint(z).inv(); for (int i = 0; i < n + m - 1; i++) a[i] *= iz; return a; } } // namespace internal template <class mint, internal::is_static_modint_t<mint>* = nullptr> std::vector<mint> convolution(std::vector<mint>&& a, std::vector<mint>&& b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; if (std::min(n, m) <= 60) return convolution_naive(a, b); return internal::convolution_fft(a, b); } template <class mint, internal::is_static_modint_t<mint>* = nullptr> std::vector<mint> convolution(const std::vector<mint>& a, const std::vector<mint>& b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; if (std::min(n, m) <= 60) return convolution_naive(a, b); return internal::convolution_fft(a, b); } template <unsigned int mod = 998244353, class T, std::enable_if_t<internal::is_integral<T>::value>* = nullptr> std::vector<T> convolution(const std::vector<T>& a, const std::vector<T>& b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; using mint = static_modint<mod>; std::vector<mint> a2(n), b2(m); for (int i = 0; i < n; i++) { a2[i] = mint(a[i]); } for (int i = 0; i < m; i++) { b2[i] = mint(b[i]); } auto c2 = convolution(move(a2), move(b2)); std::vector<T> c(n + m - 1); for (int i = 0; i < n + m - 1; i++) { c[i] = c2[i].val(); } return c; } std::vector<long long> convolution_ll(const std::vector<long long>& a, const std::vector<long long>& b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; static constexpr unsigned long long MOD1 = 754974721; // 2^24 static constexpr unsigned long long MOD2 = 167772161; // 2^25 static constexpr unsigned long long MOD3 = 469762049; // 2^26 static constexpr unsigned long long M2M3 = MOD2 * MOD3; static constexpr unsigned long long M1M3 = MOD1 * MOD3; static constexpr unsigned long long M1M2 = MOD1 * MOD2; static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3; static constexpr unsigned long long i1 = internal::inv_gcd(MOD2 * MOD3, MOD1).second; static constexpr unsigned long long i2 = internal::inv_gcd(MOD1 * MOD3, MOD2).second; static constexpr unsigned long long i3 = internal::inv_gcd(MOD1 * MOD2, MOD3).second; auto c1 = convolution<MOD1>(a, b); auto c2 = convolution<MOD2>(a, b); auto c3 = convolution<MOD3>(a, b); std::vector<long long> c(n + m - 1); for (int i = 0; i < n + m - 1; i++) { unsigned long long x = 0; x += (c1[i] * i1) % MOD1 * M2M3; x += (c2[i] * i2) % MOD2 * M1M3; x += (c3[i] * i3) % MOD3 * M1M2; long long diff = c1[i] - internal::safe_mod((long long)(x), (long long)(MOD1)); if (diff < 0) diff += MOD1; static constexpr unsigned long long offset[5] = { 0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3}; x -= offset[diff % 5]; c[i] = x; } return c; } } // namespace atcoder namespace tomo0608 { template<typename mint> struct Formal_Power_Series : public std::vector<mint> { using FPS = Formal_Power_Series; using std::vector<mint>::vector; using std::vector<mint>::operator=; void shrink() { while (this->size() && this->back() == mint(0))this->pop_back(); } FPS& operator+=(const FPS& rhs) { if (rhs.size() > this->size())this->resize(rhs.size()); for (int i = 0; i < (int)rhs.size();i++)(*this)[i] += rhs[i]; return *this; } FPS& operator-=(const FPS& rhs) { if (rhs.size() > this->size())this->resize(rhs.size()); for (int i = 0; i < (int)rhs.size();i++)(*this)[i] -= rhs[i]; return *this; } FPS& operator+=(const mint& rhs) { if (this->empty())this->resize(1); (*this)[0] += rhs; return *this; } FPS& operator-=(const mint& rhs) { if (this->empty())this->resize(1); (*this)[0] -= rhs; return *this; } FPS& operator*=(const mint& rhs) { for (auto& e : *this)e *= rhs; return *this; } FPS& operator*=(const FPS& rhs) { *this = atcoder::convolution((*this), rhs); return *this; } FPS& operator/=(const mint& c) { assert(c != mint(0)); *this *= c.inv(); return *this; } FPS& operator/=(const FPS& rhs) { *this *= rhs.inv(); this->resize(rhs.size()); return *this; } friend FPS operator+(const FPS& lhs, const FPS& rhs) { return FPS(lhs) += rhs; } friend FPS operator+(const FPS& lhs, const mint& rhs) { return FPS(lhs) += rhs; } friend FPS operator-(const FPS& lhs, const FPS& rhs) { return FPS(lhs) -= rhs; } friend FPS operator-(const FPS& lhs, const mint& rhs) { return FPS(lhs) -= rhs; } friend FPS operator*(const FPS& lhs, const FPS& rhs) { return FPS(lhs) *= rhs; } friend FPS operator*(const FPS& lhs, const mint& rhs) { return FPS(lhs) *= rhs; } friend FPS operator/(const FPS& lhs, const mint& rhs) { return FPS(lhs) /= rhs; } // friend std::ostream& operator<<(std::ostream& os, FPS& f) { // for (int i = 0; i < f.size(); i++) { // os << f[i] << (i + 1 == f.size() ? "\n" : " "); // } // return (os); // } FPS inv(int deg = -1) const { assert(this->front() != mint(0)); if (deg == -1)deg = this->size(); FPS res = { this->front().inv() }; for (int d = 1; d < deg; d <<= 1) { FPS f(2 * d), g(2 * d); for (int j = 0; j < std::min((int)this->size(), 2 * d); j++)f[j] = (*this)[j]; for (int j = 0; j < d; j++)g[j] = res[j]; atcoder::internal::butterfly(f); atcoder::internal::butterfly(g); for (int j = 0; j < 2 * d; j++)f[j] *= g[j]; atcoder::internal::butterfly_inv(f); f /= 2 * d; for (int j = 0; j < d; j++) { f[j] = f[j + d]; f[j + d] = 0; } atcoder::internal::butterfly(f); for (int j = 0; j < 2 * d; j++)f[j] *= -g[j]; atcoder::internal::butterfly_inv(f); f /= 2 * d; for (int j = 0; j < d; j++)res.emplace_back(f[j]); } res.resize(deg); return res; } FPS operator-() const { FPS res(*this); for (auto& e : res) e = -e; return res; } FPS derivative() const { FPS res(this->begin() + 1, this->end()); res.emplace_back(0); for (int j = 0; j < res.size(); j++)res[j] *= (j + 1); return res; } FPS integral(bool truncate = true) const { FPS res(this->size() + 1 - truncate, 0); for (int j = 0; j < this->size() - truncate; j++)res[j + 1] = (*this)[j] / (j + 1); return res; } FPS log() const { FPS f = this->derivative(); f /= (*this); return f.integral(); } // https://arxiv.org/pdf/1301.5804.pdf FPS exp(int deg = -1) const { assert(this->front() == mint(0)); if (deg == -1)deg = this->size(); FPS f = { 1 }, g = { 1 }; for (int d = 1; d < deg; d <<= 1) { FPS f_next(f); f_next.resize(2 * d, 0); atcoder::internal::butterfly(f_next); g.resize(2 * d, 0); atcoder::internal::butterfly(g); for (int j = 0; j < 2 * d; j++)g[j] = 2 * g[j] - f_next[j] * g[j] * g[j]; atcoder::internal::butterfly_inv(g); g /= 2 * d; g.resize(d); FPS q(2 * d); for (int j = 0; j < d && j < this->size() - 1; j++)q[j] = (*this)[j + 1] * (j + 1); FPS w(2 * d); FPS G(g); G.resize(2 * d, 0); atcoder::internal::butterfly(G); for (int j = 0; j < 2 * d; j++)w[j] = f_next[j] * G[j]; atcoder::internal::butterfly_inv(w); w /= 2 * d; for (int j = 0; j < d; j++) { w[j] = w[j + d]; w[j + d] = 0; } atcoder::internal::butterfly(w); atcoder::internal::butterfly(q); for (int j = 0; j < 2 * d; j++)w[j] *= q[j]; atcoder::internal::butterfly_inv(w); w /= 2 * d; FPS df(f.derivative()); df.resize(2 * d, 0); atcoder::internal::butterfly(df); for (int j = 0;j < 2 * d;j++)df[j] *= G[j]; atcoder::internal::butterfly_inv(df); df /= 2 * d; for (int j = 0; j < d;j++) { w[j + d] = w[j]; w[j] = 0; } w -= df; w = w.integral(); for (int j = 0; j < 2 * d && j < this->size(); j++)w[j] += (*this)[j]; for (int j = 0; j < d;j++) { w[j] = w[j + d]; w[j + d] = 0; } atcoder::internal::butterfly(w); for (int j = 0; j < 2 * d; j++)f_next[j] *= w[j]; atcoder::internal::butterfly_inv(f_next); f_next /= 2 * d; f.resize(2 * d, 0); for (int j = 0; j < d; j++)f[j + d] = f_next[j]; } f.resize(deg); return f; } FPS pow(long long m) const { int n = this->size(); if (m == 0) { auto res = FPS(n, 0); res[0] = 1; return res; } int l = std::find_if(this->begin(), this->end(), [](mint x) {return x != mint(0);}) - this->begin(); if (l == this->size() || (l && m >= (n + l - 1) / l))return FPS(n, 0); FPS res(this->begin() + l, this->end()); mint c = (*this)[l]; res /= c; res.resize(n, 0); res = (res.log() * mint(m)).exp(); res.erase(res.begin() + (n - m * l), res.end()); res *= c.pow(m); std::reverse(res.begin(), res.end()); res.resize(n, 0); std::reverse(res.begin(), res.end()); return res; } }; // for sparse fps template <typename mint> Formal_Power_Series<mint> positive_unit_fractions(int n) { // res[i] = 1 / i, res[0] = 0 length: n+1 static const int mod = mint::mod(); static Formal_Power_Series<mint> res = { 0, 1 }; assert(0 < n); if (n >= mod) n -= mod; while (int(res.size()) <= n) { int num = res.size(); int q = (mod + num - 1) / num; res.emplace_back(res[num * q - mod] * mint(q)); } return res; } template<typename mint> std::vector<std::pair<int, mint>> compress_fps(const Formal_Power_Series<mint>& f) { int n = f.size(); std::vector<std::pair<int, mint>> cf; for (int i = 0; i < n; i++) { if (f[i] != 0)cf.emplace_back(i, f[i]); } return cf; } template<typename mint> Formal_Power_Series<mint> mul_sparse(const Formal_Power_Series<mint>& f, const Formal_Power_Series<mint>& g) { int n = f.size(), m = g.size(); auto cf = compress_fps<mint>(f), cg = compress_fps<mint>(g); Formal_Power_Series<mint> h(n + m - 1); for (auto [i, p] : cf)for (auto [j, q] : cg)h[i + j] += p * q; return h; } template<typename mint> Formal_Power_Series<mint> inv_sparse(const Formal_Power_Series<mint>& f, int deg = -1) { assert(f[0] != 0); if (deg == -1)deg = f.size(); auto cf = compress_fps<mint>(f); Formal_Power_Series<mint> f_inv(deg); f_inv[0] = f[0].inv(); for (int i = 1; i < deg; i++) { for (auto [k, p] : cf) { if (i - k < 0)break; f_inv[i] -= f_inv[i - k] * p; } f_inv[i] *= f_inv[0]; } return f_inv; } template<typename mint> Formal_Power_Series<mint> exp_sparse(const Formal_Power_Series<mint>& f, int deg = -1) { assert(f[0] == 0); if (deg == -1)deg = f.size(); auto cf = compress_fps<mint>(f); Formal_Power_Series<mint> f_exp(deg); Formal_Power_Series<mint> inv_num = positive_unit_fractions<mint>(deg); f_exp[0] = 1; for (int i = 1; i < deg; i++) { for (auto [k, p] : cf) { if (i - k < 0)break; f_exp[i] += f_exp[i - k] * p * k; } f_exp[i] *= inv_num[i]; } return f_exp; } template<typename mint> Formal_Power_Series<mint> log_sparse(const Formal_Power_Series<mint>& f, int deg = -1) { assert(f[0] == 1); if (deg == -1)deg = f.size(); Formal_Power_Series<mint> df = f.derivative(); Formal_Power_Series<mint> df_log = mul_sparse<mint>(df, inv_sparse<mint>(f)); Formal_Power_Series<mint> f_log = df_log.integral(); f_log.resize(deg); return f_log; } template<typename mint> Formal_Power_Series<mint> __pow_sparse_const_1(const Formal_Power_Series<mint>& f, long long k, int deg) { int n = f.size(); assert(n > 0 && f[0] == 1); auto cf = compress_fps<mint>(f); Formal_Power_Series<mint> f_pow_k(deg); Formal_Power_Series<mint> inv_num = positive_unit_fractions<mint>(deg); f_pow_k[0] = 1; for (int i = 1; i < deg; i++) { for (const auto& [j, coef] : cf) { if (i - j < 0) break; f_pow_k[i] += (mint(k) * mint(j) - mint(i - j)) * coef * f_pow_k[i - j]; } f_pow_k[i] *= inv_num[i]; } return f_pow_k; } template<typename mint> Formal_Power_Series<mint> pow_sparse(const Formal_Power_Series<mint>& f, long long k, int deg = -1) { int n = f.size(); if (deg < 0)deg = n; assert(k >= 0); if (k == 0) { Formal_Power_Series<mint> res(deg, 0); if (deg)res[0] = 1; return res; } int l = std::find_if(f.begin(), f.end(), [](mint x) {return x != mint(0);}) - f.begin(); if (l == f.size() || (l && k >= (deg + l - 1) / l))return Formal_Power_Series<mint>(deg, 0); Formal_Power_Series<mint> res(f.begin() + l, f.end()); mint c = f[l]; res /= c; res.resize(deg, 0); res = __pow_sparse_const_1<mint>(res, k, deg); res.erase(res.begin() + (deg - k * l), res.end()); res *= c.pow(k); std::reverse(res.begin(), res.end()); res.resize(deg, 0); std::reverse(res.begin(), res.end()); return res; } } // namespace tomo0608 //#include "Bit_Convolution.hpp" //#include<atcoder/maxflow> //#include<atcoder/mincostflow> //#include "Primal_Dual.hpp" //#include "maxflow_mincap.hpp" //#include<atcoder/fenwicktree> //#include<atcoder/segtree> //#include<atcoder/lazysegtree> //#include "2D_Segment_Tree.hpp" //#include "DisjointSparseTable.hpp" //#include "SWAG.hpp" //#include "Mo_algorithm.hpp" //#include "Heavy_Light_Decomposition.hpp" //#include "Binary_Trie.hpp" //#include "LCT.hpp" //#include "Slope_Trick.hpp" //#include<atcoder/string> //#include<atcoder/scc> //#include "TwoEdgeCC.hpp" namespace tomo0608 { typedef long long ll; typedef long double ld; template <class T> using V = std::vector<T>; template <class T> using VV = V<V<T>>; template <class T> using VVV = V<VV<T>>; typedef std::pair<int, int> pii; typedef std::pair<long long, long long> pll; template<class... T>void input(T&... a) { (std::cin >> ... >> a); }; #define INT(...) int __VA_ARGS__; IN(__VA_ARGS__) #define LL(...) long long __VA_ARGS__; IN(__VA_ARGS__) #define STR(...) string __VA_ARGS__; IN(__VA_ARGS__) #define DBL(...) double __VA_ARGS__; IN(__VA_ARGS__) #define VEC(type, name, size) std::vector<type> name(size);IN(name) #define VECVEC(type, name, h, w) std::vector<std::vector<type>> name(h, std::vector<type>(w));IN(name) template<class T1, class T2> std::istream& operator>>(std::istream& is, std::pair<T1, T2>& p) { is >> p.first >> p.second; return is; } template<class T1, class T2> std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) { os << '(' << p.first << ", " << p.second << ')'; return os; } template<class T> std::istream& operator>>(std::istream& is, std::vector<T>& v) { for (auto& e : v) is >> e; return is; } template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) { for (auto& e : v) os << e << ' '; return os; } template<typename T> std::ostream& operator << (std::ostream& os, std::set<T>& set_var) { os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr;++itr;if (itr != set_var.end()) os << ", ";itr--; }os << "}";return os; } template <typename T, typename U> std::ostream& operator<<(std::ostream& os, std::map<T, U>& map_var) { os << "{";for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << *itr;itr++;if (itr != map_var.end()) os << ", ";itr--; }os << "}";return os; } void IN() {} template <class Head, class... Tail> void IN(Head& head, Tail &...tail) { std::cin >> head; IN(tail...); } void print() { std::cout << '\n'; } template<class T, class... Ts>void print(const T& a, const Ts&... b) { std::cout << a; (std::cout << ... << (std::cout << ' ', b)); std::cout << '\n'; } void drop() { std::cout << '\n';exit(0); } template<class T, class... Ts>void drop(const T& a, const Ts&... b) { std::cout << a; (std::cout << ... << (std::cout << ' ', b)); std::cout << '\n';exit(0); } #ifdef __LOCAL void debug_out() { std::cerr << std::endl; } template < class Head, class... Tail> void debug_out(Head H, Tail... T) { std::cerr << ' ' << H; debug_out(T...); } #define debug(...) std::cerr << 'L' << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define dump(x) std::cerr << 'L' << __LINE__ << " " << #x << " = " << (x) << std::endl #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif #define rep1(a) for(long long i = 0; i < a; i++) #define rep2(i, a) for(long long i = 0; i < a; i++) #define rep3(i, a, b) for(long long i = a; i < b; i++) #define rep4(i, a, b, c) for(long long i = a; i < b; i += c) #define drep1(a) for(long long i = a-1; i >= 0; i--) #define drep2(i, a) for(long long i = a-1; i >= 0; i--) #define drep3(i, a, b) for(long long i = a-1; i >= b; i--) #define drep4(i, a, b, c) for(long long i = a-1; i >= b; i -= c) #define overload4(a, b, c, d, e, ...) e #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define drep(...) overload4(__VA_ARGS__, drep4, drep3, drep2, drep1)(__VA_ARGS__) #define endl '\n' } // namespace tomo0608 namespace tomo0608 { #define ALL(x) x.begin(),x.end() template <class T = long long, class S> T SUM(const S& v) { return accumulate(ALL(v), T(0)); } #define MIN(v) *min_element(ALL(v)) #define MAX(v) *max_element(ALL(v)) #define SORT(v) sort(ALL(v)) #define REVERSE(v) reverse(ALL(v)) #define RSORT(v) sort(ALL(v)); reverse(ALL(v)) #define UNIQUE(x) SORT(x), x.erase(unique(ALL(x)), x.end()) #define lb(c, x) distance((c).begin(), lower_bound(ALL(c), (x))) #define ub(c, x) distance((c).begin(), upper_bound(ALL(c), (x))) template <typename T> void zip(std::vector<T>& x) { std::vector<T> y = x;UNIQUE(y);for (int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); } } template<class T> using priority_queue_rev = std::priority_queue<T, std::vector<T>, std::greater<T>>; template<class T, class U> inline bool chmax(T& a, const U& b) { if (a < b) { a = b; return 1; } return 0; } template<class T, class U> inline bool chmin(T& a, const U& b) { if (a > b) { a = b; return 1; } return 0; } template<class T> inline int count_between(std::vector<T>& a, T l, T r) { return lower_bound(ALL(a), r) - lower_bound(ALL(a), l); } // [l, r) #define bittest(n, k) (((n) >> (k)) & 1) int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } int lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); } int lowbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); } #define perm(v) for(bool permrepflag = true; (permrepflag ? exchange(permrepflag, false) : next_permutation(ALL(v)));) template <typename T, typename S> T ceil(T x, S y) { assert(y); return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y)); } template <typename T, typename S> T floor(T x, S y) { assert(y); return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1))); } } using namespace atcoder; using namespace std; using namespace tomo0608; int dx[8] = { 1, 0, -1, 0, 1, 1, -1, -1 }; int dy[8] = { 0, 1, 0, -1, 1, -1, -1, 1 }; // インタラクティブ問題のときは出力するたびにcout.flush();を忘れない!!!!! void solve(); int main() { std::cin.tie(0); std::ios_base::sync_with_stdio(false); std::cout << std::setprecision(20); int codeforces = 1; //cin >> codeforces; while (codeforces--) { solve(); } return 0; } #pragma endregion const int m2 = 999630629; typedef Formal_Power_Series<mint> fps; void solve() { INT(n); VEC(ll, a, n); ll sum_a = SUM(a); // sum_a < 2 * m2 mint ans = sum_a * mint(2).pow(n-1); if(sum_a < m2)drop(ans); // \sum_{i \in S}a_i >= m2となるようなSの個数を求める // \sum_{i \in T}a_i <= sum_a - m2となるようなTの個数を求めればよい ll sz = sum_a - m2 + 1; fps sum_log(sz); BiCoef<mint> bc(sz); rep(i, n){ // log(1 + x^a_i)を求める for(int j = a[i], num = 1, c = 1; j < sz; j += a[i], num++, c *= -1){ sum_log[j] += bc.inv(num) * c; } } fps prd = sum_log.exp(); mint cnt = mint(2).pow(n); rep(i, sz)cnt -= prd[i]; print(ans - cnt * m2); }