結果
問題 | No.3046 yukicoderの過去問 |
ユーザー | kyon2326 |
提出日時 | 2020-12-15 15:20:07 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 158 ms / 2,000 ms |
コード長 | 22,615 bytes |
コンパイル時間 | 2,857 ms |
コンパイル使用メモリ | 241,584 KB |
実行使用メモリ | 25,596 KB |
最終ジャッジ日時 | 2024-09-20 01:41:24 |
合計ジャッジ時間 | 4,056 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 133 ms
25,596 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 129 ms
25,476 KB |
testcase_06 | AC | 158 ms
25,432 KB |
testcase_07 | AC | 144 ms
25,468 KB |
testcase_08 | AC | 158 ms
25,464 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; */ #ifdef LOCAL_TEST #define BOOST_STACKTRACE_USE_ADDR2LINE #define BOOST_STACKTRACE_ADDR2LINE_LOCATION /usr/local/opt/binutils/bin/addr2line #define _GNU_SOURCE 1 #include <boost/stacktrace.hpp> 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) #else #define debug(...) do {} while (false) #endif //#define int long long using ll = long long; //constexpr int INF = (ll)1e9 + 7; //INT_MAX = (1<<31)-1 = 2147483647 constexpr ll INF = (ll)1e18; //INT64_MAX = (1LL<<63)-1 = 9223372036854775807 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 : Modint(1) / *this; 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; 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]; } //// //高速フーリエ変換 //計算量 O((n+m)log(n+m)) namespace FastFourierTransform { using real = double; struct C { real x, y; C() : x(0), y(0) {} C(real x, real y) : x(x), y(y) {} inline C operator+(const C &c) const { return C(x + c.x, y + c.y); } inline C operator-(const C &c) const { return C(x - c.x, y - c.y); } inline C operator*(const C &c) const { return C(x * c.x - y * c.y, x * c.y + y * c.x); } inline C conj() const { return C(x, -y); } }; int base = 1; vector< C > rts = {{0, 0}, {1, 0}}; vector< int > rev = {0, 1}; void ensure_base(int nbase) { if(nbase <= base) return; rev.resize(1 << nbase); rts.resize(1 << nbase); for(int i = 0; i < (1 << nbase); i++) { rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1)); } while(base < nbase) { real angle = M_PI * 2.0 / (1 << (base + 1)); for(int i = 1 << (base - 1); i < (1 << base); i++) { rts[i << 1] = rts[i]; real angle_i = angle * (2 * i + 1 - (1 << base)); rts[(i << 1) + 1] = C(cos(angle_i), sin(angle_i)); } ++base; } } void fft(vector< C > &a, int n) { assert((n & (n - 1)) == 0); int zeros = __builtin_ctz(n); ensure_base(zeros); int shift = base - zeros; for(int i = 0; i < n; i++) { if(i < (rev[i] >> shift)) { swap(a[i], a[rev[i] >> shift]); } } for(int k = 1; k < n; k <<= 1) { for(int i = 0; i < n; i += 2 * k) { for(int j = 0; j < k; j++) { C z = a[i + j + k] * rts[j + k]; a[i + j + k] = a[i + j] - z; a[i + j] = a[i + j] + z; } } } } template<typename T> vector< ll > multiply(vector< T > &a, vector< T > &b) { int need = (int) a.size() + (int) b.size() - 1; int nbase = 1; while((1 << nbase) < need) nbase++; ensure_base(nbase); int sz = 1 << nbase; vector< C > fa(sz); for(int i = 0; i < sz; i++) { real x = (i < (int) a.size() ? a[i] : 0); real y = (i < (int) b.size() ? b[i] : 0); fa[i] = C(x, y); } fft(fa, sz); C r(0, -0.25 / (sz >> 1)), s(0, 1), t(0.5, 0); for(int i = 0; i <= (sz >> 1); i++) { int j = (sz - i) & (sz - 1); C z = (fa[j] * fa[j] - (fa[i] * fa[i]).conj()) * r; fa[j] = (fa[i] * fa[i] - (fa[j] * fa[j]).conj()) * r; fa[i] = z; } for(int i = 0; i < (sz >> 1); i++) { C A0 = (fa[i] + fa[i + (sz >> 1)]) * t; C A1 = (fa[i] - fa[i + (sz >> 1)]) * t * rts[(sz >> 1) + i]; fa[i] = A0 + A1 * s; } fft(fa, sz >> 1); vector< ll > ret(need); for(int i = 0; i < need; i++) { ret[i] = llround(i & 1 ? fa[i >> 1].y : fa[i >> 1].x); } return ret; } }; //任意mod畳み込み(Arbitrary-Mod-Convolution) template< typename T > struct ArbitraryModConvolution { using real = FastFourierTransform::real; using C = FastFourierTransform::C; ArbitraryModConvolution() = default; vector< T > multiply(const vector< T > &a, const vector< T > &b, int need = -1) { if(need == -1) need = a.size() + b.size() - 1; int nbase = 0; while((1 << nbase) < need) nbase++; FastFourierTransform::ensure_base(nbase); int sz = 1 << nbase; vector< C > fa(sz); for(int i = 0; i < (int)a.size(); i++) { fa[i] = C(a[i].num & ((1 << 15) - 1), a[i].num >> 15); } fft(fa, sz); vector< C > fb(sz); if(a == b) { fb = fa; } else { for(int i = 0; i < (int)b.size(); i++) { fb[i] = C(b[i].num & ((1 << 15) - 1), b[i].num >> 15); } fft(fb, sz); } real ratio = 0.25 / sz; C r2(0, -1), r3(ratio, 0), r4(0, -ratio), r5(0, 1); for(int i = 0; i <= (sz >> 1); i++) { int j = (sz - i) & (sz - 1); C a1 = (fa[i] + fa[j].conj()); C a2 = (fa[i] - fa[j].conj()) * r2; C b1 = (fb[i] + fb[j].conj()) * r3; C b2 = (fb[i] - fb[j].conj()) * r4; if(i != j) { C c1 = (fa[j] + fa[i].conj()); C c2 = (fa[j] - fa[i].conj()) * r2; C d1 = (fb[j] + fb[i].conj()) * r3; C d2 = (fb[j] - fb[i].conj()) * r4; fa[i] = c1 * d1 + c2 * d2 * r5; fb[i] = c1 * d2 + c2 * d1; } fa[j] = a1 * b1 + a2 * b2 * r5; fb[j] = a1 * b2 + a2 * b1; } fft(fa, sz); fft(fb, sz); vector< T > ret(need); for(int i = 0; i < need; i++) { ll aa = llround(fa[i].x); ll bb = llround(fb[i].x); ll cc = llround(fa[i].y); aa = T(aa).num, bb = T(bb).num, cc = T(cc).num; ret[i] = T(aa + (bb << 15) + (cc << 30)); } return ret; } }; //形式的冪級数 //https://ei1333.github.io/luzhiled/snippets/math/formal-power-series.html template< typename T > struct FormalPowerSeries : vector< T > { using vector< T >::vector; using P = FormalPowerSeries; using MULT = function< P(P, P) >; static MULT &get_mult() { static MULT mult = nullptr; return mult; } static void set_fft(MULT f) { get_mult() = f; } template< typename E > FormalPowerSeries(const vector< E > &x) : vector< T >(begin(x), end(x)) {} 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); return *this = get_mult()(*this, r); } 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 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; 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] == T(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) << (i / 2); if(ret.size() < deg) ret.resize(deg, T(0)); return ret; } } return P(deg, 0); } P ret({T(1)}); 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; 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 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 C(*this * rev); P D(n - i); for(int j = i; j < n; j++) D[j - i] = C[j]; D = (D.log() * k).exp() * (*this)[i].pow(k); P E(deg); if(i * k > deg) return E; auto S = i * k; for(int j = 0; j + S < deg && j < D.size(); j++) E[j + S] = D[j]; return E; } } return *this; } T eval(T x) const { T r = 0, w = 1; for(auto &v : *this) { r += w * v; w *= x; } return r; } //sparse P mul(vector<pair<ll, T>> g) { 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) { 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; } }; //partition(N): [0,N] の分割数を返す。 //分割数:自然数 n を 自然数の和で表す組み合わせの数 (順序は考慮しない) //計算量:O(NlogN) template< typename T > FormalPowerSeries< T > partition(int N) { ArbitraryModConvolution< Modint > fft; using FPS = FormalPowerSeries< Modint >; auto mult = [&](const FPS::P &a, const FPS::P &b) { return fft.multiply(a, b); }; FPS::set_fft(mult); FormalPowerSeries< T > po(N + 1); po[0] = 1; for(int k = 1; k <= N; k++) { if(1LL * k * (3 * k + 1) / 2 <= N) po[k * (3 * k + 1) / 2] += (k % 2 ? -1 : 1); if(1LL * k * (3 * k - 1) / 2 <= N) po[k * (3 * k - 1) / 2] += (k % 2 ? -1 : 1); } return po.inv(); } /*-----8<-----library-----8<-----*/ //https://yukicoder.me/problems/no/3046 void yuki3046() { /* メモは形式的冪級数.cppを参照のこと */ ArbitraryModConvolution< Modint > fft; using FPS = FormalPowerSeries< Modint >; using SPARSE = vector<pair<ll,Modint>>; auto mult = [&](const FPS::P &a, const FPS::P &b) { return fft.multiply(a, b); }; FPS::set_fft(mult); /* //任意mod畳み込みだけやりたいとき ModintのMODを変更しておくこと ArbitraryModConvolution< Modint > fft; vector<Modint> A(N + 1), B(N + 1); vector<Modint> C = fft.multiply(A, B); */ ll K, N; cin >> K >> N; //f(T)=(T^(進める歩数1) + T^(進める歩数2) + ... )とすると、 //求めたいのは 1 + f(T) + f(T)^2 + ... = 1/(1-f(T)) //まず X に 1-f(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-f(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); } void bunkatsusu(){ FormalPowerSeries<Modint> partitionv = partition<Modint>(10); debug(partitionv); } signed main() { yuki3046(); //bunkatsusu(); return 0; }