結果
問題 | No.8046 yukicoderの過去問 |
ユーザー | kyon2326 |
提出日時 | 2021-01-13 13:55:20 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,129 ms / 2,000 ms |
コード長 | 38,423 bytes |
コンパイル時間 | 3,654 ms |
コンパイル使用メモリ | 243,992 KB |
実行使用メモリ | 21,476 KB |
最終ジャッジ日時 | 2024-11-22 08:03:18 |
合計ジャッジ時間 | 9,766 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 1,127 ms
21,344 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 1,119 ms
21,476 KB |
testcase_06 | AC | 1,125 ms
21,348 KB |
testcase_07 | AC | 1,129 ms
21,352 KB |
testcase_08 | AC | 1,121 ms
21,260 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; /* #include <atcoder/all> using namespace atcoder; */ /* #include <boost/multiprecision/cpp_int.hpp> #include <boost/multiprecision/cpp_dec_float.hpp> using bll = boost::multiprecision::cpp_int; using bdouble = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<100>>; using namespace boost::multiprecision; */ #if defined(LOCAL_TEST) || defined(LOCAL_DEV) #define BOOST_STACKTRACE_USE_ADDR2LINE #define BOOST_STACKTRACE_ADDR2LINE_LOCATION /usr/local/opt/binutils/bin/addr2line #define _GNU_SOURCE 1 #include <boost/stacktrace.hpp> #endif #ifdef LOCAL_TEST namespace std { template<typename T> class dvector : public std::vector<T> { public: dvector() : std::vector<T>() {} explicit dvector(size_t n, const T& value = T()) : std::vector<T>(n, value) {} dvector(const std::vector<T>& v) : std::vector<T>(v) {} dvector(const std::initializer_list<T> il) : std::vector<T>(il) {} dvector(const std::string::iterator first, const std::string::iterator last) : std::vector<T>(first, last) {} dvector(const typename std::vector<T>::iterator first, const typename std::vector<T>::iterator last) : std::vector<T>(first, last) {} dvector(const typename std::vector<T>::reverse_iterator first, const typename std::vector<T>::reverse_iterator last) : std::vector<T>(first, last) {} dvector(const typename std::vector<T>::const_iterator first, const typename std::vector<T>::const_iterator last) : std::vector<T>(first, last) {} dvector(const typename std::vector<T>::const_reverse_iterator first, const typename std::vector<T>::const_reverse_iterator last) : std::vector<T>(first, last) {} T& operator[](size_t n) { if (this->size() <= n) { std::cerr << boost::stacktrace::stacktrace() << '\n' << "vector::_M_range_check: __n (which is " << n << ") >= this->size() (which is " << this->size() << ")" << '\n'; } return this->at(n); } const T& operator[](size_t n) const { if (this->size() <= n) { std::cerr << boost::stacktrace::stacktrace() << '\n' << "vector::_M_range_check: __n (which is " << n << ") >= this->size() (which is " << this->size() << ")" << '\n'; } return this->at(n); } }; } class dbool { private: bool boolvalue; public: dbool() : boolvalue(false) {} dbool(bool b) : boolvalue(b) {} operator bool&() { return boolvalue; } operator const bool&() const { return boolvalue; } }; #define vector dvector #define bool dbool class SIGFPE_exception : std::exception {}; class SIGSEGV_exception : std::exception {}; void catch_SIGFPE([[maybe_unused]] int e) { std::cerr << boost::stacktrace::stacktrace() << '\n'; throw SIGFPE_exception(); } void catch_SIGSEGV([[maybe_unused]] int e) { std::cerr << boost::stacktrace::stacktrace() << '\n'; throw SIGSEGV_exception(); } signed convertedmain(); signed main() { signal(SIGFPE, catch_SIGFPE); signal(SIGSEGV, catch_SIGSEGV); return convertedmain(); } #define main() convertedmain() #endif #ifdef LOCAL_DEV template<typename T1, typename T2> std::ostream& operator<<(std::ostream& s, const std::pair<T1, T2>& p) { return s << "(" << p.first << ", " << p.second << ")"; } template <typename T, size_t N> std::ostream& operator<<(std::ostream& s, const std::array<T, N>& a) { s << "{ "; for (size_t i = 0; i < N; ++i){ s << a[i] << "\t"; } s << "}"; return s; } template<typename T> std::ostream& operator<<(std::ostream& s, const std::set<T>& se) { s << "{ "; for (auto itr = se.begin(); itr != se.end(); ++itr){ s << (*itr) << "\t"; } s << "}"; return s; } template<typename T> std::ostream& operator<<(std::ostream& s, const std::multiset<T>& se) { s << "{ "; for (auto itr = se.begin(); itr != se.end(); ++itr){ s << (*itr) << "\t"; } s << "}"; return s; } template<typename T1, typename T2> std::ostream& operator<<(std::ostream& s, const std::map<T1, T2>& m) { s << "{\n"; for (auto itr = m.begin(); itr != m.end(); ++itr){ s << "\t" << (*itr).first << " : " << (*itr).second << "\n"; } s << "}"; return s; } template<typename T> std::ostream& operator<<(std::ostream& s, const std::deque<T>& v) { for (size_t i = 0; i < v.size(); ++i){ s << v[i]; if (i < v.size() - 1) s << "\t"; } return s; } template<typename T> std::ostream& operator<<(std::ostream& s, const std::vector<T>& v) { for (size_t i = 0; i < v.size(); ++i){ s << v[i]; if (i < v.size() - 1) s << "\t"; } return s; } template<typename T> std::ostream& operator<<(std::ostream& s, const std::vector<std::vector<T>>& vv) { s << "\\\n"; for (size_t i = 0; i < vv.size(); ++i){ s << vv[i] << "\n"; } return s; } void debug_impl() { std::cerr << '\n'; } template<typename Head, typename... Tail> void debug_impl(Head head, Tail... tail) { std::cerr << " " << head << (sizeof...(tail) ? "," : ""); debug_impl(tail...); } #define debug(...) do { std::cerr << ":" << __LINE__ << " (" << #__VA_ARGS__ << ") ="; debug_impl(__VA_ARGS__); } while (false) constexpr inline long long prodlocal([[maybe_unused]] long long prod, [[maybe_unused]] long long local) { return local; } #else #define debug(...) do {} while (false) constexpr inline long long prodlocal([[maybe_unused]] long long prod, [[maybe_unused]] long long local) { return prod; } #endif //#define int long long using ll = long long; //INT_MAX = (1<<31)-1 = 2147483647, INT64_MAX = (1LL<<63)-1 = 9223372036854775807 constexpr ll INF = numeric_limits<ll>::max() == INT_MAX ? (ll)1e9 + 7 : (ll)1e18; constexpr ll MOD = (ll)1e9 + 7; //primitive root = 5 //constexpr ll MOD = 998244353; //primitive root = 3 constexpr double EPS = 1e-9; constexpr ll dx[4] = {1, 0, -1, 0}; constexpr ll dy[4] = {0, 1, 0, -1}; constexpr ll dx8[8] = {1, 0, -1, 0, 1, 1, -1, -1}; constexpr ll dy8[8] = {0, 1, 0, -1, 1, -1, 1, -1}; #define rep(i, n) for(ll i=0, i##_length=(n); i< i##_length; ++i) #define repeq(i, n) for(ll i=1, i##_length=(n); i<=i##_length; ++i) #define rrep(i, n) for(ll i=(n)-1; i>=0; --i) #define rrepeq(i, n) for(ll i=(n) ; i>=1; --i) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() void p() { std::cout << '\n'; } template<typename Head, typename... Tail> void p(Head head, Tail... tail) { std::cout << head << (sizeof...(tail) ? " " : ""); p(tail...); } template<typename T> inline void pv(std::vector<T>& v) { for(ll i=0, N=v.size(); i<N; i++) std::cout << v[i] << " \n"[i==N-1]; } template<typename T> inline bool chmax(T& a, T b) { return a < b && (a = b, true); } template<typename T> inline bool chmin(T& a, T b) { return a > b && (a = b, true); } template<typename T> inline void uniq(std::vector<T>& v) { v.erase(std::unique(v.begin(), v.end()), v.end()); } template<typename T> inline ll sz(T& v) { return v.size(); } /*-----8<-----template-----8<-----*/ map<ll,ll> inv_cache; struct Modint{ unsigned long long num = 0; constexpr Modint() noexcept {} //constexpr Modint(const Modint &x) noexcept : num(x.num){} inline constexpr operator ll() const noexcept { return num; } inline constexpr Modint& operator+=(Modint x) noexcept { num += x.num; if(num >= MOD) num -= MOD; return *this; } inline constexpr Modint& operator++() noexcept { if(num == MOD - 1) num = 0; else num++; return *this; } inline constexpr Modint operator++(int) noexcept { Modint ans(*this); operator++(); return ans; } inline constexpr Modint operator-() const noexcept { return Modint(0) -= *this; } inline constexpr Modint& operator-=(Modint x) noexcept { if(num < x.num) num += MOD; num -= x.num; return *this; } inline constexpr Modint& operator--() noexcept { if(num == 0) num = MOD - 1; else num--; return *this; } inline constexpr Modint operator--(int) noexcept { Modint ans(*this); operator--(); return ans; } inline constexpr Modint& operator*=(Modint x) noexcept { num = (unsigned long long)(num) * x.num % MOD; return *this; } inline Modint& operator/=(Modint x) noexcept { return operator*=(x.inv()); } template<class T> constexpr Modint(T x) noexcept { using U = typename conditional<sizeof(T) >= 4, T, int>::type; U y = x; y %= U(MOD); if(y < 0) y += MOD; num = (unsigned long long)(y); } template<class T> inline constexpr Modint operator+(T x) const noexcept { return Modint(*this) += x; } template<class T> inline constexpr Modint& operator+=(T x) noexcept { return operator+=(Modint(x)); } template<class T> inline constexpr Modint operator-(T x) const noexcept { return Modint(*this) -= x; } template<class T> inline constexpr Modint& operator-=(T x) noexcept { return operator-=(Modint(x)); } template<class T> inline constexpr Modint operator*(T x) const noexcept { return Modint(*this) *= x; } template<class T> inline constexpr Modint& operator*=(T x) noexcept { return operator*=(Modint(x)); } template<class T> inline constexpr Modint operator/(T x) const noexcept { return Modint(*this) /= x; } template<class T> inline constexpr Modint& operator/=(T x) noexcept { return operator/=(Modint(x)); } inline Modint inv() const noexcept { return inv_cache.count(num) ? inv_cache[num] : inv_cache[num] = inv_calc(); } inline constexpr ll inv_calc() const noexcept { ll x = 0, y = 0; extgcd(num, MOD, x, y); return x; } static inline constexpr ll extgcd(ll a, ll b, ll &x, ll &y) noexcept { ll g = a; x = 1; y = 0; if(b){ g = extgcd(b, a % b, y, x); y -= a / b * x; } return g; } inline constexpr Modint pow(ll x) const noexcept { Modint ans = 1, cnt = x>=0 ? *this : inv(); if(x<0) x = -x; while(x){ if(x & 1) ans *= cnt; cnt *= cnt; x /= 2; } return ans; } static inline constexpr ll get_mod() { return MOD; } }; std::istream& operator>>(std::istream& is, Modint& x){ ll a; is>>a; x = a; return is; } inline constexpr Modint operator""_M(unsigned long long x) noexcept { return Modint(x); } std::vector<Modint> fac(1, 1), inv(1, 1); inline void reserve(size_t a){ if(fac.size() >= a) return; if(a < fac.size() * 2) a = fac.size() * 2; if(a >= MOD) a = MOD; fac.reserve(a); while(fac.size() < a) fac.push_back(fac.back() * Modint(fac.size())); inv.resize(fac.size()); inv.back() = fac.back().inv(); for(ll i = inv.size() - 1; !inv[i - 1]; i--) inv[i - 1] = inv[i] * i; } inline Modint factorial(ll n){ if(n < 0) return 0; reserve(n + 1); return fac[n]; } inline Modint nPk(ll n, ll r){ if(r < 0 || n < r) return 0; if(n >> 24){ Modint ans = 1; for(ll i = 0; i < r; i++) ans *= n--; return ans; } reserve(n + 1); return fac[n] * inv[n - r]; } inline Modint nCk(ll n, ll r){ if(r < 0 || n < r) return 0; r = min(r, n - r); reserve(r + 1); return inv[r] * nPk(n, r); } inline Modint nHk(ll n, ll r){ return nCk(n + r - 1, n - 1); } //n種類のものから重複を許してr個選ぶ=玉r個と仕切りn-1個 inline Modint catalan(ll n){ reserve(n * 2 + 1); return fac[n * 2] * inv[n] * inv[n + 1]; } //// /* template< typename T > struct FormalPowerSeries : vector< T > { using vector< T >::vector; using P = FormalPowerSeries; using MULT = function< vector< T >(P, P) >; using FFT = function< void(P &) >; using SQRT = function< T(T) >; static MULT &get_mult() { static MULT mult = nullptr; return mult; } static void set_mult(MULT f) { get_mult() = f; } static FFT &get_fft() { static FFT fft = nullptr; return fft; } static FFT &get_ifft() { static FFT ifft = nullptr; return ifft; } static void set_fft(FFT f, FFT g) { get_fft() = f; get_ifft() = g; } static SQRT &get_sqrt() { static SQRT sqr = nullptr; return sqr; } static void set_sqrt(SQRT sqr) { get_sqrt() = sqr; } void shrink() { while(this->size() && this->back() == T(0)) this->pop_back(); } P operator+(const P &r) const { return P(*this) += r; } P operator+(const T &v) const { return P(*this) += v; } P operator-(const P &r) const { return P(*this) -= r; } P operator-(const T &v) const { return P(*this) -= v; } P operator*(const P &r) const { return P(*this) *= r; } P operator*(const T &v) const { return P(*this) *= v; } P operator/(const P &r) const { return P(*this) /= r; } P operator%(const P &r) const { return P(*this) %= r; } P &operator+=(const P &r) { if(r.size() > this->size()) this->resize(r.size()); for(int i = 0; i < (int)r.size(); i++) (*this)[i] += r[i]; return *this; } P &operator+=(const T &r) { if(this->empty()) this->resize(1); (*this)[0] += r; return *this; } P &operator-=(const P &r) { if(r.size() > this->size()) this->resize(r.size()); for(int i = 0; i < (int)r.size(); i++) (*this)[i] -= r[i]; shrink(); return *this; } P &operator-=(const T &r) { if(this->empty()) this->resize(1); (*this)[0] -= r; shrink(); return *this; } P &operator*=(const T &v) { const int n = (int) this->size(); for(int k = 0; k < n; k++) (*this)[k] *= v; return *this; } P &operator*=(const P &r) { if(this->empty() || r.empty()) { this->clear(); return *this; } assert(get_mult() != nullptr); auto ret = get_mult()(*this, r); return *this = P(begin(ret), end(ret)); } P &operator%=(const P &r) { return *this -= *this / r * r; } P operator-() const { P ret(this->size()); for(int i = 0; i < this->size(); i++) ret[i] = -(*this)[i]; return ret; } P &operator/=(const P &r) { if(this->size() < r.size()) { this->clear(); return *this; } int n = this->size() - r.size() + 1; return *this = (rev().pre(n) * r.rev().inv(n)).pre(n).rev(n); } P dot(P r) const { P ret(min(this->size(), r.size())); for(int i = 0; i < ret.size(); i++) ret[i] = (*this)[i] * r[i]; return ret; } P pre(int sz) const { return P(begin(*this), begin(*this) + min((int) this->size(), sz)); } P operator>>(int sz) const { if((int)this->size() <= sz) return {}; P ret(*this); ret.erase(ret.begin(), ret.begin() + sz); return ret; } P operator<<(int sz) const { P ret(*this); ret.insert(ret.begin(), sz, T(0)); return ret; } P rev(int deg = -1) const { P ret(*this); if(deg != -1) ret.resize(deg, T(0)); reverse(begin(ret), end(ret)); return ret; } P diff() const { const int n = (int) this->size(); P ret(max(0, n - 1)); for(int i = 1; i < n; i++) ret[i - 1] = (*this)[i] * T(i); return ret; } P integral() const { const int n = (int) this->size(); P ret(n + 1); ret[0] = T(0); for(int i = 0; i < n; i++) ret[i + 1] = (*this)[i] / T(i + 1); return ret; } // F(0) must not be 0 P inv(int deg = -1) const { assert(((*this)[0]) != T(0)); const int n = (int) this->size(); if(deg == -1) deg = n; if(get_fft() != nullptr) { P ret(*this); ret.resize(deg, T(0)); return ret.inv_fast(); } P ret({T(1) / (*this)[0]}); for(int i = 1; i < deg; i <<= 1) { ret = (ret + ret - ret * ret * pre(i << 1)).pre(i << 1); } return ret.pre(deg); } // F(0) must be 1 P log(int deg = -1) const { assert((*this)[0] == 1); const int n = (int) this->size(); if(deg == -1) deg = n; return (this->diff() * this->inv(deg)).pre(deg - 1).integral(); } P sqrt(int deg = -1) const { const int n = (int) this->size(); if(deg == -1) deg = n; if((*this)[0] == T(0)) { for(int i = 1; i < n; i++) { if((*this)[i] != T(0)) { if(i & 1) return {}; if(deg - i / 2 <= 0) break; auto ret = (*this >> i).sqrt(deg - i / 2); if(ret.empty()) return {}; ret = ret << (i / 2); if(ret.size() < deg) ret.resize(deg, T(0)); return ret; } } return P(deg, 0); } P ret; if(get_sqrt() == nullptr) { assert((*this)[0] == T(1)); ret = {T(1)}; } else { auto sqr = get_sqrt()((*this)[0]); if(sqr * sqr != (*this)[0]) return {}; ret = {T(sqr)}; } T inv2 = T(1) / T(2); for(int i = 1; i < deg; i <<= 1) { ret = (ret + pre(i << 1) * ret.inv(i << 1)) * inv2; } return ret.pre(deg); } // F(0) must be 0 P exp(int deg = -1) const { assert((*this)[0] == T(0)); const int n = (int) this->size(); if(deg == -1) deg = n; if(get_fft() != nullptr) { P ret(*this); ret.resize(deg, T(0)); return ret.exp_rec(); } P ret({T(1)}); for(int i = 1; i < deg; i <<= 1) { ret = (ret * (pre(i << 1) + T(1) - ret.log(i << 1))).pre(i << 1); } return ret.pre(deg); } P online_convolution_exp(const P &conv_coeff) const { const int n = (int) conv_coeff.size(); assert((n & (n - 1)) == 0); vector< P > conv_ntt_coeff; auto& fft = get_fft(); auto& ifft = get_ifft(); for(int i = n; i >= 1; i >>= 1) { P g(conv_coeff.pre(i)); fft(g); conv_ntt_coeff.emplace_back(g); } P conv_arg(n), conv_ret(n); auto rec = [&](auto rec, int l, int r, int d) -> void { if(r - l <= 16) { for(int i = l; i < r; i++) { T sum = 0; for(int j = l; j < i; j++) sum += conv_arg[j] * conv_coeff[i - j]; conv_ret[i] += sum; conv_arg[i] = i == 0 ? T(1) : conv_ret[i] / i; } } else { int m = (l + r) / 2; rec(rec, l, m, d + 1); P pre(r - l); for(int i = 0; i < m - l; i++) pre[i] = conv_arg[l + i]; fft(pre); for(int i = 0; i < r - l; i++) pre[i] *= conv_ntt_coeff[d][i]; ifft(pre); for(int i = 0; i < r - m; i++) conv_ret[m + i] += pre[m + i - l]; rec(rec, m, r, d + 1); } }; rec(rec, 0, n, 0); return conv_arg; } P exp_rec() const { assert((*this)[0] == T(0)); const int n = (int) this->size(); int m = 1; while(m < n) m *= 2; P conv_coeff(m); for(int i = 1; i < n; i++) conv_coeff[i] = (*this)[i] * i; return online_convolution_exp(conv_coeff).pre(n); } P inv_fast() const { assert(((*this)[0]) != T(0)); const int n = (int) this->size(); P res{T(1) / (*this)[0]}; for(int d = 1; d < n; d <<= 1) { P f(2 * d), g(2 * d); for(int j = 0; j < min(n, 2 * d); j++) f[j] = (*this)[j]; for(int j = 0; j < d; j++) g[j] = res[j]; get_fft()(f); get_fft()(g); for(int j = 0; j < 2 * d; j++) f[j] *= g[j]; get_ifft()(f); for(int j = 0; j < d; j++) { f[j] = 0; f[j + d] = -f[j + d]; } get_fft()(f); for(int j = 0; j < 2 * d; j++) f[j] *= g[j]; get_ifft()(f); for(int j = 0; j < d; j++) f[j] = res[j]; res = f; } return res.pre(n); } P pow(int64_t k, int deg = -1) const { const int n = (int) this->size(); if(deg == -1) deg = n; for(int i = 0; i < n; i++) { if((*this)[i] != T(0)) { T rev = T(1) / (*this)[i]; P ret = (((*this * rev) >> i).log() * k).exp() * (T((*this)[i]).pow(k)); if(i * k > deg) return P(deg, T(0)); ret = (ret << (i * k)).pre(deg); if((int)ret.size() < deg) ret.resize(deg, T(0)); return ret; } } return *this; } T eval(T x) const { T r = 0, w = 1; for(auto &v : *this) { r += w * v; w *= x; } return r; } P pow_mod(int64_t n, P mod) const { P modinv = mod.rev().inv(); auto get_div = [&](P base) { if(base.size() < mod.size()) { base.clear(); return base; } int n = base.size() - mod.size() + 1; return (base.rev().pre(n) * modinv.pre(n)).pre(n).rev(n); }; P x(*this), ret{1}; while(n > 0) { if(n & 1) { ret *= x; ret -= get_div(ret) * mod; } x *= x; x -= get_div(x) * mod; n >>= 1; } return ret; } }; */ template< typename T > struct FormalPowerSeries : vector< T > { using vector< T >::vector; using P = FormalPowerSeries; using MULT = function< vector< T >(P, P) >; using FFT = function< void(P &) >; using SQRT = function< T(T) >; static MULT &get_mult() { static MULT mult = nullptr; return mult; } static void set_mult(MULT f) { get_mult() = f; } static FFT &get_fft() { static FFT fft = nullptr; return fft; } static FFT &get_ifft() { static FFT ifft = nullptr; return ifft; } static void set_fft(FFT f, FFT g) { get_fft() = f; get_ifft() = g; if(get_mult() == nullptr) { auto mult = [&](P a, P b) { int need = a.size() + b.size() - 1; int nbase = 1; while((1 << nbase) < need) nbase++; int sz = 1 << nbase; a.resize(sz, T(0)); b.resize(sz, T(0)); get_fft()(a); get_fft()(b); for(int i = 0; i < sz; i++) a[i] *= b[i]; get_ifft()(a); a.resize(need); return a; }; set_mult(mult); } } static SQRT &get_sqrt() { static SQRT sqr = nullptr; return sqr; } static void set_sqrt(SQRT sqr) { get_sqrt() = sqr; } void shrink() { while(this->size() && this->back() == T(0)) this->pop_back(); } P operator+(const P &r) const { return P(*this) += r; } P operator+(const T &v) const { return P(*this) += v; } P operator-(const P &r) const { return P(*this) -= r; } P operator-(const T &v) const { return P(*this) -= v; } P operator*(const P &r) const { return P(*this) *= r; } P operator*(const T &v) const { return P(*this) *= v; } P operator/(const P &r) const { return P(*this) /= r; } P operator%(const P &r) const { return P(*this) %= r; } P &operator+=(const P &r) { if(r.size() > this->size()) this->resize(r.size()); for(int i = 0; i < (int)r.size(); i++) (*this)[i] += r[i]; return *this; } P &operator+=(const T &r) { if(this->empty()) this->resize(1); (*this)[0] += r; return *this; } P &operator-=(const P &r) { if(r.size() > this->size()) this->resize(r.size()); for(int i = 0; i < (int)r.size(); i++) (*this)[i] -= r[i]; shrink(); return *this; } P &operator-=(const T &r) { if(this->empty()) this->resize(1); (*this)[0] -= r; shrink(); return *this; } P &operator*=(const T &v) { const int n = (int) this->size(); for(int k = 0; k < n; k++) (*this)[k] *= v; return *this; } P &operator*=(const P &r) { if(this->empty() || r.empty()) { this->clear(); return *this; } assert(get_mult() != nullptr); auto ret = get_mult()(*this, r); return *this = P(begin(ret), end(ret)); } P &operator%=(const P &r) { return *this -= *this / r * r; } P operator-() const { P ret(this->size()); for(int i = 0; i < (int)this->size(); i++) ret[i] = -(*this)[i]; return ret; } P &operator/=(const P &r) { if(this->size() < r.size()) { this->clear(); return *this; } int n = this->size() - r.size() + 1; return *this = (rev().pre(n) * r.rev().inv(n)).pre(n).rev(n); } P dot(P r) const { P ret(min(this->size(), r.size())); for(int i = 0; i < (int)ret.size(); i++) ret[i] = (*this)[i] * r[i]; return ret; } P pre(int sz) const { return P(begin(*this), begin(*this) + min((int) this->size(), sz)); } P operator>>(int sz) const { if(this->size() <= sz) return {}; P ret(*this); ret.erase(ret.begin(), ret.begin() + sz); return ret; } P operator<<(int sz) const { P ret(*this); ret.insert(ret.begin(), sz, T(0)); return ret; } P rev(int deg = -1) const { P ret(*this); if(deg != -1) ret.resize(deg, T(0)); reverse(begin(ret), end(ret)); return ret; } T operator()(T x) const { T r = 0, w = 1; for(auto &v : *this) { r += w * v; w *= x; } return r; } // https://opt-cp.com/fps-implementation/ // multiply and divide (1 + cz^d) P mul(const ll d, const T c) { P ret(*this); int n = ret.size(); if (c == T(1)) for(int i=n-d-1; i>=0; --i) ret[i+d] += ret[i]; else if (c == T(-1)) for(int i=n-d-1; i>=0; --i) ret[i+d] -= ret[i]; else for(int i=n-d-1; i>=0; --i) ret[i+d] += ret[i] * c; return ret; } P div(const ll d, const T c) { P ret(*this); int n = ret.size(); if (c == T(1)) for(int i=0; i<n-d; ++i) ret[i+d] -= ret[i]; else if (c == T(-1)) for(int i=0; i<n-d; ++i) ret[i+d] += ret[i]; else for(int i=0; i<n-d; ++i) ret[i+d] -= ret[i] * c; return ret; } // sparse P mul(vector<pair<ll, T>> g) { if ((int)g.size() == 2 && g[0] == pair<ll, T>(0, 1)) return mul(g[1].first, g[1].second); P ret(*this); int n = ret.size(); auto [d, c] = g.front(); if (d == 0) g.erase(g.begin()); else c = 0; for(int i=n-1; i>=0; i--){ ret[i] *= c; for (auto&& [j, b] : g) { if (j > i) break; ret[i] += ret[i-j] * b; } } return ret; } // sparse, required: "g[0] == (0, c)" and "c != 0" P div(vector<pair<ll, T>> g) { if ((int)g.size() == 2 && g[0] == pair<ll, T>(0, 1)) return div(g[1].first, g[1].second); P ret(*this); int n = ret.size(); auto [d, c] = g.front(); assert(d == 0 && c != T(0)); g.erase(g.begin()); for(int i=0; i<n; i++) { for (auto&& [j, b] : g) { if (j > i) break; ret[i] -= ret[i-j] * b; } ret[i] /= c; } return ret; } P diff() const; P integral() const; // F(0) must not be 0 P inv_fast() const; P inv(int deg = -1) const; // F(0) must be 1 P log(int deg = -1) const; P sqrt(int deg = -1) const; // F(0) must be 0 P exp_fast(int deg = -1) const; P exp(int deg = -1) const; P pow(int64_t k, int deg = -1) const; P mod_pow(int64_t k, P g) const; P taylor_shift(T c) const; }; template< typename T > typename FormalPowerSeries< T >::P FormalPowerSeries< T >::diff() const { const int n = (int) this->size(); P ret(max(0, n - 1)); for(int i = 1; i < n; i++) ret[i - 1] = (*this)[i] * T(i); return ret; } template< typename T > typename FormalPowerSeries< T >::P FormalPowerSeries< T >::integral() const { const int n = (int) this->size(); P ret(n + 1); ret[0] = T(0); for(int i = 0; i < n; i++) ret[i + 1] = (*this)[i] / T(i + 1); return ret; } template< typename T > typename FormalPowerSeries< T >::P FormalPowerSeries< T >::inv_fast() const { assert(((*this)[0]) != T(0)); const int n = (int) this->size(); P res{T(1) / (*this)[0]}; for(int d = 1; d < n; d <<= 1) { P f(2 * d), g(2 * d); for(int j = 0; j < min(n, 2 * d); j++) f[j] = (*this)[j]; for(int j = 0; j < d; j++) g[j] = res[j]; get_fft()(f); get_fft()(g); for(int j = 0; j < 2 * d; j++) f[j] *= g[j]; get_ifft()(f); for(int j = 0; j < d; j++) { f[j] = 0; f[j + d] = -f[j + d]; } get_fft()(f); for(int j = 0; j < 2 * d; j++) f[j] *= g[j]; get_ifft()(f); for(int j = 0; j < d; j++) f[j] = res[j]; res = f; } return res.pre(n); } template< typename T > typename FormalPowerSeries< T >::P FormalPowerSeries< T >::inv(int deg) const { assert(((*this)[0]) != T(0)); const int n = (int) this->size(); if(deg == -1) deg = n; if(get_fft() != nullptr) { P ret(*this); ret.resize(deg, T(0)); return ret.inv_fast(); } P ret({T(1) / (*this)[0]}); for(int i = 1; i < deg; i <<= 1) { ret = (ret + ret - ret * ret * pre(i << 1)).pre(i << 1); } return ret.pre(deg); } template< typename T > typename FormalPowerSeries< T >::P FormalPowerSeries< T >::log(int deg) const { assert((*this)[0] == 1); const int n = (int) this->size(); if(deg == -1) deg = n; return (this->diff() * this->inv(deg)).pre(deg - 1).integral(); } template< typename T > typename FormalPowerSeries< T >::P FormalPowerSeries< T >::sqrt(int deg) const { const int n = (int) this->size(); if(deg == -1) deg = n; if((*this)[0] == T(0)) { for(int i = 1; i < n; i++) { if((*this)[i] != T(0)) { if(i & 1) return {}; if(deg - i / 2 <= 0) break; auto ret = (*this >> i).sqrt(deg - i / 2); if(ret.empty()) return {}; ret = ret << (i / 2); if(ret.size() < deg) ret.resize(deg, T(0)); return ret; } } return P(deg, 0); } P ret; if(get_sqrt() == nullptr) { assert((*this)[0] == T(1)); ret = {T(1)}; } else { auto sqr = get_sqrt()((*this)[0]); if(sqr * sqr != (*this)[0]) return {}; ret = {T(sqr)}; } T inv2 = T(1) / T(2); for(int i = 1; i < deg; i <<= 1) { ret = (ret + pre(i << 1) * ret.inv(i << 1)) * inv2; } return ret.pre(deg); } template< typename T > typename FormalPowerSeries< T >::P FormalPowerSeries< T >::exp_fast(int deg) const { if(deg == -1) deg = this->size(); assert((*this)[0] == T(0)); P inv; inv.reserve(deg + 1); inv.push_back(T(0)); inv.push_back(T(1)); auto inplace_integral = [&](P &F) -> void { const int n = (int) F.size(); auto mod = T::get_mod(); while((int) inv.size() <= n) { int i = inv.size(); inv.push_back((-inv[mod % i]) * (mod / i)); } F.insert(begin(F), T(0)); for(int i = 1; i <= n; i++) F[i] *= inv[i]; }; auto inplace_diff = [](P &F) -> void { if(F.empty()) return; F.erase(begin(F)); T coeff = 1, one = 1; for(int i = 0; i < (int) F.size(); i++) { F[i] *= coeff; coeff += one; } }; P b{1, 1 < (int) this->size() ? (*this)[1] : T(0)}, c{1}, z1, z2{1, 1}; for(int m = 2; m < deg; m *= 2) { auto y = b; y.resize(2 * m); get_fft()(y); z1 = z2; P z(m); for(int i = 0; i < m; ++i) z[i] = y[i] * z1[i]; get_ifft()(z); fill(begin(z), begin(z) + m / 2, T(0)); get_fft()(z); for(int i = 0; i < m; ++i) z[i] *= -z1[i]; get_ifft()(z); c.insert(end(c), begin(z) + m / 2, end(z)); z2 = c; z2.resize(2 * m); get_fft()(z2); P x(begin(*this), begin(*this) + min< int >(this->size(), m)); inplace_diff(x); x.push_back(T(0)); get_fft()(x); for(int i = 0; i < m; ++i) x[i] *= y[i]; get_ifft()(x); x -= b.diff(); x.resize(2 * m); for(int i = 0; i < m - 1; ++i) x[m + i] = x[i], x[i] = T(0); get_fft()(x); for(int i = 0; i < 2 * m; ++i) x[i] *= z2[i]; get_ifft()(x); x.pop_back(); inplace_integral(x); for(int i = m; i < min< int >(this->size(), 2 * m); ++i) x[i] += (*this)[i]; fill(begin(x), begin(x) + m, T(0)); get_fft()(x); for(int i = 0; i < 2 * m; ++i) x[i] *= y[i]; get_ifft()(x); b.insert(end(b), begin(x) + m, end(x)); } return P(begin(b), begin(b) + deg); } template< typename T > typename FormalPowerSeries< T >::P FormalPowerSeries< T >::exp(int deg) const { assert((*this)[0] == T(0)); const int n = (int) this->size(); if(deg == -1) deg = n; if(get_fft() != nullptr) { P ret(*this); ret.resize(deg, T(0)); return ret.exp_fast(deg); } P ret({T(1)}); for(int i = 1; i < deg; i <<= 1) { ret = (ret * (pre(i << 1) + T(1) - ret.log(i << 1))).pre(i << 1); } return ret.pre(deg); } template< typename T > typename FormalPowerSeries< T >::P FormalPowerSeries< T >::pow(int64_t k, int deg) const { const int n = (int) this->size(); if(deg == -1) deg = n; for(int i = 0; i < n; i++) { if((*this)[i] != T(0)) { T rev = T(1) / (*this)[i]; P ret = (((*this * rev) >> i).log() * k).exp() * ((*this)[i].pow(k)); if(i * k > deg) return P(deg, T(0)); ret = (ret << (i * k)).pre(deg); if(ret.size() < deg) ret.resize(deg, T(0)); return ret; } } return *this; } template< typename T > typename FormalPowerSeries< T >::P FormalPowerSeries< T >::mod_pow(int64_t k, P g) const { P modinv = g.rev().inv(); auto get_div = [&](P base) { if(base.size() < g.size()) { base.clear(); return base; } int n = base.size() - g.size() + 1; return (base.rev().pre(n) * modinv.pre(n)).pre(n).rev(n); }; P x(*this), ret{1}; while(k > 0) { if(k & 1) { ret *= x; ret -= get_div(ret) * g; } x *= x; x -= get_div(x) * g; k >>= 1; } return ret; } template< typename T > typename FormalPowerSeries< T >::P FormalPowerSeries< T >::taylor_shift(T c) const { int n = (int) this->size(); vector< T > fact(n), rfact(n); fact[0] = rfact[0] = T(1); for(int i = 1; i < n; i++) fact[i] = fact[i - 1] * T(i); rfact[n - 1] = T(1) / fact[n - 1]; for(int i = n - 1; i > 1; i--) rfact[i - 1] = rfact[i] * T(i); P p(*this); for(int i = 0; i < n; i++) p[i] *= fact[i]; p = p.rev(); P bs(n, T(1)); for(int i = 1; i < n; i++) bs[i] = bs[i - 1] * c * rfact[i] * fact[i - 1]; p = (p * bs).pre(n); p = p.rev(); for(int i = 0; i < n; i++) p[i] *= rfact[i]; return p; } //// template< typename Mint > struct NumberTheoreticTransformFriendlyModInt { vector< Mint > dw, idw; int max_base; Mint root; NumberTheoreticTransformFriendlyModInt() { const unsigned mod = Mint::get_mod(); assert(mod >= 3 && mod % 2 == 1); auto tmp = mod - 1; max_base = 0; while(tmp % 2 == 0) tmp >>= 1, max_base++; root = 2; while(root.pow((mod - 1) >> 1) == 1) root += 1; assert(root.pow(mod - 1) == 1); dw.resize(max_base); idw.resize(max_base); for(int i = 0; i < max_base; i++) { dw[i] = -root.pow((mod - 1) >> (i + 2)); idw[i] = Mint(1) / dw[i]; } } void ntt(vector< Mint > &a) { const int n = (int) a.size(); assert((n & (n - 1)) == 0); assert(__builtin_ctz(n) <= max_base); for(int m = n; m >>= 1;) { Mint w = 1; for(int s = 0, k = 0; s < n; s += 2 * m) { for(int i = s, j = s + m; i < s + m; ++i, ++j) { auto x = a[i], y = a[j] * w; a[i] = x + y, a[j] = x - y; } w *= dw[__builtin_ctz(++k)]; } } } void intt(vector< Mint > &a, bool f = true) { const int n = (int) a.size(); assert((n & (n - 1)) == 0); assert(__builtin_ctz(n) <= max_base); for(int m = 1; m < n; m *= 2) { Mint w = 1; for(int s = 0, k = 0; s < n; s += 2 * m) { for(int i = s, j = s + m; i < s + m; ++i, ++j) { auto x = a[i], y = a[j]; a[i] = x + y, a[j] = (x - y) * w; } w *= idw[__builtin_ctz(++k)]; } } if(f) { Mint inv_sz = Mint(1) / n; for(int i = 0; i < n; i++) a[i] *= inv_sz; } } vector< Mint > multiply(vector< Mint > a, vector< Mint > b) { int need = a.size() + b.size() - 1; int nbase = 1; while((1 << nbase) < need) nbase++; int sz = 1 << nbase; a.resize(sz, 0); b.resize(sz, 0); ntt(a); ntt(b); Mint inv_sz = Mint(1) / sz; for(int i = 0; i < sz; i++) a[i] *= b[i] * inv_sz; intt(a, false); a.resize(need); return a; } }; ///////////////////// template<class T> T extgcd(T a, T b, T& x, T& y) { for (T u=y=1,v=x=0; a; ) { T q = b / a; swap(x -= q*u, u); swap(y -= q*v, v); swap(b -= q*a, a); } return b; } inline long long mod_pow(long long x, long long e, long long mod) { long long v = 1LL; for ( ; e; e>>=1) { if (e & 1) v = (v * x) % mod; x = (x * x) % mod; } return v; } inline long long mod_inv(long long a, long long mod) { // return mod_pow(a, mod-2, mod); // slower long long x, y; extgcd(a, mod, x, y); return (mod + x % mod) % mod; // ax + MODy = 1 // aとmodが互いに素である限り解が存在する // ax = 1 - MODy // ax % MOD = 1 // x = 1/a % MOD } long long _garner(vector<long long>& xs, vector<long long>& mods) { int M = xs.size(); vector<long long> coeffs(M, 1), constants(M, 0); for (int i=0; i<M-1; ++i) { long long mod_i = mods[i]; // coffs[i] * v + constants[i] == mr[i].val (mod mr[i].first) を解く long long v = (xs[i] - constants[i] + mod_i) % mod_i; v = (v * mod_inv(coeffs[i], mod_i)) % mod_i; for (int j=i+1; j<M; j++) { long long mod_j = mods[j]; constants[j] = (constants[j] + coeffs[j] * v) % mod_j; coeffs[j] = (coeffs[j] * mod_i) % mod_j; } } return constants.back(); } 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]); } } template<long long mod, long long primitive_root> class NTT { public: long long get_mod() { return mod; } void _ntt(vector<long long>& a, int sign) { const int n = a.size(); assert((n ^ (n&-n)) == 0); //n = 2^k const long long g = primitive_root; // g is primitive root of mod long long tmp = (mod - 1) * mod_inv(n, mod) % mod; // -1/n long long h = mod_pow(g, tmp, mod); // ^n√g if (sign == -1) h = mod_inv(h, mod); bit_reverse(a); for (int m=1; m<n; m<<=1) { const int m2 = 2 * m; // long long _base = mod_pow(h.val, n/m2, mod); long long _base = mod_pow(h, n/m2, mod); long long _w = 1; for (int x=0; x<m; ++x) { for (int 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_inv(input.size(), mod); for (auto &x : input) x = (x * n_inv) % mod; } // 畳み込み演算を行う 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> vector<T> convolution_ntt(const vector<T>& x, const vector<T>& y) { vector<ll> a(x.size()), b(y.size()); for (int i = 0; i < (int)x.size(); i++) a[i] = x[i]; for (int i = 0; i < (int)y.size(); i++) b[i] = y[i]; // ll maxval = max(a.size(), b.size()) * *max_element(a.begin(), a.end()) * *max_element(b.begin(), b.end()); // if (maxval < 1224736769) { // NTT<1224736769, 3> ntt3; // return ntt3.convolution(a, b); // } NTT<167772161, 3> ntt1; NTT<469762049, 3> ntt2; NTT<1224736769, 3> ntt3; vector<long long> x1 = ntt1.convolution(a, b); vector<long long> x2 = ntt2.convolution(a, b); vector<long long> x3 = ntt3.convolution(a, b); vector<T> ret(x1.size()); vector<long long> mods { 167772161, 469762049, 1224736769, T::get_mod() }; for (int i=0; i<x1.size(); ++i) { vector<long long> xs { x1[i], x2[i], x3[i], 0 }; ret[i] = _garner(xs, mods); } return ret; } /*-----8<-----library-----8<-----*/ void solve() { /* NumberTheoreticTransformFriendlyModInt< Modint > ntt; using FPS = FormalPowerSeries< Modint >; using SPARSE = vector<pair<ll,Modint>>; auto mult = [&](const FPS::P &a, const FPS::P &b) { auto ret = ntt.multiply(a, b); return FPS::P(ret.begin(), ret.end()); }; FPS::set_mult(mult); FPS::set_fft([&](FPS::P &a) { ntt.ntt(a); }, [&](FPS::P &a) { ntt.intt(a); }); */ using FPS = FormalPowerSeries< Modint >; using SPARSE = vector<pair<ll,Modint>>; auto mult = [&](const FPS::P& a, const FPS::P& b) { return convolution_ntt(a, b); }; FPS::set_mult(mult); ll K, N; cin >> K >> N; //T=(x^(進める歩数1) + x^(進める歩数2) + ... )とすると、 //求めたいのは 1 + T + T^2 + ... = 1/(1-T) //まず X に 1-T をつくる ll size = K+1; FormalPowerSeries<Modint> X(size); X[0] = 1; for(ll i = 0; i < N; i++) { ll t; cin >> t; if(t <= K) X[t] = -1; } //1/(1-T) FormalPowerSeries<Modint> v = X.inv(size); //x^Kの係数が解となる Modint ans = v[K]; cout << ans << endl; //スパース(疎)な乗算、除算 O(NK) (K=係数が0でない項の数) //Y={xの次数, 係数}を詰めた配列 vector<pair<ll,Modint>> Y{{0,1},{1,-1}}; FormalPowerSeries<Modint> Z = X.mul(Y); FormalPowerSeries<Modint> U = X.div(Y); } signed main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); //ll Q; cin >> Q; while(Q--)solve(); solve(); return 0; }