#include using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define per(i, n) for (int i = (n)-1; i >= 0; i--) #define rep2(i, l, r) for (int i = (l); i < (r); i++) #define per2(i, l, r) for (int i = (r)-1; i >= (l); i--) #define each(e, v) for (auto &e : v) #define MM << " " << #define pb push_back #define eb emplace_back #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define sz(x) (int)x.size() using ll = long long; using pii = pair; using pil = pair; using pli = pair; using pll = pair; template using minheap = priority_queue, greater>; template using maxheap = priority_queue; template bool chmax(T &x, const T &y) { return (x < y) ? (x = y, true) : false; } template bool chmin(T &x, const T &y) { return (x > y) ? (x = y, true) : false; } template int flg(T x, int i) { return (x >> i) & 1; } int pct(int x) { return __builtin_popcount(x); } int pct(ll x) { return __builtin_popcountll(x); } int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } int botbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int botbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } template void print(const vector &v, T x = 0) { int n = v.size(); for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\n' : ' '); if (v.empty()) cout << '\n'; } template void printn(const vector &v, T x = 0) { int n = v.size(); for (int i = 0; i < n; i++) cout << v[i] + x << '\n'; } template int lb(const vector &v, T x) { return lower_bound(begin(v), end(v), x) - begin(v); } template int ub(const vector &v, T x) { return upper_bound(begin(v), end(v), x) - begin(v); } template void rearrange(vector &v) { sort(begin(v), end(v)); v.erase(unique(begin(v), end(v)), end(v)); } template vector id_sort(const vector &v, bool greater = false) { int n = v.size(); vector ret(n); iota(begin(ret), end(ret), 0); sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; }); return ret; } template void reorder(vector &a, const vector &ord) { int n = a.size(); vector b(n); for (int i = 0; i < n; i++) b[i] = a[ord[i]]; swap(a, b); } template T floor(T x, T y) { assert(y != 0); if (y < 0) x = -x, y = -y; return (x >= 0 ? x / y : (x - y + 1) / y); } template T ceil(T x, T y) { assert(y != 0); if (y < 0) x = -x, y = -y; return (x >= 0 ? (x + y - 1) / y : x / y); } template pair operator+(const pair &p, const pair &q) { return make_pair(p.first + q.first, p.second + q.second); } template pair operator-(const pair &p, const pair &q) { return make_pair(p.first - q.first, p.second - q.second); } template istream &operator>>(istream &is, pair &p) { S a; T b; is >> a >> b; p = make_pair(a, b); return is; } template ostream &operator<<(ostream &os, const pair &p) { return os << p.first << ' ' << p.second; } struct io_setup { io_setup() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(15); cerr << fixed << setprecision(15); } } io_setup; constexpr int inf = (1 << 30) - 1; constexpr ll INF = (1LL << 60) - 1; // constexpr int MOD = 1000000007; constexpr int MOD = 998244353; template struct Mod_Int { int x; Mod_Int() : x(0) {} Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} static int get_mod() { return mod; } Mod_Int &operator+=(const Mod_Int &p) { if ((x += p.x) >= mod) x -= mod; return *this; } Mod_Int &operator-=(const Mod_Int &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } Mod_Int &operator*=(const Mod_Int &p) { x = (int)(1LL * x * p.x % mod); return *this; } Mod_Int &operator/=(const Mod_Int &p) { *this *= p.inverse(); return *this; } Mod_Int &operator++() { return *this += Mod_Int(1); } Mod_Int operator++(int) { Mod_Int tmp = *this; ++*this; return tmp; } Mod_Int &operator--() { return *this -= Mod_Int(1); } Mod_Int operator--(int) { Mod_Int tmp = *this; --*this; return tmp; } Mod_Int operator-() const { return Mod_Int(-x); } Mod_Int operator+(const Mod_Int &p) const { return Mod_Int(*this) += p; } Mod_Int operator-(const Mod_Int &p) const { return Mod_Int(*this) -= p; } Mod_Int operator*(const Mod_Int &p) const { return Mod_Int(*this) *= p; } Mod_Int operator/(const Mod_Int &p) const { return Mod_Int(*this) /= p; } bool operator==(const Mod_Int &p) const { return x == p.x; } bool operator!=(const Mod_Int &p) const { return x != p.x; } Mod_Int inverse() const { assert(*this != Mod_Int(0)); return pow(mod - 2); } Mod_Int pow(long long k) const { Mod_Int now = *this, ret = 1; for (; k > 0; k >>= 1, now *= now) { if (k & 1) ret *= now; } return ret; } friend ostream &operator<<(ostream &os, const Mod_Int &p) { return os << p.x; } friend istream &operator>>(istream &is, Mod_Int &p) { long long a; is >> a; p = Mod_Int(a); return is; } }; using mint = Mod_Int; template struct Number_Theoretic_Transform { static int max_base; static T root; static vector r, ir; Number_Theoretic_Transform() {} static void init() { if (!r.empty()) return; int mod = T::get_mod(); int tmp = mod - 1; root = 2; while (root.pow(tmp >> 1) == 1) root++; max_base = 0; while (tmp % 2 == 0) tmp >>= 1, max_base++; r.resize(max_base), ir.resize(max_base); for (int i = 0; i < max_base; i++) { r[i] = -root.pow((mod - 1) >> (i + 2)); // r[i] := 1 の 2^(i+2) 乗根 ir[i] = r[i].inverse(); // ir[i] := 1/r[i] } } static void ntt(vector &a) { init(); int n = a.size(); assert((n & (n - 1)) == 0); assert(n <= (1 << max_base)); for (int k = n; k >>= 1;) { T w = 1; for (int s = 0, t = 0; s < n; s += 2 * k) { for (int i = s, j = s + k; i < s + k; i++, j++) { T x = a[i], y = w * a[j]; a[i] = x + y, a[j] = x - y; } w *= r[__builtin_ctz(++t)]; } } } static void intt(vector &a) { init(); int n = a.size(); assert((n & (n - 1)) == 0); assert(n <= (1 << max_base)); for (int k = 1; k < n; k <<= 1) { T w = 1; for (int s = 0, t = 0; s < n; s += 2 * k) { for (int i = s, j = s + k; i < s + k; i++, j++) { T x = a[i], y = a[j]; a[i] = x + y, a[j] = w * (x - y); } w *= ir[__builtin_ctz(++t)]; } } T inv = T(n).inverse(); for (auto &e : a) e *= inv; } static vector convolve(vector a, vector b) { if (a.empty() || b.empty()) return {}; if (min(a.size(), b.size()) < 40) { int n = a.size(), m = b.size(); vector c(n + m - 1, 0); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) c[i + j] += a[i] * b[j]; } return c; } int k = (int)a.size() + (int)b.size() - 1, n = 1; while (n < k) n <<= 1; a.resize(n, 0), b.resize(n, 0); ntt(a), ntt(b); for (int i = 0; i < n; i++) a[i] *= b[i]; intt(a), a.resize(k); return a; } }; template int Number_Theoretic_Transform::max_base = 0; template T Number_Theoretic_Transform::root = T(); template vector Number_Theoretic_Transform::r = vector(); template vector Number_Theoretic_Transform::ir = vector(); using NTT = Number_Theoretic_Transform; template vector Berlekamp_Massey(const vector &a) { int n = a.size(); vector c = {-1}, c_pre = {0}; int i_pre = -1; T x_pre = 1; for (int i = 0; i < n; i++) { int d = c.size(), d_pre = c_pre.size(); T x = 0; for (int j = 0; j < d; j++) x += a[i - j] * c[j]; if (x == 0) continue; T coef = -x / x_pre; if (d >= d_pre + i - i_pre) { for (int j = 0; j < d_pre; j++) c[i - i_pre + j] += coef * c_pre[j]; } else { vector memo = c; c.resize(d_pre + i - i_pre); for (int j = 0; j < d_pre; j++) { c[i - i_pre + j] += coef * c_pre[j]; } c_pre = memo, i_pre = i, x_pre = x; } } return c; } template T Bostan_Mori(vector f, vector g, long long k) { assert(!g.empty() && g[0] != 0); if (f.empty()) return 0; using NTT_ = Number_Theoretic_Transform; int n = 2; while (n < max((int)f.size(), (int)g.size())) n <<= 1; f.resize(n, 0), g.resize(n, 0); vector h(n, 0); h[1] = 1; NTT_::ntt(h); while (k > 0) { vector fe(n, 0), fo(n, 0), ge(n, 0), go(n, 0); for (int i = 0; i < n; i += 2) { fe[i >> 1] = f[i]; ge[i >> 1] = g[i]; } for (int i = 1; i < n; i += 2) { fo[i >> 1] = f[i]; go[i >> 1] = g[i]; } NTT_::ntt(fe), NTT_::ntt(fo), NTT_::ntt(ge), NTT_::ntt(go); if (k & 1) { for (int i = 0; i < n; i++) { f[i] = fo[i] * ge[i] - fe[i] * go[i]; g[i] = ge[i] * ge[i] - go[i] * go[i] * h[i]; } } else { for (int i = 0; i < n; i++) { f[i] = fe[i] * ge[i] - fo[i] * go[i] * h[i]; g[i] = ge[i] * ge[i] - go[i] * go[i] * h[i]; } } NTT_::intt(f), NTT_::intt(g); k >>= 1; } return f[0] / g[0]; } // d 項間漸化式 a_n = ∑[1<=i<=d]c_i * a_{n-i} を満たす数列 // a_0,...,a_{d-1} を与えたときに、a_k を計算する template T linear_recurrence(const vector &a, const vector &c, long long k) { int d = a.size(); assert((int)c.size() == d + 1); using NTT_ = Number_Theoretic_Transform; vector g(d + 1, 0); g[0] = 1; for (int i = 1; i <= d; i++) g[i] = -c[i]; vector f = NTT_::convolve(a, g); f.resize(d, 0); return Bostan_Mori(f, g, k); } template struct Formal_Power_Series : vector { using NTT_ = Number_Theoretic_Transform; using vector::vector; Formal_Power_Series(const vector &f) : vector(f) {} // f(x) mod x^n Formal_Power_Series pre(int n) const { Formal_Power_Series ret(begin(*this), begin(*this) + min((int)this->size(), n)); ret.resize(n, 0); return ret; } // f(1/x)x^{n-1} Formal_Power_Series rev(int n = -1) const { Formal_Power_Series ret = *this; if (n != -1) ret.resize(n, 0); reverse(begin(ret), end(ret)); return ret; } void normalize() { while (!this->empty() && this->back() == 0) this->pop_back(); } Formal_Power_Series operator-() const { Formal_Power_Series ret = *this; for (int i = 0; i < (int)ret.size(); i++) ret[i] = -ret[i]; return ret; } Formal_Power_Series &operator+=(const T &t) { if (this->empty()) this->resize(1, 0); (*this)[0] += t; return *this; } Formal_Power_Series &operator+=(const Formal_Power_Series &g) { if (g.size() > this->size()) this->resize(g.size()); for (int i = 0; i < (int)g.size(); i++) (*this)[i] += g[i]; this->normalize(); return *this; } Formal_Power_Series &operator-=(const T &t) { if (this->empty()) this->resize(1, 0); (*this)[0] -= t; return *this; } Formal_Power_Series &operator-=(const Formal_Power_Series &g) { if (g.size() > this->size()) this->resize(g.size()); for (int i = 0; i < (int)g.size(); i++) (*this)[i] -= g[i]; this->normalize(); return *this; } Formal_Power_Series &operator*=(const T &t) { for (int i = 0; i < (int)this->size(); i++) (*this)[i] *= t; return *this; } Formal_Power_Series &operator*=(const Formal_Power_Series &g) { if (empty(*this) || empty(g)) { this->clear(); return *this; } return *this = NTT_::convolve(*this, g); } Formal_Power_Series &operator/=(const T &t) { assert(t != 0); T inv = t.inverse(); return *this *= inv; } // f(x) を g(x) で割った商 Formal_Power_Series &operator/=(const Formal_Power_Series &g) { if (g.size() > this->size()) { this->clear(); return *this; } int n = this->size(), m = g.size(); return *this = (rev() * g.rev().inv(n - m + 1)).pre(n - m + 1).rev(); } // f(x) を g(x) で割った余り Formal_Power_Series &operator%=(const Formal_Power_Series &g) { return *this -= (*this / g) * g; } // f(x)/x^k Formal_Power_Series &operator<<=(int k) { Formal_Power_Series ret(k, 0); ret.insert(end(ret), begin(*this), end(*this)); return *this = ret; } // f(x)x^k Formal_Power_Series &operator>>=(int k) { Formal_Power_Series ret; ret.insert(end(ret), begin(*this) + k, end(*this)); return *this = ret; } Formal_Power_Series operator+(const T &t) const { return Formal_Power_Series(*this) += t; } Formal_Power_Series operator+(const Formal_Power_Series &g) const { return Formal_Power_Series(*this) += g; } Formal_Power_Series operator-(const T &t) const { return Formal_Power_Series(*this) -= t; } Formal_Power_Series operator-(const Formal_Power_Series &g) const { return Formal_Power_Series(*this) -= g; } Formal_Power_Series operator*(const T &t) const { return Formal_Power_Series(*this) *= t; } Formal_Power_Series operator*(const Formal_Power_Series &g) const { return Formal_Power_Series(*this) *= g; } Formal_Power_Series operator/(const T &t) const { return Formal_Power_Series(*this) /= t; } Formal_Power_Series operator/(const Formal_Power_Series &g) const { return Formal_Power_Series(*this) /= g; } Formal_Power_Series operator%(const Formal_Power_Series &g) const { return Formal_Power_Series(*this) %= g; } Formal_Power_Series operator<<(int k) const { return Formal_Power_Series(*this) <<= k; } Formal_Power_Series operator>>(int k) const { return Formal_Power_Series(*this) >>= k; } // f(c) T val(const T &c) const { T ret = 0; for (int i = (int)this->size() - 1; i >= 0; i--) ret *= c, ret += (*this)[i]; return ret; } // df/dx Formal_Power_Series derivative() const { if (empty(*this)) return *this; int n = this->size(); Formal_Power_Series ret(n - 1); for (int i = 1; i < n; i++) ret[i - 1] = (*this)[i] * i; return ret; } // ∫f(x)dx Formal_Power_Series integral() const { if (empty(*this)) return *this; int n = this->size(); vector inv(n + 1, 0); inv[1] = 1; int mod = T::get_mod(); for (int i = 2; i <= n; i++) inv[i] = -inv[mod % i] * T(mod / i); Formal_Power_Series ret(n + 1, 0); for (int i = 0; i < n; i++) ret[i + 1] = (*this)[i] * inv[i + 1]; return ret; } // 1/f(x) mod x^n (f[0] != 0) Formal_Power_Series inv(int n = -1) const { assert((*this)[0] != 0); if (n == -1) n = this->size(); Formal_Power_Series ret(1, (*this)[0].inverse()); for (int m = 1; m < n; m <<= 1) { Formal_Power_Series f = pre(2 * m), g = ret; f.resize(2 * m), g.resize(2 * m); NTT_::ntt(f), NTT_::ntt(g); Formal_Power_Series h(2 * m); for (int i = 0; i < 2 * m; i++) h[i] = f[i] * g[i]; NTT_::intt(h); for (int i = 0; i < m; i++) h[i] = 0; NTT_::ntt(h); for (int i = 0; i < 2 * m; i++) h[i] *= g[i]; NTT_::intt(h); for (int i = 0; i < m; i++) h[i] = 0; ret -= h; } ret.resize(n); return ret; } // log(f(x)) mod x^n (f[0] = 1) Formal_Power_Series log(int n = -1) const { assert((*this)[0] == 1); if (n == -1) n = this->size(); Formal_Power_Series ret = (derivative() * inv(n)).pre(n - 1).integral(); ret.resize(n); return ret; } // exp(f(x)) mod x^n (f[0] = 0) Formal_Power_Series exp(int n = -1) const { assert((*this)[0] == 0); if (n == -1) n = this->size(); vector inv(2 * n + 1, 0); inv[1] = 1; int mod = T::get_mod(); for (int i = 2; i <= 2 * n; i++) inv[i] = -inv[mod % i] * T(mod / i); auto inplace_integral = [inv](Formal_Power_Series &f) { if (empty(f)) return; int m = f.size(); f.insert(begin(f), 0); for (int i = 1; i <= m; i++) f[i] *= inv[i]; }; auto inplace_derivative = [](Formal_Power_Series &f) { if (empty(f)) return; int m = f.size(); f.erase(begin(f)); for (int i = 0; i < m - 1; i++) f[i] *= T(i + 1); }; Formal_Power_Series ret{1, this->size() > 1 ? (*this)[1] : 0}, c{1}, z1, z2{1, 1}; for (int m = 2; m < n; m *= 2) { auto y = ret; y.resize(2 * m); NTT_::ntt(y); z1 = z2; Formal_Power_Series z(m); for (int i = 0; i < m; i++) z[i] = y[i] * z1[i]; NTT_::intt(z); fill(begin(z), begin(z) + m / 2, 0); NTT_::ntt(z); for (int i = 0; i < m; i++) z[i] *= -z1[i]; NTT_::intt(z); c.insert(end(c), begin(z) + m / 2, end(z)); z2 = c, z2.resize(2 * m); NTT_::ntt(z2); Formal_Power_Series x(begin(*this), begin(*this) + min((int)this->size(), m)); inplace_derivative(x); x.resize(m, 0); NTT_::ntt(x); for (int i = 0; i < m; i++) x[i] *= y[i]; NTT_::intt(x); x -= ret.derivative(), x.resize(2 * m); for (int i = 0; i < m - 1; i++) x[m + i] = x[i], x[i] = 0; NTT_::ntt(x); for (int i = 0; i < 2 * m; i++) x[i] *= z2[i]; NTT_::intt(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, 0); NTT_::ntt(x); for (int i = 0; i < 2 * m; i++) x[i] *= y[i]; NTT_::intt(x); ret.insert(end(ret), begin(x) + m, end(x)); } ret.resize(n); return ret; } // f(x)^k mod x^n Formal_Power_Series pow(long long k, int n = -1) const { if (n == -1) n = this->size(); int m = this->size(); for (int i = 0; i < m; i++) { if ((*this)[i] == 0) continue; T inv = (*this)[i].inverse(); Formal_Power_Series g(m - i, 0); for (int j = i; j < m; j++) g[j - i] = (*this)[j] * inv; g = (g.log(n) * k).exp(n) * ((*this)[i].pow(k)); Formal_Power_Series ret(n, 0); if (i > 0 && k > n / i) return ret; long long d = i * k; for (int j = 0; j + d < n && j < (int)g.size(); j++) ret[j + d] = g[j]; return ret; } Formal_Power_Series ret(n, 0); if (k == 0) ret[0] = 1; return ret; } // √f(x) mod x^n (存在しなければ空) Formal_Power_Series sqrt(int n = -1) const { if (n == -1) n = this->size(); int mod = T::get_mod(); auto sqrt_mod = [mod](const T &a) { if (mod == 2) return a; int s = mod - 1, t = 0; while (s % 2 == 0) s /= 2, t++; T root = 2; while (root.pow((mod - 1) / 2) == 1) root++; T x = a.pow((s + 1) / 2); T u = root.pow(s); T y = x * x * a.inverse(); while (y != 1) { int k = 0; T z = y; while (z != 1) k++, z *= z; for (int i = 0; i < t - k - 1; i++) u *= u; x *= u, u *= u, y *= u; t = k; } return x; }; if ((*this)[0] == 0) { for (int i = 1; i < (int)this->size(); i++) { if ((*this)[i] != 0) { if (i & 1) return {}; if ((*this)[i].pow((mod - 1) / 2) != 1) return {}; if (n <= i / 2) break; return ((*this) >> i).sqrt(n - i / 2) << (i / 2); } } return Formal_Power_Series(n, 0); } if ((*this)[0].pow((mod - 1) / 2) != 1) return {}; T tw = T(2).inverse(); Formal_Power_Series ret{sqrt_mod((*this)[0])}; for (int m = 1; m < n; m *= 2) { Formal_Power_Series g = (*this).pre(m * 2) * ret.inv(m * 2); g.resize(2 * m); ret = (ret + g) * tw; } ret.resize(n); return ret; } // f(x+c) Formal_Power_Series Taylor_shift(T c) const { int n = this->size(); vector ifac(n, 1); Formal_Power_Series f(n), g(n); for (int i = 0; i < n; i++) { f[n - 1 - i] = (*this)[i] * ifac[n - 1]; if (i < n - 1) ifac[n - 1] *= i + 1; } ifac[n - 1] = ifac[n - 1].inverse(); for (int i = n - 1; i > 0; i--) ifac[i - 1] = ifac[i] * i; T pw = 1; for (int i = 0; i < n; i++) { g[i] = pw * ifac[i]; pw *= c; } f *= g; Formal_Power_Series b(n); for (int i = 0; i < n; i++) b[i] = f[n - 1 - i] * ifac[i]; return b; } }; using fps = Formal_Power_Series; template struct Combination { static vector _fac, _ifac; Combination() {} static void init(int n) { _fac.resize(n + 1), _ifac.resize(n + 1); _fac[0] = 1; for (int i = 1; i <= n; i++) _fac[i] = _fac[i - 1] * i; _ifac[n] = _fac[n].inverse(); for (int i = n; i >= 1; i--) _ifac[i - 1] = _ifac[i] * i; } static T fac(int k) { return _fac[k]; } static T ifac(int k) { return _ifac[k]; } static T inv(int k) { return fac(k - 1) * ifac(k); } static T P(int n, int k) { if (k < 0 || n < k) return 0; return fac(n) * ifac(n - k); } static T C(int n, int k) { if (k < 0 || n < k) return 0; return fac(n) * ifac(n - k) * ifac(k); } // n 個の区別できる箱に、k 個の区別できない玉を入れる場合の数 static T H(int n, int k) { if (n < 0 || k < 0) return 0; return k == 0 ? 1 : C(n + k - 1, k); } // n 個の区別できる玉を、k 個の区別しない箱に、各箱に 1 個以上玉が入るように入れる場合の数 static T second_stirling_number(int n, int k) { T ret = 0; for (int i = 0; i <= k; i++) { T tmp = C(k, i) * T(i).pow(n); ret += ((k - i) & 1) ? -tmp : tmp; } return ret * ifac(k); } // n 個の区別できる玉を、k 個の区別しない箱に入れる場合の数 static T bell_number(int n, int k) { if (n == 0) return 1; k = min(k, n); vector pref(k + 1); pref[0] = 1; for (int i = 1; i <= k; i++) { if (i & 1) { pref[i] = pref[i - 1] - ifac(i); } else { pref[i] = pref[i - 1] + ifac(i); } } T ret = 0; for (int i = 1; i <= k; i++) ret += T(i).pow(n) * ifac(i) * pref[k - i]; return ret; } }; template vector Combination::_fac = vector(); template vector Combination::_ifac = vector(); using comb = Combination; void solve() { int N, M; cin >> N >> M; if (N % 3 != M % 3) { cout << "0\n"; return; } int K = 0; rep2(i, 0, M + 1) K += M - i + 1; comb::init(100000); int L = 2 * K + 100; fps f(L, 0); rep(i, L) { if (i % 3 == 1) f[i] = comb::ifac(i); } f = f.pow(M); vector a; int t = M % 3; for (int i = t; i < L; i += 3) a.eb(comb::fac(i) * f[i]); // print(a); auto c = Berlekamp_Massey(a); int D = sz(c) - 1; a.resize(D); cout << linear_recurrence(a, c, (N - t) / 3) << '\n'; } int main() { int T = 1; // cin >> T; while (T--) solve(); }