結果
問題 | No.8046 yukicoderの過去問 |
ユーザー | kyon2326 |
提出日時 | 2020-04-14 21:53:44 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 337 ms / 2,000 ms |
コード長 | 22,440 bytes |
コンパイル時間 | 6,728 ms |
コンパイル使用メモリ | 429,340 KB |
実行使用メモリ | 47,760 KB |
最終ジャッジ日時 | 2024-10-01 18:13:39 |
合計ジャッジ時間 | 9,267 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | 325 ms
47,640 KB |
testcase_04 | AC | 3 ms
5,248 KB |
testcase_05 | AC | 302 ms
47,532 KB |
testcase_06 | AC | 337 ms
47,760 KB |
testcase_07 | AC | 320 ms
47,448 KB |
testcase_08 | AC | 336 ms
47,632 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #if __has_include(<boost/multiprecision/cpp_int.hpp>) #include <boost/multiprecision/cpp_int.hpp> #include <boost/multiprecision/cpp_dec_float.hpp> using bll = boost::multiprecision::cpp_int; using bdouble = boost::multiprecision::cpp_dec_float_100; using namespace boost::multiprecision; #endif #ifdef LOCAL_DEV 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 << "(" << #__VA_ARGS__ << ") ="; debug_impl(__VA_ARGS__); } while (false) #else #define debug(...) do {} while (false) #endif #ifdef LOCAL_TEST #define BOOST_STACKTRACE_USE_ADDR2LINE #define BOOST_STACKTRACE_ADDR2LINE_LOCATION /usr/local/opt/binutils/bin/addr2line #define _GNU_SOURCE #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) { try { return this->at(n); } catch (const std::exception& e) { std::cerr << boost::stacktrace::stacktrace() << '\n'; return this->at(n); } } const T& operator[](size_t n) const { try { return this->at(n); } catch (const std::exception& e) { std::cerr << boost::stacktrace::stacktrace() << '\n'; return this->at(n); } } }; } class dbool { private: bool boolvalue; public: dbool() : boolvalue(false) {} dbool(bool b) : boolvalue(b) {} dbool(const dbool& b) : boolvalue(b.boolvalue) {} operator bool&() { return boolvalue; } dbool operator= (bool b) { return boolvalue = b; } operator const bool&() const { return boolvalue; } }; template<typename T> std::ostream& operator<<(std::ostream& s, const std::dvector<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::dvector<std::dvector<T>>& vv) { s << "\\\n"; for (size_t i = 0; i < vv.size(); ++i){ s << vv[i] << "\n"; } 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::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 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 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 T1, typename T2> std::ostream& operator<<(std::ostream& s, const std::pair<T1, T2>& p) { return s << "(" << p.first << ", " << p.second << ")"; } #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 //#define int long long using ll = long long; //constexpr int INF = 1e9;//INT_MAX=(1<<31)-1=2147483647 constexpr ll INF = (ll)1e18;//(1LL<<63)-1=9223372036854775807 constexpr ll MOD = (ll)1e9 + 7; constexpr double EPS = 1e-9; constexpr ll dx[4] = {1LL, 0LL, -1LL, 0LL}; constexpr ll dy[4] = {0LL, 1LL, 0LL, -1LL}; constexpr ll dx8[8] = {1LL, 0LL, -1LL, 0LL, 1LL, 1LL, -1LL, -1LL}; constexpr ll dy8[8] = {0LL, 1LL, 0LL, -1LL, 1LL, -1LL, 1LL, -1LL}; #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()); } /*-----8<-----template-----8<-----*/ inline constexpr ll extgcd(ll a, ll b, ll &x, ll &y){ ll g = a; x = 1; y = 0; if(b){ g = extgcd(b, a % b, y, x); y -= a / b * x; } return g; } inline constexpr ll invmod(ll a, ll m = MOD){ ll x = 0, y = 0; extgcd(a, m, x, y); return (x + m) % m; } class Modint{ public: ll _num; constexpr Modint() : _num() { _num = 0; } constexpr Modint(ll x) : _num() { _num = x % MOD; if(_num < 0) _num += MOD; } inline constexpr Modint operator= (int x){ _num = x % MOD; if(_num<0) _num += MOD; return *this; } inline constexpr Modint operator= (ll x){ _num = x % MOD; if(_num<0) _num += MOD; return *this; } //inline constexpr Modint operator= (Modint x){ _num = x._num; return *this; } inline constexpr Modint operator+ (int x){ return Modint(_num + x); } inline constexpr Modint operator+ (ll x){ return Modint(_num + x); } inline constexpr Modint operator+ (Modint x){ ll a = _num + x._num; if(a >= MOD) a -= MOD; return Modint{a}; } inline constexpr Modint operator+=(int x){ _num += x; _num %= MOD; if(_num < 0) _num += MOD; return *this; } inline constexpr Modint operator+=(ll x){ _num += x; _num %= MOD; if(_num < 0) _num += MOD; return *this; } inline constexpr Modint operator+=(Modint x){ _num += x._num; if(_num >= MOD) _num -= MOD; return *this; } inline constexpr Modint operator++(){ _num++; if(_num == MOD) _num = 0; return *this; } inline constexpr Modint operator- (int x){ return Modint(_num - x); } inline constexpr Modint operator- (ll x){ return Modint(_num - x); } inline constexpr Modint operator- (Modint x){ ll a = _num - x._num; if(a < 0) a += MOD; return Modint{a}; } inline constexpr Modint operator-=(int x){ _num -= x; _num %= MOD; if(_num < 0) _num += MOD; return *this; } inline constexpr Modint operator-=(ll x){ _num -= x; _num %= MOD; if(_num < 0) _num += MOD; return *this; } inline constexpr Modint operator-=(Modint x){ _num -= x._num; if(_num < 0) _num += MOD; return *this; } inline constexpr Modint operator--(){ _num--; if(_num == -1) _num = MOD - 1; return *this; } inline constexpr Modint operator* (int x){ return Modint(_num * (x % MOD)); } inline constexpr Modint operator* (ll x){ return Modint(_num * (x % MOD)); } inline constexpr Modint operator* (Modint x){ return Modint{_num * x._num % MOD}; } inline constexpr Modint operator*=(int x){ _num *= Modint(x); _num %= MOD; return *this; } inline constexpr Modint operator*=(ll x){ _num *= Modint(x); _num %= MOD; return *this; } inline constexpr Modint operator*=(Modint x){ _num *= x._num; _num %= MOD; return *this; } inline constexpr Modint operator/ (int x){ return Modint(_num * invmod(Modint(x), MOD)); } inline constexpr Modint operator/ (ll x){ return Modint(_num * invmod(Modint(x), MOD)); } inline constexpr Modint operator/ (Modint x){ return Modint{_num * invmod(x._num, MOD) % MOD}; } inline constexpr Modint operator/=(int x){ _num *= invmod(Modint(x), MOD); _num %= MOD; return *this; } inline constexpr Modint operator/=(ll x){ _num *= invmod(Modint(x), MOD); _num %= MOD; return *this; } inline constexpr Modint operator/=(Modint x){ _num *= invmod(x._num, MOD); _num %= MOD; return *this; } inline constexpr Modint pow(ll n){ ll i = 1, x = n>=0 ? n : -n; Modint ans = 1, cnt = n>=0 ? *this : Modint(1) / *this; while(i <= x){ if(x & i){ ans *= cnt; x ^= i; } cnt *= cnt; i *= 2; } return ans; } inline constexpr operator ll() const { return _num; } }; inline std::istream& operator>>(std::istream &s, Modint &x){ ll t; s>>t; x=t; return s; } 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() * ll(fac.size())); inv.resize(fac.size()); inv.back() = Modint(1) / fac.back(); 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_loop(ll n, ll k){ if(n<k) return 0; Modint val(1); for(ll i=n;i>(n-k);i--)val*=i; return val; } inline Modint nCk_loop(ll n, ll k){ if(n<k) return 0; Modint val(1); k=min(k,n-k); for(ll i=n;i>(n-k);i--)val*=i; for(ll i=k;i>1;i--)val/=i; return val; }; inline Modint nPk(ll n, ll k){ if(k < 0 || n < k) return 0; if(n>=(ll)1e7) return nPk_loop(n, k); reserve(n + 1); return fac[n] * inv[n - k]; } inline Modint nCk(ll n, ll k){ if(k < 0 || n < k) return 0; if(n>=(ll)1e7) return nCk_loop(n, k); reserve(n + 1); return fac[n] * inv[k] * inv[n - k]; } inline Modint nHk(ll n, ll k){ return nCk(n + k - 1, k); } //n種類のものから重複を許してk個選ぶ=玉k個と仕切りn-1個 /* nCk:n!が間に合わないくらい巨大でkが小さいとき、素直に計算すると間に合う のは1e7以上に組み込んであります auto f = [](ll n, ll k){ if(n<k)return Modint(0); Modint val(1); k=min(k,n-k); for(ll i=n;i>(n-k);i--)val*=i; for(ll i=k;i>1;i--)val/=i; return val; }; */ //// //高速フーリエ変換 //計算量 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< int64_t > 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< int64_t > ret(need); for(int i = 0; i < need; i++) { ret[i] = llround(i & 1 ? fa[i >> 1].y : fa[i >> 1].x); } return ret; } }; 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++) { int64_t aa = llround(fa[i].x); int64_t bb = llround(fb[i].x); int64_t 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; } }; //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() { 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); /* //ll版 オーバーフロー注意 using FPS = FormalPowerSeries< ll >; auto mult = [&](const FPS::P &a, const FPS::P &b) { return FastFourierTransform::multiply(a, b); }; FPS::set_fft(mult); */ 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) をつくる //sizeは2倍ほど確保した方がよさそう ll size = 2*K; 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; } void bunkatsusu(){ FormalPowerSeries<Modint> partitionv = partition<Modint>(10); debug(partitionv); } signed main() { yuki3046(); //bunkatsusu(); return 0; }