QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#91013#6126. Sequence and SequenceNyaanAC ✓1472ms4472kbC++1749.0kb2023-03-26 16:58:062023-03-26 16:58:09

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-26 16:58:09]
  • 评测
  • 测评结果:AC
  • 用时:1472ms
  • 内存:4472kb
  • [2023-03-26 16:58:06]
  • 提交

answer





#define NDEBUG
#include <algorithm>

#include <cassert>

#include <chrono>

#include <cstring>

#include <functional>

#include <iomanip>

#include <iostream>

#include <list>

#include <map>

#include <new>

#include <numeric>

#include <queue>

#include <random>

#include <set>

#include <string>

#include <utility>

#include <vector>

using namespace std;
struct IoSetupNya {
 IoSetupNya() {
 cin.tie(nullptr);
 ios::sync_with_stdio(false);
 cout << fixed << setprecision(15);
 cerr << fixed << setprecision(7);
 }
} iosetupnya;
constexpr long long TEN(int n) { return n ? TEN(n - 1) * 10 : 1; }

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define sz(v) ((int)(v).size())
using ll = long long;
using vl = vector<ll>;
using pl = pair<ll, ll>;

template <typename T>
using V = vector<T>;



template <typename mint>
struct NTT {
 static constexpr uint32_t get_pr() {
 uint32_t _mod = mint::get_mod();
 using u64 = uint64_t;
 u64 ds[32] = {};
 int idx = 0;
 u64 m = _mod - 1;
 for (u64 i = 2; i * i <= m; ++i) {
 if (m % i == 0) {
 ds[idx++] = i;
 while (m % i == 0) m /= i;
 }
 }
 if (m != 1) ds[idx++] = m;

 uint32_t _pr = 2;
 while (1) {
 int flg = 1;
 for (int i = 0; i < idx; ++i) {
 u64 a = _pr, b = (_mod - 1) / ds[i], r = 1;
 while (b) {
 if (b & 1) r = r * a % _mod;
 a = a * a % _mod;
 b >>= 1;
 }
 if (r == 1) {
 flg = 0;
 break;
 }
 }
 if (flg == 1) break;
 ++_pr;
 }
 return _pr;
 };

 static constexpr uint32_t mod = mint::get_mod();
 static constexpr uint32_t pr = get_pr();
 static constexpr int level = __builtin_ctzll(mod - 1);
 mint dw[level], dy[level];

 void setwy(int k) {
 mint w[level], y[level];
 w[k - 1] = mint(pr).pow((mod - 1) / (1 << k));
 y[k - 1] = w[k - 1].inverse();
 for (int i = k - 2; i > 0; --i)
 w[i] = w[i + 1] * w[i + 1], y[i] = y[i + 1] * y[i + 1];
 dw[1] = w[1], dy[1] = y[1], dw[2] = w[2], dy[2] = y[2];
 for (int i = 3; i < k; ++i) {
 dw[i] = dw[i - 1] * y[i - 2] * w[i];
 dy[i] = dy[i - 1] * w[i - 2] * y[i];
 }
 }

 NTT() { setwy(level); }

 void fft4(vector<mint> &a, int k) {
 if ((int)a.size() <= 1) return;
 if (k == 1) {
 mint a1 = a[1];
 a[1] = a[0] - a[1];
 a[0] = a[0] + a1;
 return;
 }
 if (k & 1) {
 int v = 1 << (k - 1);
 for (int j = 0; j < v; ++j) {
 mint ajv = a[j + v];
 a[j + v] = a[j] - ajv;
 a[j] += ajv;
 }
 }
 int u = 1 << (2 + (k & 1));
 int v = 1 << (k - 2 - (k & 1));
 mint one = mint(1);
 mint imag = dw[1];
 while (v) {
 
 {
 int j0 = 0;
 int j1 = v;
 int j2 = j1 + v;
 int j3 = j2 + v;
 for (; j0 < v; ++j0, ++j1, ++j2, ++j3) {
 mint t0 = a[j0], t1 = a[j1], t2 = a[j2], t3 = a[j3];
 mint t0p2 = t0 + t2, t1p3 = t1 + t3;
 mint t0m2 = t0 - t2, t1m3 = (t1 - t3) * imag;
 a[j0] = t0p2 + t1p3, a[j1] = t0p2 - t1p3;
 a[j2] = t0m2 + t1m3, a[j3] = t0m2 - t1m3;
 }
 }
 
 mint ww = one, xx = one * dw[2], wx = one;
 for (int jh = 4; jh < u;) {
 ww = xx * xx, wx = ww * xx;
 int j0 = jh * v;
 int je = j0 + v;
 int j2 = je + v;
 for (; j0 < je; ++j0, ++j2) {
 mint t0 = a[j0], t1 = a[j0 + v] * xx, t2 = a[j2] * ww,
 t3 = a[j2 + v] * wx;
 mint t0p2 = t0 + t2, t1p3 = t1 + t3;
 mint t0m2 = t0 - t2, t1m3 = (t1 - t3) * imag;
 a[j0] = t0p2 + t1p3, a[j0 + v] = t0p2 - t1p3;
 a[j2] = t0m2 + t1m3, a[j2 + v] = t0m2 - t1m3;
 }
 xx *= dw[__builtin_ctzll((jh += 4))];
 }
 u <<= 2;
 v >>= 2;
 }
 }

 void ifft4(vector<mint> &a, int k) {
 if ((int)a.size() <= 1) return;
 if (k == 1) {
 mint a1 = a[1];
 a[1] = a[0] - a[1];
 a[0] = a[0] + a1;
 return;
 }
 int u = 1 << (k - 2);
 int v = 1;
 mint one = mint(1);
 mint imag = dy[1];
 while (u) {
 
 {
 int j0 = 0;
 int j1 = v;
 int j2 = v + v;
 int j3 = j2 + v;
 for (; j0 < v; ++j0, ++j1, ++j2, ++j3) {
 mint t0 = a[j0], t1 = a[j1], t2 = a[j2], t3 = a[j3];
 mint t0p1 = t0 + t1, t2p3 = t2 + t3;
 mint t0m1 = t0 - t1, t2m3 = (t2 - t3) * imag;
 a[j0] = t0p1 + t2p3, a[j2] = t0p1 - t2p3;
 a[j1] = t0m1 + t2m3, a[j3] = t0m1 - t2m3;
 }
 }
 
 mint ww = one, xx = one * dy[2], yy = one;
 u <<= 2;
 for (int jh = 4; jh < u;) {
 ww = xx * xx, yy = xx * imag;
 int j0 = jh * v;
 int je = j0 + v;
 int j2 = je + v;
 for (; j0 < je; ++j0, ++j2) {
 mint t0 = a[j0], t1 = a[j0 + v], t2 = a[j2], t3 = a[j2 + v];
 mint t0p1 = t0 + t1, t2p3 = t2 + t3;
 mint t0m1 = (t0 - t1) * xx, t2m3 = (t2 - t3) * yy;
 a[j0] = t0p1 + t2p3, a[j2] = (t0p1 - t2p3) * ww;
 a[j0 + v] = t0m1 + t2m3, a[j2 + v] = (t0m1 - t2m3) * ww;
 }
 xx *= dy[__builtin_ctzll(jh += 4)];
 }
 u >>= 4;
 v <<= 2;
 }
 if (k & 1) {
 u = 1 << (k - 1);
 for (int j = 0; j < u; ++j) {
 mint ajv = a[j] - a[j + u];
 a[j] += a[j + u];
 a[j + u] = ajv;
 }
 }
 }

 void ntt(vector<mint> &a) {
 if ((int)a.size() <= 1) return;
 fft4(a, __builtin_ctz(a.size()));
 }

 void intt(vector<mint> &a) {
 if ((int)a.size() <= 1) return;
 ifft4(a, __builtin_ctz(a.size()));
 mint iv = mint(a.size()).inverse();
 for (auto &x : a) x *= iv;
 }

 vector<mint> multiply(const vector<mint> &a, const vector<mint> &b) {
 int l = a.size() + b.size() - 1;
 if (min<int>(a.size(), b.size()) <= 40) {
 vector<mint> s(l);
 for (int i = 0; i < (int)a.size(); ++i)
 for (int j = 0; j < (int)b.size(); ++j) s[i + j] += a[i] * b[j];
 return s;
 }
 int k = 2, M = 4;
 while (M < l) M <<= 1, ++k;
 setwy(k);
 vector<mint> s(M), t(M);
 for (int i = 0; i < (int)a.size(); ++i) s[i] = a[i];
 for (int i = 0; i < (int)b.size(); ++i) t[i] = b[i];
 fft4(s, k);
 fft4(t, k);
 for (int i = 0; i < M; ++i) s[i] *= t[i];
 ifft4(s, k);
 s.resize(l);
 mint invm = mint(M).inverse();
 for (int i = 0; i < l; ++i) s[i] *= invm;
 return s;
 }

 void ntt_doubling(vector<mint> &a) {
 int M = (int)a.size();
 auto b = a;
 intt(b);
 mint r = 1, zeta = mint(pr).pow((mint::get_mod() - 1) / (M << 1));
 for (int i = 0; i < M; i++) b[i] *= r, r *= zeta;
 ntt(b);
 copy(begin(b), end(b), back_inserter(a));
 }
};

template <typename mint>
struct FormalPowerSeries : vector<mint> {
 using vector<mint>::vector;
 using FPS = FormalPowerSeries;

 FPS &operator+=(const FPS &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;
 }

 FPS &operator+=(const mint &r) {
 if (this->empty()) this->resize(1);
 (*this)[0] += r;
 return *this;
 }

 FPS &operator-=(const FPS &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;
 }

 FPS &operator-=(const mint &r) {
 if (this->empty()) this->resize(1);
 (*this)[0] -= r;
 return *this;
 }

 FPS &operator*=(const mint &v) {
 for (int k = 0; k < (int)this->size(); k++) (*this)[k] *= v;
 return *this;
 }

 FPS &operator/=(const FPS &r) {
 if (this->size() < r.size()) {
 this->clear();
 return *this;
 }
 int n = this->size() - r.size() + 1;
 if ((int)r.size() <= 64) {
 FPS f(*this), g(r);
 g.shrink();
 mint coeff = g.back().inverse();
 for (auto &x : g) x *= coeff;
 int deg = (int)f.size() - (int)g.size() + 1;
 int gs = g.size();
 FPS quo(deg);
 for (int i = deg - 1; i >= 0; i--) {
 quo[i] = f[i + gs - 1];
 for (int j = 0; j < gs; j++) f[i + j] -= quo[i] * g[j];
 }
 *this = quo * coeff;
 this->resize(n, mint(0));
 return *this;
 }
 return *this = ((*this).rev().pre(n) * r.rev().inv(n)).pre(n).rev();
 }

 FPS &operator%=(const FPS &r) {
 *this -= *this / r * r;
 shrink();
 return *this;
 }

 FPS operator+(const FPS &r) const { return FPS(*this) += r; }
 FPS operator+(const mint &v) const { return FPS(*this) += v; }
 FPS operator-(const FPS &r) const { return FPS(*this) -= r; }
 FPS operator-(const mint &v) const { return FPS(*this) -= v; }
 FPS operator*(const FPS &r) const { return FPS(*this) *= r; }
 FPS operator*(const mint &v) const { return FPS(*this) *= v; }
 FPS operator/(const FPS &r) const { return FPS(*this) /= r; }
 FPS operator%(const FPS &r) const { return FPS(*this) %= r; }
 FPS operator-() const {
 FPS ret(this->size());
 for (int i = 0; i < (int)this->size(); i++) ret[i] = -(*this)[i];
 return ret;
 }

 void shrink() {
 while (this->size() && this->back() == mint(0)) this->pop_back();
 }

 FPS rev() const {
 FPS ret(*this);
 reverse(begin(ret), end(ret));
 return ret;
 }

 FPS dot(FPS r) const {
 FPS ret(min(this->size(), r.size()));
 for (int i = 0; i < (int)ret.size(); i++) ret[i] = (*this)[i] * r[i];
 return ret;
 }

 FPS pre(int sz) const {
 return FPS(begin(*this), begin(*this) + min((int)this->size(), sz));
 }

 FPS operator>>(int sz) const {
 if ((int)this->size() <= sz) return {};
 FPS ret(*this);
 ret.erase(ret.begin(), ret.begin() + sz);
 return ret;
 }

 FPS operator<<(int sz) const {
 FPS ret(*this);
 ret.insert(ret.begin(), sz, mint(0));
 return ret;
 }

 FPS diff() const {
 const int n = (int)this->size();
 FPS ret(max(0, n - 1));
 mint one(1), coeff(1);
 for (int i = 1; i < n; i++) {
 ret[i - 1] = (*this)[i] * coeff;
 coeff += one;
 }
 return ret;
 }

 FPS integral() const {
 const int n = (int)this->size();
 FPS ret(n + 1);
 ret[0] = mint(0);
 if (n > 0) ret[1] = mint(1);
 auto mod = mint::get_mod();
 for (int i = 2; i <= n; i++) ret[i] = (-ret[mod % i]) * (mod / i);
 for (int i = 0; i < n; i++) ret[i + 1] *= (*this)[i];
 return ret;
 }

 mint eval(mint x) const {
 mint r = 0, w = 1;
 for (auto &v : *this) r += w * v, w *= x;
 return r;
 }

 FPS log(int deg = -1) const {
 assert((*this)[0] == mint(1));
 if (deg == -1) deg = (int)this->size();
 return (this->diff() * this->inv(deg)).pre(deg - 1).integral();
 }

 FPS pow(int64_t k, int deg = -1) const {
 const int n = (int)this->size();
 if (deg == -1) deg = n;
 if (k == 0) {
 FPS ret(deg);
 if (deg) ret[0] = 1;
 return ret;
 }
 for (int i = 0; i < n; i++) {
 if ((*this)[i] != mint(0)) {
 mint rev = mint(1) / (*this)[i];
 FPS ret = (((*this * rev) >> i).log(deg) * k).exp(deg);
 ret *= (*this)[i].pow(k);
 ret = (ret << (i * k)).pre(deg);
 if ((int)ret.size() < deg) ret.resize(deg, mint(0));
 return ret;
 }
 if (__int128_t(i + 1) * k >= deg) return FPS(deg, mint(0));
 }
 return FPS(deg, mint(0));
 }

 static void *ntt_ptr;
 static void set_fft();
 FPS &operator*=(const FPS &r);
 void ntt();
 void intt();
 void ntt_doubling();
 static int ntt_pr();
 FPS inv(int deg = -1) const;
 FPS exp(int deg = -1) const;
};
template <typename mint>
void *FormalPowerSeries<mint>::ntt_ptr = nullptr;






template <typename mint>
void FormalPowerSeries<mint>::set_fft() {
 if (!ntt_ptr) ntt_ptr = new NTT<mint>;
}

template <typename mint>
FormalPowerSeries<mint>& FormalPowerSeries<mint>::operator*=(
 const FormalPowerSeries<mint>& r) {
 if (this->empty() || r.empty()) {
 this->clear();
 return *this;
 }
 set_fft();
 auto ret = static_cast<NTT<mint>*>(ntt_ptr)->multiply(*this, r);
 return *this = FormalPowerSeries<mint>(ret.begin(), ret.end());
}

template <typename mint>
void FormalPowerSeries<mint>::ntt() {
 set_fft();
 static_cast<NTT<mint>*>(ntt_ptr)->ntt(*this);
}

template <typename mint>
void FormalPowerSeries<mint>::intt() {
 set_fft();
 static_cast<NTT<mint>*>(ntt_ptr)->intt(*this);
}

template <typename mint>
void FormalPowerSeries<mint>::ntt_doubling() {
 set_fft();
 static_cast<NTT<mint>*>(ntt_ptr)->ntt_doubling(*this);
}

template <typename mint>
int FormalPowerSeries<mint>::ntt_pr() {
 set_fft();
 return static_cast<NTT<mint>*>(ntt_ptr)->pr;
}

template <typename mint>
FormalPowerSeries<mint> FormalPowerSeries<mint>::inv(int deg) const {
 assert((*this)[0] != mint(0));
 if (deg == -1) deg = (int)this->size();
 FormalPowerSeries<mint> res(deg);
 res[0] = {mint(1) / (*this)[0]};
 for (int d = 1; d < deg; d <<= 1) {
 FormalPowerSeries<mint> f(2 * d), g(2 * d);
 for (int j = 0; j < min((int)this->size(), 2 * d); j++) f[j] = (*this)[j];
 for (int j = 0; j < d; j++) g[j] = res[j];
 f.ntt();
 g.ntt();
 for (int j = 0; j < 2 * d; j++) f[j] *= g[j];
 f.intt();
 for (int j = 0; j < d; j++) f[j] = 0;
 f.ntt();
 for (int j = 0; j < 2 * d; j++) f[j] *= g[j];
 f.intt();
 for (int j = d; j < min(2 * d, deg); j++) res[j] = -f[j];
 }
 return res.pre(deg);
}

template <typename mint>
FormalPowerSeries<mint> FormalPowerSeries<mint>::exp(int deg) const {
 using fps = FormalPowerSeries<mint>;
 assert((*this).size() == 0 || (*this)[0] == mint(0));
 if (deg == -1) deg = this->size();

 fps inv;
 inv.reserve(deg + 1);
 inv.push_back(mint(0));
 inv.push_back(mint(1));

 auto inplace_integral = [&](fps& F) -> void {
 const int n = (int)F.size();
 auto mod = mint::get_mod();
 while ((int)inv.size() <= n) {
 int i = inv.size();
 inv.push_back((-inv[mod % i]) * (mod / i));
 }
 F.insert(begin(F), mint(0));
 for (int i = 1; i <= n; i++) F[i] *= inv[i];
 };

 auto inplace_diff = [](fps& F) -> void {
 if (F.empty()) return;
 F.erase(begin(F));
 mint coeff = 1, one = 1;
 for (int i = 0; i < (int)F.size(); i++) {
 F[i] *= coeff;
 coeff += one;
 }
 };

 fps b{1, 1 < (int)this->size() ? (*this)[1] : 0}, c{1}, z1, z2{1, 1};
 for (int m = 2; m < deg; m *= 2) {
 auto y = b;
 y.resize(2 * m);
 y.ntt();
 z1 = z2;
 fps z(m);
 for (int i = 0; i < m; ++i) z[i] = y[i] * z1[i];
 z.intt();
 fill(begin(z), begin(z) + m / 2, mint(0));
 z.ntt();
 for (int i = 0; i < m; ++i) z[i] *= -z1[i];
 z.intt();
 c.insert(end(c), begin(z) + m / 2, end(z));
 z2 = c;
 z2.resize(2 * m);
 z2.ntt();
 fps x(begin(*this), begin(*this) + min<int>(this->size(), m));
 x.resize(m);
 inplace_diff(x);
 x.push_back(mint(0));
 x.ntt();
 for (int i = 0; i < m; ++i) x[i] *= y[i];
 x.intt();
 x -= b.diff();
 x.resize(2 * m);
 for (int i = 0; i < m - 1; ++i) x[m + i] = x[i], x[i] = mint(0);
 x.ntt();
 for (int i = 0; i < 2 * m; ++i) x[i] *= z2[i];
 x.intt();
 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, mint(0));
 x.ntt();
 for (int i = 0; i < 2 * m; ++i) x[i] *= y[i];
 x.intt();
 b.insert(end(b), begin(x) + m, end(x));
 }
 return fps{begin(b), begin(b) + deg};
}










template <typename mint>
struct ProductTree {
 using fps = FormalPowerSeries<mint>;
 const vector<mint> &xs;
 vector<fps> buf;
 int N, xsz;
 vector<int> l, r;
 ProductTree(const vector<mint> &xs_) : xs(xs_), xsz(xs.size()) {
 N = 1;
 while (N < (int)xs.size()) N *= 2;
 buf.resize(2 * N);
 l.resize(2 * N, xs.size());
 r.resize(2 * N, xs.size());
 fps::set_fft();
 if (fps::ntt_ptr == nullptr)
 build();
 else
 build_ntt();
 }

 void build() {
 for (int i = 0; i < xsz; i++) {
 l[i + N] = i;
 r[i + N] = i + 1;
 buf[i + N] = {-xs[i], 1};
 }
 for (int i = N - 1; i > 0; i--) {
 l[i] = l[(i << 1) | 0];
 r[i] = r[(i << 1) | 1];
 if (buf[(i << 1) | 0].empty())
 continue;
 else if (buf[(i << 1) | 1].empty())
 buf[i] = buf[(i << 1) | 0];
 else
 buf[i] = buf[(i << 1) | 0] * buf[(i << 1) | 1];
 }
 }

 void build_ntt() {
 fps f;
 f.reserve(N * 2);
 for (int i = 0; i < xsz; i++) {
 l[i + N] = i;
 r[i + N] = i + 1;
 buf[i + N] = {-xs[i] + 1, -xs[i] - 1};
 }
 for (int i = N - 1; i > 0; i--) {
 l[i] = l[(i << 1) | 0];
 r[i] = r[(i << 1) | 1];
 if (buf[(i << 1) | 0].empty())
 continue;
 else if (buf[(i << 1) | 1].empty())
 buf[i] = buf[(i << 1) | 0];
 else if (buf[(i << 1) | 0].size() == buf[(i << 1) | 1].size()) {
 buf[i] = buf[(i << 1) | 0];
 f.clear();
 copy(begin(buf[(i << 1) | 1]), end(buf[(i << 1) | 1]),
 back_inserter(f));
 buf[i].ntt_doubling();
 f.ntt_doubling();
 for (int j = 0; j < (int)buf[i].size(); j++) buf[i][j] *= f[j];
 } else {
 buf[i] = buf[(i << 1) | 0];
 f.clear();
 copy(begin(buf[(i << 1) | 1]), end(buf[(i << 1) | 1]),
 back_inserter(f));
 buf[i].ntt_doubling();
 f.intt();
 f.resize(buf[i].size(), mint(0));
 f.ntt();
 for (int j = 0; j < (int)buf[i].size(); j++) buf[i][j] *= f[j];
 }
 }
 for (int i = 0; i < 2 * N; i++) {
 buf[i].intt();
 buf[i].shrink();
 }
 }
};

template <typename mint>
vector<mint> InnerMultipointEvaluation(const FormalPowerSeries<mint> &f,
 const vector<mint> &xs,
 const ProductTree<mint> &ptree) {
 using fps = FormalPowerSeries<mint>;
 vector<mint> ret;
 ret.reserve(xs.size());
 auto rec = [&](auto self, fps a, int idx) {
 if (ptree.l[idx] == ptree.r[idx]) return;
 a %= ptree.buf[idx];
 if ((int)a.size() <= 64) {
 for (int i = ptree.l[idx]; i < ptree.r[idx]; i++)
 ret.push_back(a.eval(xs[i]));
 return;
 }
 self(self, a, (idx << 1) | 0);
 self(self, a, (idx << 1) | 1);
 };
 rec(rec, f, 1);
 return ret;
}

template <typename mint>
vector<mint> MultipointEvaluation(const FormalPowerSeries<mint> &f,
 const vector<mint> &xs) {
 if(f.empty() || xs.empty()) return vector<mint>(xs.size(), mint(0));
 return InnerMultipointEvaluation(f, xs, ProductTree<mint>(xs));
}





template <class mint>
FormalPowerSeries<mint> PolynomialInterpolation(const vector<mint> &xs,
 const vector<mint> &ys) {
 using fps = FormalPowerSeries<mint>;
 assert(xs.size() == ys.size());
 ProductTree<mint> ptree(xs);
 fps w = ptree.buf[1].diff();
 vector<mint> vs = InnerMultipointEvaluation<mint>(w, xs, ptree);
 auto rec = [&](auto self, int idx) -> fps {
 if (idx >= ptree.N) {
 if (idx - ptree.N < (int)xs.size())
 return {ys[idx - ptree.N] / vs[idx - ptree.N]};
 else
 return {mint(1)};
 }
 if (ptree.buf[idx << 1 | 0].empty())
 return {};
 else if (ptree.buf[idx << 1 | 1].empty())
 return self(self, idx << 1 | 0);
 return self(self, idx << 1 | 0) * ptree.buf[idx << 1 | 1] +
 self(self, idx << 1 | 1) * ptree.buf[idx << 1 | 0];
 };
 return rec(rec, 1);
}




#include <cmath>
#include <tuple>
using namespace std;





template <uint32_t mod>
struct LazyMontgomeryModInt {
 using mint = LazyMontgomeryModInt;
 using i32 = int32_t;
 using u32 = uint32_t;
 using u64 = uint64_t;

 static constexpr u32 get_r() {
 u32 ret = mod;
 for (i32 i = 0; i < 4; ++i) ret *= 2 - mod * ret;
 return ret;
 }

 static constexpr u32 r = get_r();
 static constexpr u32 n2 = -u64(mod) % mod;
 static_assert(r * mod == 1, "invalid, r * mod != 1");
 static_assert(mod < (1 << 30), "invalid, mod >= 2 ^ 30");
 static_assert((mod & 1) == 1, "invalid, mod % 2 == 0");

 u32 a;

 constexpr LazyMontgomeryModInt() : a(0) {}
 constexpr LazyMontgomeryModInt(const int64_t &b)
 : a(reduce(u64(b % mod + mod) * n2)){};

 static constexpr u32 reduce(const u64 &b) {
 return (b + u64(u32(b) * u32(-r)) * mod) >> 32;
 }

 constexpr mint &operator+=(const mint &b) {
 if (i32(a += b.a - 2 * mod) < 0) a += 2 * mod;
 return *this;
 }

 constexpr mint &operator-=(const mint &b) {
 if (i32(a -= b.a) < 0) a += 2 * mod;
 return *this;
 }

 constexpr mint &operator*=(const mint &b) {
 a = reduce(u64(a) * b.a);
 return *this;
 }

 constexpr mint &operator/=(const mint &b) {
 *this *= b.inverse();
 return *this;
 }

 constexpr mint operator+(const mint &b) const { return mint(*this) += b; }
 constexpr mint operator-(const mint &b) const { return mint(*this) -= b; }
 constexpr mint operator*(const mint &b) const { return mint(*this) *= b; }
 constexpr mint operator/(const mint &b) const { return mint(*this) /= b; }
 constexpr bool operator==(const mint &b) const {
 return (a >= mod ? a - mod : a) == (b.a >= mod ? b.a - mod : b.a);
 }
 constexpr bool operator!=(const mint &b) const {
 return (a >= mod ? a - mod : a) != (b.a >= mod ? b.a - mod : b.a);
 }
 constexpr mint operator-() const { return mint() - mint(*this); }

 constexpr mint pow(u64 n) const {
 mint ret(1), mul(*this);
 while (n > 0) {
 if (n & 1) ret *= mul;
 mul *= mul;
 n >>= 1;
 }
 return ret;
 }
 
 constexpr mint inverse() const { return pow(mod - 2); }

 friend ostream &operator<<(ostream &os, const mint &b) {
 return os << b.get();
 }

 friend istream &operator>>(istream &is, mint &b) {
 int64_t t;
 is >> t;
 b = LazyMontgomeryModInt<mod>(t);
 return (is);
 }
 
 constexpr u32 get() const {
 u32 ret = reduce(a);
 return ret >= mod ? ret - mod : ret;
 }

 static constexpr u32 get_mod() { return mod; }
};
namespace ArbitraryNTT {
using i64 = int64_t;
using u128 = __uint128_t;
constexpr int32_t m0 = 167772161;
constexpr int32_t m1 = 469762049;
constexpr int32_t m2 = 754974721;
using mint0 = LazyMontgomeryModInt<m0>;
using mint1 = LazyMontgomeryModInt<m1>;
using mint2 = LazyMontgomeryModInt<m2>;
constexpr int r01 = mint1(m0).inverse().get();
constexpr int r02 = mint2(m0).inverse().get();
constexpr int r12 = mint2(m1).inverse().get();
constexpr int r02r12 = i64(r02) * r12 % m2;
constexpr i64 w1 = m0;
constexpr i64 w2 = i64(m0) * m1;

template <typename T, typename submint>
vector<submint> mul(const vector<T> &a, const vector<T> &b) {
 static NTT<submint> ntt;
 vector<submint> s(a.size()), t(b.size());
 for (int i = 0; i < (int)a.size(); ++i) s[i] = i64(a[i] % submint::get_mod());
 for (int i = 0; i < (int)b.size(); ++i) t[i] = i64(b[i] % submint::get_mod());
 return ntt.multiply(s, t);
}

template <typename T>
vector<int> multiply(const vector<T> &s, const vector<T> &t, int mod) {
 auto d0 = mul<T, mint0>(s, t);
 auto d1 = mul<T, mint1>(s, t);
 auto d2 = mul<T, mint2>(s, t);
 int n = d0.size();
 vector<int> ret(n);
 const int W1 = w1 % mod;
 const int W2 = w2 % mod;
 for (int i = 0; i < n; i++) {
 int n1 = d1[i].get(), n2 = d2[i].get(), a = d0[i].get();
 int b = i64(n1 + m1 - a) * r01 % m1;
 int c = (i64(n2 + m2 - a) * r02r12 + i64(m2 - b) * r12) % m2;
 ret[i] = (i64(a) + i64(b) * W1 + i64(c) * W2) % mod;
 }
 return ret;
}

template <typename mint>
vector<mint> multiply(const vector<mint> &a, const vector<mint> &b) {
 if (a.size() == 0 && b.size() == 0) return {};
 if (min<int>(a.size(), b.size()) < 128) {
 vector<mint> ret(a.size() + b.size() - 1);
 for (int i = 0; i < (int)a.size(); ++i)
 for (int j = 0; j < (int)b.size(); ++j) ret[i + j] += a[i] * b[j];
 return ret;
 }
 vector<int> s(a.size()), t(b.size());
 for (int i = 0; i < (int)a.size(); ++i) s[i] = a[i].get();
 for (int i = 0; i < (int)b.size(); ++i) t[i] = b[i].get();
 vector<int> u = multiply<int>(s, t, mint::get_mod());
 vector<mint> ret(u.size());
 for (int i = 0; i < (int)u.size(); ++i) ret[i] = mint(u[i]);
 return ret;
}

template <typename T>
vector<u128> multiply_u128(const vector<T> &s, const vector<T> &t) {
 if (s.size() == 0 && t.size() == 0) return {};
 if (min<int>(s.size(), t.size()) < 128) {
 vector<u128> ret(s.size() + t.size() - 1);
 for (int i = 0; i < (int)s.size(); ++i)
 for (int j = 0; j < (int)t.size(); ++j) ret[i + j] += i64(s[i]) * t[j];
 return ret;
 }
 auto d0 = mul<T, mint0>(s, t);
 auto d1 = mul<T, mint1>(s, t);
 auto d2 = mul<T, mint2>(s, t);
 int n = d0.size();
 vector<u128> ret(n);
 for (int i = 0; i < n; i++) {
 i64 n1 = d1[i].get(), n2 = d2[i].get();
 i64 a = d0[i].get();
 i64 b = (n1 + m1 - a) * r01 % m1;
 i64 c = ((n2 + m2 - a) * r02r12 + (m2 - b) * r12) % m2;
 ret[i] = a + b * w1 + u128(c) * w2;
 }
 return ret;
}
} 

namespace MultiPrecisionIntegerImpl {
struct TENS {
 static constexpr int offset = 30;
 constexpr TENS() : _tend() {
 _tend[offset] = 1;
 for (int i = 1; i <= offset; i++) {
 _tend[offset + i] = _tend[offset + i - 1] * 10.0;
 _tend[offset - i] = 1.0 / _tend[offset + i];
 }
 }
 long double ten_ld(int n) const {
 assert(-offset <= n and n <= offset);
 return _tend[n + offset];
 }

 private:
 long double _tend[offset * 2 + 1];
};
} 


struct MultiPrecisionInteger {
 using M = MultiPrecisionInteger;
 inline constexpr static MultiPrecisionIntegerImpl::TENS tens = {};

 static constexpr int D = 1000000000;
 static constexpr int logD = 9;
 bool neg;
 vector<int> dat;

 MultiPrecisionInteger() : neg(false), dat() {}

 MultiPrecisionInteger(bool n, const vector<int>& d) : neg(n), dat(d) {}

 template <typename I, enable_if_t<is_integral_v<I> ||
 is_same_v<I, __int128_t>>* = nullptr>
 MultiPrecisionInteger(I x) : neg(false) {
 if constexpr (is_signed_v<I> || is_same_v<I, __int128_t>) {
 if (x < 0) neg = true, x = -x;
 }
 while (x) dat.push_back(x % D), x /= D;
 }

 MultiPrecisionInteger(const string& S) : neg(false) {
 assert(!S.empty());
 if (S.size() == 1u && S[0] == '0') return;
 int l = 0;
 if (S[0] == '-') ++l, neg = true;
 for (int ie = S.size(); l < ie; ie -= logD) {
 int is = max(l, ie - logD);
 long long x = 0;
 for (int i = is; i < ie; i++) x = x * 10 + S[i] - '0';
 dat.push_back(x);
 }
 }

 friend M operator+(const M& lhs, const M& rhs) {
 if (lhs.neg == rhs.neg) return {lhs.neg, _add(lhs.dat, rhs.dat)};
 if (_leq(lhs.dat, rhs.dat)) {
 
 auto c = _sub(rhs.dat, lhs.dat);
 bool n = _is_zero(c) ? false : rhs.neg;
 return {n, c};
 }
 auto c = _sub(lhs.dat, rhs.dat);
 bool n = _is_zero(c) ? false : lhs.neg;
 return {n, c};
 }
 friend M operator-(const M& lhs, const M& rhs) { return lhs + (-rhs); }

 friend M operator*(const M& lhs, const M& rhs) {
 auto c = _mul(lhs.dat, rhs.dat);
 bool n = _is_zero(c) ? false : (lhs.neg ^ rhs.neg);
 return {n, c};
 }
 friend pair<M, M> divmod(const M& lhs, const M& rhs) {
 auto dm = _divmod_newton(lhs.dat, rhs.dat);
 bool dn = _is_zero(dm.first) ? false : lhs.neg != rhs.neg;
 bool mn = _is_zero(dm.second) ? false : lhs.neg;
 return {M{dn, dm.first}, M{mn, dm.second}};
 }
 friend M operator/(const M& lhs, const M& rhs) {
 return divmod(lhs, rhs).first;
 }
 friend M operator%(const M& lhs, const M& rhs) {
 return divmod(lhs, rhs).second;
 }

 M& operator+=(const M& rhs) { return (*this) = (*this) + rhs; }
 M& operator-=(const M& rhs) { return (*this) = (*this) - rhs; }
 M& operator*=(const M& rhs) { return (*this) = (*this) * rhs; }
 M& operator/=(const M& rhs) { return (*this) = (*this) / rhs; }
 M& operator%=(const M& rhs) { return (*this) = (*this) % rhs; }

 M operator-() const {
 if (is_zero()) return *this;
 return {!neg, dat};
 }
 M operator+() const { return *this; }
 friend M abs(const M& m) { return {false, m.dat}; }
 bool is_zero() const { return _is_zero(dat); }

 friend bool operator==(const M& lhs, const M& rhs) {
 return lhs.neg == rhs.neg && lhs.dat == rhs.dat;
 }
 friend bool operator!=(const M& lhs, const M& rhs) {
 return lhs.neg != rhs.neg || lhs.dat != rhs.dat;
 }
 friend bool operator<(const M& lhs, const M& rhs) {
 if (lhs == rhs) return false;
 return _neq_lt(lhs, rhs);
 }
 friend bool operator<=(const M& lhs, const M& rhs) {
 if (lhs == rhs) return true;
 return _neq_lt(lhs, rhs);
 }
 friend bool operator>(const M& lhs, const M& rhs) {
 if (lhs == rhs) return false;
 return _neq_lt(rhs, lhs);
 }
 friend bool operator>=(const M& lhs, const M& rhs) {
 if (lhs == rhs) return true;
 return _neq_lt(rhs, lhs);
 }

 
 
 pair<long double, int> dfp() const {
 if (is_zero()) return {0, 0};
 int l = max<int>(0, _size() - 3);
 int b = logD * l;
 string prefix{};
 for (int i = _size() - 1; i >= l; i--) {
 prefix += _itos(dat[i], i != _size() - 1);
 }
 b += prefix.size() - 1;
 long double a = 0;
 for (auto& c : prefix) a = a * 10.0 + (c - '0');
 a *= tens.ten_ld(-((int)prefix.size()) + 1);
 a = clamp<long double>(a, 1.0, nextafterl(10.0, 1.0));
 if (neg) a = -a;
 return {a, b};
 }
 string to_string() const {
 if (is_zero()) return "0";
 string res;
 if (neg) res.push_back('-');
 for (int i = _size() - 1; i >= 0; i--) {
 res += _itos(dat[i], i != _size() - 1);
 }
 return res;
 }
 long double to_ld() const {
 auto [a, b] = dfp();
 if (-tens.offset <= b and b <= tens.offset) {
 return a * tens.ten_ld(b);
 }
 return a * powl(10, b);
 }
 long long to_ll() const {
 long long res = _to_ll(dat);
 return neg ? -res : res;
 }
 __int128_t to_i128() const {
 __int128_t res = _to_i128(dat);
 return neg ? -res : res;
 }

 friend istream& operator>>(istream& is, M& m) {
 string s;
 is >> s;
 m = M{s};
 return is;
 }

 friend ostream& operator<<(ostream& os, const M& m) {
 return os << m.to_string();
 }

 
 static void _test_private_function(const M&, const M&);

 private:
 
 int _size() const { return dat.size(); }
 
 static bool _eq(const vector<int>& a, const vector<int>& b) { return a == b; }
 
 static bool _lt(const vector<int>& a, const vector<int>& b) {
 if (a.size() != b.size()) return a.size() < b.size();
 for (int i = a.size() - 1; i >= 0; i--) {
 if (a[i] != b[i]) return a[i] < b[i];
 }
 return false;
 }
 
 static bool _leq(const vector<int>& a, const vector<int>& b) {
 return _eq(a, b) || _lt(a, b);
 }
 
 static bool _neq_lt(const M& lhs, const M& rhs) {
 assert(lhs != rhs);
 if (lhs.neg != rhs.neg) return lhs.neg;
 bool f = _lt(lhs.dat, rhs.dat);
 if (f) return !lhs.neg;
 return lhs.neg;
 }
 
 static bool _is_zero(const vector<int>& a) { return a.empty(); }
 
 static bool _is_one(const vector<int>& a) {
 return (int)a.size() == 1 && a[0] == 1;
 }
 
 static void _shrink(vector<int>& a) {
 while (a.size() && a.back() == 0) a.pop_back();
 }
 
 void _shrink() {
 while (_size() && dat.back() == 0) dat.pop_back();
 }
 
 static vector<int> _add(const vector<int>& a, const vector<int>& b) {
 vector<int> c(max(a.size(), b.size()) + 1);
 for (int i = 0; i < (int)a.size(); i++) c[i] += a[i];
 for (int i = 0; i < (int)b.size(); i++) c[i] += b[i];
 for (int i = 0; i < (int)c.size() - 1; i++) {
 if (c[i] >= D) c[i] -= D, c[i + 1]++;
 }
 _shrink(c);
 return c;
 }
 
 static vector<int> _sub(const vector<int>& a, const vector<int>& b) {
 assert(_leq(b, a));
 vector<int> c{a};
 int borrow = 0;
 for (int i = 0; i < (int)a.size(); i++) {
 if (i < (int)b.size()) borrow += b[i];
 c[i] -= borrow;
 borrow = 0;
 if (c[i] < 0) c[i] += D, borrow = 1;
 }
 assert(borrow == 0);
 _shrink(c);
 return c;
 }
 
 static vector<int> _mul_fft(const vector<int>& a, const vector<int>& b) {
 if (a.empty() || b.empty()) return {};
 auto m = ArbitraryNTT::multiply_u128(a, b);
 vector<int> c;
 c.reserve(m.size() + 3);
 __uint128_t x = 0;
 for (int i = 0;; i++) {
 if (i >= (int)m.size() && x == 0) break;
 if (i < (int)m.size()) x += m[i];
 c.push_back(x % D);
 x /= D;
 }
 _shrink(c);
 return c;
 }
 
 static vector<int> _mul_naive(const vector<int>& a, const vector<int>& b) {
 if (a.empty() || b.empty()) return {};
 vector<long long> prod(a.size() + b.size() - 1 + 1);
 for (int i = 0; i < (int)a.size(); i++) {
 for (int j = 0; j < (int)b.size(); j++) {
 long long p = 1LL * a[i] * b[j];
 prod[i + j] += p;
 if (prod[i + j] >= (4LL * D * D)) {
 prod[i + j] -= 4LL * D * D;
 prod[i + j + 1] += 4LL * D;
 }
 }
 }
 vector<int> c;
 long long x = 0;
 for (int i = 0;; i++) {
 if (i >= (int)prod.size() && x == 0) break;
 if (i < (int)prod.size()) x += prod[i];
 c.push_back(x % D);
 x /= D;
 }
 _shrink(c);
 return c;
 }
 
 static vector<int> _mul(const vector<int>& a, const vector<int>& b) {
 if (_is_zero(a) || _is_zero(b)) return {};
 if (_is_one(a)) return b;
 if (_is_one(b)) return a;
 if (min<int>(a.size(), b.size()) <= 128) {
 return a.size() < b.size() ? _mul_naive(b, a) : _mul_naive(a, b);
 }
 return _mul_fft(a, b);
 }
 
 static pair<vector<int>, vector<int>> _divmod_li(const vector<int>& a,
 const vector<int>& b) {
 assert(0 <= (int)a.size() && (int)a.size() <= 2);
 assert((int)b.size() == 1);
 long long va = _to_ll(a);
 int vb = b[0];
 return {_integer_to_vec(va / vb), _integer_to_vec(va % vb)};
 }
 
 static pair<vector<int>, vector<int>> _divmod_ll(const vector<int>& a,
 const vector<int>& b) {
 assert(0 <= (int)a.size() && (int)a.size() <= 2);
 assert(1 <= (int)b.size() && (int)b.size() <= 2);
 long long va = _to_ll(a), vb = _to_ll(b);
 return {_integer_to_vec(va / vb), _integer_to_vec(va % vb)};
 }
 
 static pair<vector<int>, vector<int>> _divmod_1e9(const vector<int>& a,
 const vector<int>& b) {
 assert((int)b.size() == 1);
 if (b[0] == 1) return {a, {}};
 if ((int)a.size() <= 2) return _divmod_li(a, b);
 vector<int> quo(a.size());
 long long d = 0;
 int b0 = b[0];
 for (int i = a.size() - 1; i >= 0; i--) {
 d = d * D + a[i];
 assert(d < 1LL * D * b0);
 int q = d / b0, r = d % b0;
 quo[i] = q, d = r;
 }
 _shrink(quo);
 return {quo, d ? vector<int>{int(d)} : vector<int>{}};
 }
 
 static pair<vector<int>, vector<int>> _divmod_naive(const vector<int>& a,
 const vector<int>& b) {
 if (_is_zero(b)) {
 cerr << "Divide by Zero Exception" << endl;
 exit(1);
 }
 assert(1 <= (int)b.size());
 if ((int)b.size() == 1) return _divmod_1e9(a, b);
 if (max<int>(a.size(), b.size()) <= 2) return _divmod_ll(a, b);
 if (_lt(a, b)) return {{}, a};
 
 int norm = D / (b.back() + 1);
 vector<int> x = _mul(a, {norm});
 vector<int> y = _mul(b, {norm});
 int yb = y.back();
 vector<int> quo(x.size() - y.size() + 1);
 vector<int> rem(x.end() - y.size(), x.end());
 for (int i = quo.size() - 1; i >= 0; i--) {
 if (rem.size() < y.size()) {
 
 } else if (rem.size() == y.size()) {
 if (_leq(y, rem)) {
 quo[i] = 1, rem = _sub(rem, y);
 }
 } else {
 assert(y.size() + 1 == rem.size());
 long long rb = 1LL * rem[rem.size() - 1] * D + rem[rem.size() - 2];
 int q = rb / yb;
 vector<int> yq = _mul(y, {q});
 
 while (_lt(rem, yq)) q--, yq = _sub(yq, y);
 rem = _sub(rem, yq);
 while (_leq(y, rem)) q++, rem = _sub(rem, y);
 quo[i] = q;
 }
 if (i) rem.insert(begin(rem), x[i - 1]);
 }
 _shrink(quo), _shrink(rem);
 auto [q2, r2] = _divmod_1e9(rem, {norm});
 assert(_is_zero(r2));
 return {quo, q2};
 }

 
 static pair<vector<int>, vector<int>> _divmod_dc(const vector<int>& a,
 const vector<int>& b) {
 if (_is_zero(b)) {
 cerr << "Divide by Zero Exception" << endl;
 exit(1);
 }
 if ((int)b.size() <= 64) return _divmod_naive(a, b);
 if ((int)a.size() - (int)b.size() <= 64) return _divmod_naive(a, b);

 int norm = D / (b.back() + 1);
 vector<int> x = _mul(a, {norm});
 vector<int> y = _mul(b, {norm});

 int s = x.size(), t = y.size();
 int yu = (t + 1) / 2, yv = t - yu;
 vector<int> yh{end(y) - yu, end(y)};
 int xv = max<int>(yv, s - (yu * 2 - 1));
 int xu = s - xv;
 vector<int> xh{end(x) - xu, end(x)};
 vector<int> rem{end(x) - xu - yv, end(x)};
 auto [qh, _unused] = _divmod_dc(xh, yh);
 vector<int> yqh = _mul(y, qh);
 while (_lt(rem, yqh)) _sub(qh, {1}), yqh = _sub(yqh, y);
 rem = _sub(rem, yqh);
 while (_leq(y, rem)) _add(qh, {1}), rem = _sub(rem, y);
 vector<int> q, r;
 if (xu + yv == s) {
 swap(q, qh), swap(r, rem);
 } else {
 vector<int> xnxt{begin(x), end(x) - xu - yv};
 copy(begin(rem), end(rem), back_inserter(xnxt));
 tie(q, r) = _divmod_dc(xnxt, y);
 q.resize(s - xu - yv, 0);
 copy(begin(qh), end(qh), back_inserter(q));
 }
 _shrink(q), _shrink(r);
 auto [q2, r2] = _divmod_1e9(r, {norm});
 assert(_is_zero(r2));
 return {q, q2};
 }

 
 static vector<int> _calc_inv(const vector<int>& a, int deg) {
 assert(!a.empty() && D / 2 <= a.back() and a.back() < D);
 int k = deg, c = a.size();
 while (k > 64) k = (k + 1) / 2;
 vector<int> z(c + k + 1);
 z.back() = 1;
 z = _divmod_naive(z, a).first;
 while (k < deg) {
 vector<int> s = _mul(z, z);
 s.insert(begin(s), 0);
 vector<int> t(2 * k + 1);
 copy(end(a) - min(c, 2 * k + 1), end(a), end(t) - min(c, 2 * k + 1));
 vector<int> u = _mul(s, t);
 u.erase(begin(u), begin(u) + 2 * k + 1);
 vector<int> w(k + 1, 0), w2 = _add(z, z);
 copy(begin(w2), end(w2), back_inserter(w));
 z = _sub(w, u);
 z.erase(begin(z));
 k *= 2;
 }
 z.erase(begin(z), begin(z) + k - deg);
 return z;
 }

 static pair<vector<int>, vector<int>> _divmod_newton(const vector<int>& a,
 const vector<int>& b) {
 if (_is_zero(b)) {
 cerr << "Divide by Zero Exception" << endl;
 exit(1);
 }
 if ((int)b.size() <= 64) return _divmod_naive(a, b);
 if ((int)a.size() - (int)b.size() <= 64) return _divmod_naive(a, b);
 int norm = D / (b.back() + 1);
 vector<int> x = _mul(a, {norm});
 vector<int> y = _mul(b, {norm});
 int s = x.size(), t = y.size();
 int deg = s - t + 2;
 vector<int> z = _calc_inv(y, deg);
 vector<int> q = _mul(x, z);
 q.erase(begin(q), begin(q) + t + deg);
 vector<int> yq = _mul(y, {q});
 while (_lt(x, yq)) q = _sub(q, {1}), yq = _sub(yq, y);
 vector<int> r = _sub(x, yq);
 while (_leq(y, r)) q = _add(q, {1}), r = _sub(r, y);
 _shrink(q), _shrink(r);
 auto [q2, r2] = _divmod_1e9(r, {norm});
 assert(_is_zero(r2));
 return {q, q2};
 }

 
 
 static string _itos(int x, bool zero_padding) {
 assert(0 <= x && x < D);
 string res;
 for (int i = 0; i < logD; i++) {
 res.push_back('0' + x % 10), x /= 10;
 }
 if (!zero_padding) {
 while (res.size() && res.back() == '0') res.pop_back();
 assert(!res.empty());
 }
 reverse(begin(res), end(res));
 return res;
 }

 
 template <typename I, enable_if_t<is_integral_v<I> ||
 is_same_v<I, __int128_t>>* = nullptr>
 static vector<int> _integer_to_vec(I x) {
 if constexpr (is_signed_v<I> || is_same_v<I, __int128_t>) {
 assert(x >= 0);
 }
 vector<int> res;
 while (x) res.push_back(x % D), x /= D;
 return res;
 }

 static long long _to_ll(const vector<int>& a) {
 long long res = 0;
 for (int i = (int)a.size() - 1; i >= 0; i--) res = res * D + a[i];
 return res;
 }

 static __int128_t _to_i128(const vector<int>& a) {
 __int128_t res = 0;
 for (int i = (int)a.size() - 1; i >= 0; i--) res = res * D + a[i];
 return res;
 }

 static void _dump(const vector<int>& a, string s = "") {
 if (!s.empty()) cerr << s << " : ";
 cerr << "{ ";
 for (int i = 0; i < (int)a.size(); i++) cerr << a[i] << ", ";
 cerr << "}" << endl;
 }
};

using bigint = MultiPrecisionInteger;





namespace GarnerImpl {

template <typename T,
 enable_if_t<is_integral_v<T> || is_same_v<T, __int128_t>>* = nullptr>
T inv_mod(T a, T m) {
 assert(0 <= a);
 if (a >= m) a %= m;
 T b = m, s = 1, t = 0;
 while (true) {
 if (a == 1) return s;
 t -= b / a * s;
 b %= a;
 if (b == 1) return t + m;
 s -= a / b * t;
 a %= b;
 }
}

pair<bigint, bigint> garner_dc(const vector<int>& r, const vector<int>& m) {
 int N = m.size();
 if (N == 0) return {0, 1};
 int B = 1;
 while (B < N) B *= 2;
 vector<bigint> tree(2 * B);
 for (int i = 0; i < B; i++) tree[B + i] = i < (int)m.size() ? m[i] : 1;
 for (int i = B - 1; i; i--) tree[i] = tree[i * 2 + 0] * tree[i * 2 + 1];
 auto calc = [&](auto&& rc, int ti, bigint X, bigint Y, int L,
 int R) -> bigint {
 if (N <= L) return 0;
 X %= tree[ti], Y %= tree[ti];
 if (L + 1 == R) {
 int xl = X.to_ll(), yl = Y.to_ll();
 int t = (1LL * (r[L] - xl) * inv_mod(yl, m[L])) % m[L];
 return t < 0 ? t + m[L] : t;
 }
 auto& prod = tree[ti * 2 + 0];
 int M = (L + R) / 2;
 auto xl = rc(rc, ti * 2 + 0, X, Y, L, M);
 auto xr = rc(rc, ti * 2 + 1, X + xl * Y, Y * prod, M, R);
 return xl + xr * prod;
 };
 bigint ans = calc(calc, 1, 0, 1, 0, B);
 return {ans, tree[1]};
}

pair<bigint, bigint> garner_naive(const vector<int>& r, const vector<int>& m) {
 int N = r.size();
 if (N == 0) return {0, 1};
 vector<int> y(N), x(N), t(N);
 for (int i = 0; i < N; i++) y[i] = 1 % m[i];
 for (int i = 0; i < N; ++i) {
 t[i] = (1LL * (r[i] - x[i]) * inv_mod(y[i], m[i])) % m[i];
 if (t[i] < 0) t[i] += m[i];
 for (int j = i + 1; j < N; j++) {
 x[j] = (x[j] + 1LL * y[j] * t[i]) % m[j];
 y[j] = 1LL * y[j] * m[i] % m[j];
 }
 }
 bigint ans = 0, mod = 1;
 for (int i = N - 1; i >= 0; --i) ans = ans * m[i] + t[i], mod *= m[i];
 return {ans, mod};
}



pair<bigint, bigint> garner_bigint(const vector<int>& r, const vector<int>& m) {
 assert(r.size() == m.size());
 if ((int)m.size() <= 3000) return garner_naive(r, m);
 return garner_dc(r, m);
}


pair<bigint, bigint> crt_bigint(const vector<int>& r, const vector<int>& m) {
 return garner_bigint(r, m);
}
} 

using GarnerImpl::crt_bigint;
using GarnerImpl::garner_bigint;









vector<int> small = {
 0, 1, 2, 4, 6, 8, 12, 16, 20,
 24, 30, 36, 42, 48, 54, 62, 70, 78,
 86, 94, 102, 114, 126, 138, 150, 162, 174,
 186, 202, 218, 234, 250, 266, 282, 298, 314,
 334, 354, 374, 394, 414, 434, 454, 474, 494,
 518, 542, 566, 590, 614, 638, 662, 686, 710,
 734, 764, 794, 824, 854, 884, 914, 944, 974,
 1004, 1034, 1064, 1100, 1136, 1172, 1208, 1244, 1280,
 1316, 1352, 1388, 1424, 1460, 1496, 1538, 1580, 1622,
 1664, 1706, 1748, 1790, 1832, 1874, 1916, 1958, 2000,
 2042, 2090, 2138, 2186, 2234, 2282, 2330, 2378, 2426,
 2474, 2522, 2570, 2618, 2666, 2714, 2768, 2822, 2876,
 2930, 2984, 3038, 3092, 3146, 3200, 3254, 3308, 3362,
 3416, 3470, 3524, 3586, 3648, 3710, 3772, 3834, 3896,
 3958, 4020, 4082, 4144, 4206, 4268, 4330, 4392, 4454,
 4516, 4586, 4656, 4726, 4796, 4866, 4936, 5006, 5076,
 5146, 5216, 5286, 5356, 5426, 5496, 5566, 5636, 5706,
 5784, 5862, 5940, 6018, 6096, 6174, 6252, 6330, 6408,
 6486, 6564, 6642, 6720, 6798, 6876, 6954, 7032, 7110,
 7196, 7282, 7368, 7454, 7540, 7626, 7712, 7798, 7884,
 7970, 8056, 8142, 8228, 8314, 8400, 8486, 8572, 8658,
 8744, 8838, 8932, 9026, 9120, 9214, 9308, 9402, 9496,
 9590, 9684, 9778, 9872, 9966, 10060, 10154, 10248, 10342,
 10436, 10530, 10624, 10726, 10828, 10930, 11032, 11134, 11236,
 11338, 11440, 11542, 11644, 11746, 11848, 11950, 12052, 12154,
 12256, 12358, 12460, 12562, 12664, 12766, 12880, 12994, 13108,
 13222, 13336, 13450, 13564, 13678, 13792, 13906, 14020, 14134,
 14248, 14362, 14476, 14590, 14704, 14818, 14932, 15046, 15160,
 15274, 15400, 15526, 15652, 15778, 15904, 16030, 16156, 16282,
 16408, 16534, 16660, 16786, 16912, 17038, 17164, 17290, 17416,
 17542, 17668, 17794, 17920, 18046, 18172, 18310, 18448, 18586,
 18724, 18862, 19000, 19138, 19276, 19414, 19552, 19690, 19828,
 19966, 20104, 20242, 20380, 20518, 20656, 20794, 20932, 21070,
 21208, 21346, 21484, 21634, 21784, 21934, 22084, 22234, 22384,
 22534, 22684, 22834, 22984, 23134, 23284, 23434, 23584, 23734,
 23884, 24034, 24184, 24334, 24484, 24634, 24784, 24934, 25084,
 25234, 25396, 25558, 25720, 25882, 26044, 26206, 26368, 26530,
 26692, 26854, 27016, 27178, 27340, 27502, 27664, 27826, 27988,
 28150, 28312, 28474, 28636, 28798, 28960, 29122, 29284, 29446,
 29620, 29794, 29968, 30142, 30316, 30490, 30664, 30838, 31012,
 31186, 31360, 31534, 31708, 31882, 32056, 32230, 32404, 32578,
 32752, 32926, 33100, 33274, 33448, 33622, 33796, 33970, 34144,
 34330, 34516, 34702, 34888, 35074, 35260, 35446, 35632, 35818,
 36004, 36190, 36376, 36562, 36748, 36934, 37120, 37306, 37492,
 37678, 37864, 38050, 38236, 38422, 38608, 38794, 38980, 39166,
 39352, 39554, 39756, 39958, 40160, 40362, 40564, 40766, 40968,
 41170, 41372, 41574, 41776, 41978, 42180, 42382, 42584, 42786,
 42988, 43190, 43392, 43594, 43796, 43998, 44200, 44402, 44604,
 44806, 45008, 45210, 45428, 45646, 45864, 46082, 46300, 46518,
 46736, 46954, 47172, 47390, 47608, 47826, 48044, 48262, 48480,
 48698, 48916, 49134, 49352, 49570, 49788, 50006, 50224, 50442,
 50660, 50878, 51096, 51314, 51532, 51750, 51984, 52218, 52452,
 52686, 52920, 53154, 53388, 53622, 53856, 54090, 54324, 54558,
 54792, 55026, 55260, 55494, 55728, 55962, 56196, 56430, 56664,
 56898, 57132, 57366, 57600, 57834, 58068, 58302, 58536, 58770,
 59004, 59254, 59504, 59754, 60004, 60254, 60504, 60754, 61004,
 61254, 61504, 61754, 62004, 62254, 62504, 62754, 63004, 63254,
 63504, 63754, 64004, 64254, 64504, 64754, 65004, 65254, 65504,
 65754, 66004, 66254, 66504, 66754, 67004, 67270, 67536, 67802,
 68068, 68334, 68600, 68866, 69132, 69398, 69664, 69930, 70196,
 70462, 70728, 70994, 71260, 71526, 71792, 72058, 72324, 72590,
 72856, 73122, 73388, 73654, 73920, 74186, 74452, 74718, 74984,
 75250, 75516, 75782, 76064, 76346, 76628, 76910, 77192, 77474,
 77756, 78038, 78320, 78602, 78884, 79166, 79448, 79730, 80012,
 80294, 80576, 80858, 81140, 81422, 81704, 81986, 82268, 82550,
 82832, 83114, 83396, 83678, 83960, 84242, 84524, 84806, 85088,
 85370, 85668, 85966, 86264, 86562, 86860, 87158, 87456, 87754,
 88052, 88350, 88648, 88946, 89244, 89542, 89840, 90138, 90436,
 90734, 91032, 91330, 91628, 91926, 92224, 92522, 92820, 93118,
 93416, 93714, 94012, 94310, 94608, 94906, 95204, 95502, 95800,
 96114, 96428, 96742, 97056, 97370, 97684, 97998, 98312, 98626,
 98940, 99254, 99568, 99882, 100196, 100510, 100824, 101138, 101452,
 101766, 102080, 102394, 102708, 103022, 103336, 103650, 103964, 104278,
 104592, 104906, 105220, 105534, 105848, 106162, 106476, 106790, 107104,
 107438, 107772, 108106, 108440, 108774, 109108, 109442, 109776, 110110,
 110444, 110778, 111112, 111446, 111780, 112114, 112448, 112782, 113116,
 113450, 113784, 114118, 114452, 114786, 115120, 115454, 115788, 116122,
 116456, 116790, 117124, 117458, 117792, 118126, 118460, 118794, 119128,
 119462, 119816, 120170, 120524, 120878, 121232, 121586, 121940, 122294,
 122648, 123002, 123356, 123710, 124064, 124418, 124772, 125126, 125480,
 125834, 126188, 126542, 126896, 127250, 127604, 127958, 128312, 128666,
 129020, 129374, 129728, 130082, 130436, 130790, 131144, 131498, 131852,
 132206, 132560, 132914, 133288, 133662, 134036, 134410, 134784, 135158,
 135532, 135906, 136280, 136654, 137028, 137402, 137776, 138150, 138524,
 138898, 139272, 139646, 140020, 140394, 140768, 141142, 141516, 141890,
 142264, 142638, 143012, 143386, 143760, 144134, 144508, 144882, 145256,
 145630, 146004, 146378, 146752, 147126, 147500, 147894, 148288, 148682,
 149076, 149470, 149864, 150258, 150652, 151046, 151440, 151834, 152228,
 152622, 153016, 153410, 153804, 154198, 154592, 154986, 155380, 155774,
 156168, 156562, 156956, 157350, 157744, 158138, 158532, 158926, 159320,
 159714, 160108, 160502, 160896, 161290, 161684, 162078, 162472, 162866,
 163260, 163674, 164088, 164502, 164916, 165330, 165744, 166158, 166572,
 166986, 167400, 167814, 168228, 168642, 169056, 169470, 169884, 170298,
 170712, 171126, 171540, 171954, 172368, 172782, 173196, 173610, 174024,
 174438, 174852, 175266, 175680, 176094, 176508, 176922, 177336, 177750,
 178164, 178578, 178992, 179406, 179820, 180234, 180668, 181102, 181536,
 181970, 182404, 182838, 183272, 183706, 184140, 184574, 185008, 185442,
 185876, 186310, 186744, 187178, 187612, 188046, 188480, 188914, 189348,
 189782, 190216, 190650, 191084, 191518, 191952, 192386, 192820, 193254,
 193688, 194122, 194556, 194990, 195424, 195858, 196292, 196726, 197160,
 197594, 198028, 198462, 198916, 199370, 199824, 200278, 200732, 201186,
 201640, 202094, 202548, 203002, 203456, 203910, 204364, 204818, 205272,
 205726, 206180, 206634, 207088, 207542, 207996, 208450, 208904, 209358,
 209812, 210266, 210720, 211174, 211628, 212082, 212536, 212990, 213444,
 213898, 214352, 214806, 215260, 215714, 216168, 216622, 217076, 217530,
 217984, 218458, 218932, 219406, 219880, 220354, 220828, 221302, 221776,
 222250, 222724, 223198, 223672, 224146, 224620, 225094, 225568, 226042,
 226516, 226990, 227464, 227938, 228412, 228886, 229360, 229834, 230308,
 230782, 231256, 231730, 232204, 232678, 233152, 233626, 234100, 234574,
 235048, 235522, 235996, 236470, 236944, 237418, 237892, 238366, 238840,
 239334, 239828, 240322, 240816, 241310, 241804, 242298, 242792, 243286,
 243780, 244274,
};

bigint f(bigint x) {
 
 
 
 
 __int128_t Y = sqrtl(2 * x.to_ld());
 __int128_t ylo = max<__int128_t>(1, Y * (1 - 1e-14));
 __int128_t yhi = __int128_t(Y * (1 + 1e-14)) + 3;
 bigint ok = ylo, ng = yhi;
 bigint y = x * 2;
 while (ok + 1 < ng) {
 bigint m = (ok + ng) / 2;
 (m * (m + 1) <= y ? ok : ng) = m;
 }
 return ok;
}

V<bigint> v;
template <int mod>
ll calc(bigint N) {
 using mint = LazyMontgomeryModInt<mod>;
 using vm = vector<mint>;
 using fps = FormalPowerSeries<mint>;

 static V<fps> pre;
 if (pre.empty()) {
 rep(d, 40) {
 int NN = d + 3;
 vm xs(NN), ys(NN);
 xs[0] = 0, ys[0] = (d ? 0 : 1);
 rep1(x, NN - 1) {
 xs[x] = x, ys[x] = mint{x}.pow(d);
 ys[x] += ys[x - 1];
 }
 fps f = PolynomialInterpolation(xs, ys);
 f.shrink();
 f.resize((sz(f) + 3) / 4 * 4);
 pre.push_back(f);
 }
 }
 auto rui = [&](fps f) -> fps {
 f.shrink();
 int n = sz(f);
 fps res(n + 5);
 rep(d, sz(f)) {
 for (int j = 0; j < sz(pre[d]); j += 4) {
 res[j + 0] += pre[d][j + 0] * f[d];
 res[j + 1] += pre[d][j + 1] * f[d];
 res[j + 2] += pre[d][j + 2] * f[d];
 res[j + 3] += pre[d][j + 3] * f[d];
 }
 }
 res.shrink();
 return res;
 };
 
 auto eval3 = [&](fps& f) -> void {
 int n = sz(f);
 f.resize(n + 2);
 for (int i = n - 1; i >= 0; i--) {
 f[i + 2] += f[i];
 f[i + 1] += f[i];
 f[i] = -f[i] - f[i];
 }
 };
 auto eval2 = [&](fps f) -> fps {
 fps res;
 f.shrink();
 int n = sz(f);
 mint inv2 = mint{2}.inverse(), c = 1;
 rep(i, n) f[i] *= c, c *= inv2;
 for (int i = n - 1; i >= 0; i--) {
 eval3(res);
 res += f[i];
 }
 res.shrink();
 return res;
 };
 if (v.empty()) {
 for (bigint x = N; sz(v) < 5; x = f(x)) v.push_back(x);
 }
 V<fps> p(sz(v));
 p[0] = fps{1};
 rep1(i, sz(v) - 1) {
 fps q = rui(p[i - 1]);
 fps res = fps{q.eval((v[i - 1] % mod).to_ll())};
 res -= eval2(q);
 p[i] = res;
 }
 fps f = p.back();
 fps fr = rui(f);
 int vb = v.back().to_ll();
 mint res = 0;
 for (int x = 1, y = 1; x <= vb; x = y) {
 int d = small[x] - small[x - 1];
 while (y <= vb and small[y] - small[y - 1] == d) y++;
 mint cur = fr.eval(y - 1) - fr.eval(x - 1);
 res += cur * d;
 }
 return res.get();
}

void q() {
 bigint N;
#ifdef NyaanDebug
 N = 1;
 rep(_, 40) N *= 10;
#else
 cin >> N;
#endif

 v.clear();

 vector<int> mod, rem;
#define z(p) \
 mod.push_back(p); \
 rem.push_back(calc<p>(N));
 z(943718401);
 z(950009857);
 z(957349889);
 z(962592769);
 z(972029953);
 z(975175681);
 z(976224257);
 z(985661441);
 z(998244353);
 cout << garner_bigint(rem, mod).first << "\n";
}

int main() {
 int t = 1;
 cin >> t;
 while (t--) q();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 14ms
memory: 4472kb

input:

4
10
100
1000
987654321123456789

output:

30
2522
244274
235139898689017607381017686096176798

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 779ms
memory: 4352kb

input:

10000
613939207402503646
408978283345333197
976677512231308716
629053280913850466
148712339
236220313279945487
590396556995977994
9226
215693877607285701
649702683896705
343173887453826567
847003949499596615
867133040287550291
159928123569892354
864534948175618654
209739383170746721
4295456752378791...

output:

91030728117067063595428375419346402
40469246710473908695676971059074376
229951682342450470520349294486964970
95558039501640054006660579352994194
5418340556433412
13536357243714243974772906966693552
84197207203086091733385317631619504
20934656
11291075621624104092841708040651034
104019777231815308683...

result:

ok 10000 lines

Test #3:

score: 0
Accepted
time: 153ms
memory: 4420kb

input:

1000
6403632579734162001008235137760132245297
1307698664787972023762442022139627469666
668870338048562416595095770565441759482
5092270
8806864498496723812760785099973409980711
2178464116010775202899984038946879469187
204381824371
8638495456004772442511643693521120926431
45954412333082528168092594892...

output:

9822905445826021159291522774438593145331066315784567505849706373529921001845336
409552844852728078841401625660602682494169687360338453221088647649526339235330
107160056181509722327918304399871120167096186888511567354513496578559803370274
6354453295964
185817323129718525790559571287806776421589155460...

result:

ok 1000 lines

Test #4:

score: 0
Accepted
time: 296ms
memory: 4400kb

input:

2000
2587627816607030340103003175959756184662
7728753705064569253253827797978613582938
6507847628603052973674714721
6989857636824717968431061258300652290539
4734281027640913533293237760425416005062
9411123735455625690768098631226366597446
8309753930995253536640660321476246470149
63565157427098067709...

output:

1603610451790269237852635641930301658193367441164307312552842461667027137613454
14309943493171496191506053530749878345271155973702143153225815632926701434642842
10414697803791950572309383355053080338229465674803757518
11704102206894264239190418551798088411458157423624785417336589284698838535371266
5...

result:

ok 2000 lines

Test #5:

score: 0
Accepted
time: 757ms
memory: 4400kb

input:

5000
6701025283447923273597553918313900029215
1618190467906965189844764312914748628527
2135261797018456059451326428589353332126
8027429917075086154217136768450383650445
8263301530632040969183919589286944799002
376886964626613356031779878
1191561726595055348898524031899625958102
453561433135467095374...

output:

10756647971303093856509780939435968749671842310025383400207261624750784751725876
627115945498078452730193858129037650151913122482133071938783012929533045529940
1091921493223233296724616654299408127388059493196845968361252389122720100379070
154375707400033063668088306493199269136326791073281723548116...

result:

ok 5000 lines

Test #6:

score: 0
Accepted
time: 1472ms
memory: 4376kb

input:

10000
260865660295317278841
5232352637620496342310336202478387251106
7108789244285764135987032973912918380
12766535319519586095540974948550152061
5138306300154916887884637635208203681949
7603163140266839847784708214115317398585
149590294591155205497765668766786424787
63283557694500045989299147454323...

output:

16323111740957704392106241109891718054228
6557703685144914472554701877112177422100848067214049191882271294010013621817762
12143115079716078114619105501427985631361994195400195527663921137836417758
39139456824156526604158618001888125076786817219954316014947704612553450312
6324051018379978443719363340...

result:

ok 10000 lines