#line 1 "..\\Main.cpp" #include #include #include #include #include #line 5 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\fps\\fps-ntt.hpp" #include #line 3 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\math-modulo\\modulo-primitive-root.hpp" #include namespace nachia{ template struct PrimitiveRoot{ using u64 = unsigned long long; static constexpr u64 powm(u64 a, u64 i) { u64 res = 1, aa = a; while(i){ if(i & 1) res = res * aa % MOD; aa = aa * aa % MOD; i /= 2; } return res; } static constexpr bool ExamineVal(unsigned int g){ unsigned int t = MOD - 1; for(u64 d=2; d*d<=t; d++) if(t % d == 0){ if(powm(g, (MOD - 1) / d) == 1) return false; while(t % d == 0) t /= d; } if(t != 1) if(powm(g, (MOD - 1) / t) == 1) return false; return true; } static constexpr unsigned int GetVal(){ for(unsigned int x=2; x class Comb{ private: static constexpr int MOD = Modint::mod(); std::vector F; std::vector iF; public: void extend(int newN){ int prevN = (int)F.size() - 1; if(newN >= MOD) newN = MOD - 1; if(prevN >= newN) return; F.resize(newN+1); iF.resize(newN+1); for(int i=prevN+1; i<=newN; i++) F[i] = F[i-1] * Modint::raw(i); iF[newN] = F[newN].inv(); for(int i=newN; i>prevN; i--) iF[i-1] = iF[i] * Modint::raw(i); } Comb(int n = 1){ F.assign(2, Modint(1)); iF.assign(2, Modint(1)); extend(n); } Modint factorial(int n) const { return F[n]; } Modint invFactorial(int n) const { return iF[n]; } Modint invOf(int n) const { return iF[n] * F[n-1]; } Modint comb(int n, int r) const { if(n < 0 || n < r || r < 0) return Modint(0); return F[n] * iF[r] * iF[n-r]; } Modint invComb(int n, int r) const { if(n < 0 || n < r || r < 0) return Modint(0); return iF[n] * F[r] * F[n-r]; } Modint perm(int n, int r) const { if(n < 0 || n < r || r < 0) return Modint(0); return F[n] * iF[n-r]; } Modint invPerm(int n, int r) const { if(n < 0 || n < r || r < 0) return Modint(0); return iF[n] * F[n-r]; } Modint operator()(int n, int r) const { return comb(n,r); } }; } // namespace nachia #line 4 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\misc\\bit-operations.hpp" namespace nachia{ int Popcount(unsigned long long c) noexcept { #ifdef __GNUC__ return __builtin_popcountll(c); #else c = (c & (~0ull/3)) + ((c >> 1) & (~0ull/3)); c = (c & (~0ull/5)) + ((c >> 2) & (~0ull/5)); c = (c & (~0ull/17)) + ((c >> 4) & (~0ull/17)); c = (c * (~0ull/257)) >> 56; return c; #endif } // please ensure x != 0 int MsbIndex(unsigned long long x) noexcept { #ifdef __GNUC__ return 63 - __builtin_clzll(x); #else int res = 0; for(int d=32; d>0; d>>=1) if(x >> d){ res |= d; x >>= d; } return res; #endif } // please ensure x != 0 int LsbIndex(unsigned long long x) noexcept { #ifdef __GNUC__ return __builtin_ctzll(x); #else return MsbIndex(x & -x); #endif } } #line 2 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\fps\\ntt-interface.hpp" namespace nachia { template struct NttInterface{ template void Butterfly(Iter, int) const {} template void IButterfly(Iter, int) const {} template void BitReversal(Iter a, int N) const { for(int i=0, j=0; j>1; k > (i^=k); k>>=1); } } }; } // namespace nachia #line 5 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\fps\\ntt-acl.hpp" #include #line 8 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\fps\\ntt-acl.hpp" #include namespace nachia{ constexpr int bsf_constexpr(unsigned int n) { int x = 0; while (!(n & (1 << x))) x++; return x; } template struct NttFromAcl : NttInterface { using u32 = unsigned int; using u64 = unsigned long long; static int ceil_pow2(int n) { int x = 0; while ((1U << x) < (u32)(n)) x++; return x; } struct fft_info { static constexpr u32 g = nachia::PrimitiveRoot::val; static constexpr int rank2 = bsf_constexpr(mint::mod()-1); std::array root; std::array iroot; std::array rate2; std::array irate2; std::array rate3; std::array irate3; fft_info(){ root[rank2] = mint(g).pow((mint::mod() - 1) >> rank2); iroot[rank2] = root[rank2].inv(); for(int i=rank2-1; i>=0; i--){ root[i] = root[i+1] * root[i+1]; iroot[i] = iroot[i+1] * iroot[i+1]; } mint prod = 1, iprod = 1; for(int i=0; i<=rank2-2; i++){ rate2[i] = root[i+2] * prod; irate2[i] = iroot[i+2] * iprod; prod *= iroot[i+2]; iprod *= root[i+2]; } prod = 1; iprod = 1; for(int i=0; i<=rank2-3; i++){ rate3[i] = root[i+3] * prod; irate3[i] = iroot[i+3] * iprod; prod *= iroot[i+3]; iprod *= root[i+3]; } } }; template void Butterfly(RandomAccessIterator a, int n) const { int h = ceil_pow2(n); static const fft_info info; int len = 0; while(len < h){ if(h-len == 1){ int p = 1 << (h-len-1); mint rot = 1; for(int s=0; s<(1< void IButterfly(RandomAccessIterator a, int n) const { int h = ceil_pow2(n); static const fft_info info; constexpr int MOD = mint::mod(); int len = h; while(len){ if(len == 1){ int p = 1 << (h-len); mint irot = 1; for(int s=0; s<(1<<(len-1)); s++){ int offset = s << (h-len+1); for(int i=0; i> struct FpsNtt { public: using Fps = FpsNtt; using ElemTy = Elem; static constexpr unsigned int MOD = Elem::mod(); static constexpr int CONV_THRES = 30; static const NttInst nttInst; static const unsigned int zeta = nachia::PrimitiveRoot::GetVal(); private: using u32 = unsigned int; static Elem ZeroElem() noexcept { return Elem(0); } static Elem OneElem() noexcept { return Elem(1); } static Comb comb; std::vector a; int RSZ(int& sz) const { return sz = (sz < 0 ? size() : sz); } public: int size() const noexcept { return a.size(); } Elem& operator[](int x) noexcept { return a[x]; } const Elem& operator[](int x) const noexcept { return a[x]; } Elem getCoeff(int x) const noexcept { return (0 <= x && x < size()) ? a[x] : ZeroElem(); } static Comb& GetComb() { return comb; } static int BestNttSize(int x) noexcept { assert(x); return 1 << MsbIndex(x*2-1); } Fps move(){ return std::move(*this); } Fps& set(int i, Elem c){ a[i] = c; return *this; } Fps& removeLeadingZeros(){ int newsz = size(); while(newsz && a[newsz-1].val() == 0) newsz--; a.resize(newsz); if((int)a.capacity() / 4 > newsz) a.shrink_to_fit(); return *this; } FpsNtt(){} FpsNtt(int sz) : a(sz, ZeroElem()) {} FpsNtt(int sz, Elem e) : a(sz, e) {} FpsNtt(std::vector&& src) : a(std::move(src)) {} FpsNtt(const std::vector& src) : a(src) {} Fps& ntt() { capSize(BestNttSize(size())); nttInst.Butterfly(a.begin(), size()); return *this; } Fps& intt() { nttInst.IButterfly(a.begin(), a.size()); return times(Elem::raw(size()).inv()); } Fps nttDouble(Fps vanilla) const { int n = size(); assert(n == (n&-n)); // n is a power of 2 Elem q = Elem::raw(zeta).pow((Elem::mod() - 1) / (n*2)); Elem qq = OneElem(); for(int i=0; i srcR = max(srcL, size()); // if resSz is unspecified -> resSz = destL + srcR - srcL Fps clip(int srcL, int srcR = -1, int destL = 0, int resSz = -1) const { srcR = RSZ(srcR); if(resSz < 0) resSz = destL + srcR - srcL; int rj = std::min(std::min(srcR, size()) - srcL, resSz - destL); Fps res(resSz); for(int j=std::max(0, -srcL); j b.size()) return convolution(b, a, sz); if(sz < 0) sz = std::max(0, a.size() + b.size() - 1); std::vector res(sz); for(int i=0; i=1; i--) a[i] = a[i-1] * comb.invOf(i); return set(0, ZeroElem()); } Fps& EgfToOgf(){ comb.extend(size()); for(int i=0; i= (n-1) / k + 1) return Fps(n); Elem a0 = a[ctz]; return clip(ctz, ctz+n-ctz*k).times(a0.inv()).log().times(Elem(k)).exp().times(a0.pow(k)).clip(0, -1, ctz*k); } auto begin(){ return a.begin(); } auto end(){ return a.end(); } auto begin() const { return a.begin(); } auto end() const { return a.end(); } std::string toString(std::string beg = "[ ", std::string delim = " ", std::string en = " ]") const { std::string res = beg; bool f = false; for(auto x : a){ if(f){ res += delim; } f = true; res += std::to_string(x.val()); } res += en; return res; } std::vector getVectorMoved(){ return std::move(a); } Fps& operator+=(const Fps& r){ capSize(std::max(size(), r.size())); for(int i=0; i=0; i--) res = res * x + a[i]; return res; } }; template Comb FpsNtt::comb; template const NttInst FpsNtt::nttInst; } // namespace nachia #line 4 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\fps\\evaluate-on-geometric-sequence.hpp" namespace nachia{ template Fps EvaluateOnGeometricSequence(const Fps& a, typename Fps::ElemTy w, int len){ using Elem = typename Fps::ElemTy; int n = a.size(); int x = std::max(n,len); if(len == 0) return Fps(); if(w.val() == 0 || a.size() == 0) return Fps(std::vector(len, a.getCoeff(0))).set(0,a.eval(1)).move(); int nttsz = Fps::BestNttSize(n+len-1); Fps iwti(nttsz), wti(nttsz); iwti[0] = wti[0] = Elem::raw(1); for(int i=1; i=1; i--) iwti[i-1] = iwti[i] * iwti[i-1]; return a.clip(0, nttsz).mulEach(iwti, n).reverse(n) .ntt().mulEach(wti.ntt()).intt().clip(n-1, len+n-1).mulEach(iwti, len).move(); } } // namespace nachia #line 7 "..\\Main.cpp" using namespace std; using i32 = int; using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i<(int)(n); i++) const i64 INF = 1001001001001001001; using Modint = atcoder::static_modint<998244353>; using Fps = nachia::FpsNtt; int main(){ int T; cin >> T; rep(t,T){ Modint A; { int a; cin >> a; A = a; } int N; cin >> N; int n = 1; while((i64)(n+1)*(n+1) <= N) n++; Fps f(n); int off = N - n*n; Modint ans = 0; { Modint t0 = A; Modint t1 = 1; Modint t2 = 1; rep(i,off){ ans += t2; t2 *= t1 * t1 * t0; t1 *= t0; } } Modint An = A.pow(n); Modint Ann = An.pow(n); { Modint t0 = A; Modint t1 = A.pow(off); Modint t2 = t1.pow(off); rep(i,n){ f[i] = t2; t2 *= t1 * t1 * t0; t1 *= t0; } } auto ev = nachia::EvaluateOnGeometricSequence(f, An*An, n); { Modint t0 = Ann; Modint t1 = An.pow(off); Modint t2 = 1; rep(i,n){ ans += ev[i] * t2; t2 *= t1 * t1 * t0; t1 *= t0; } } cout << ans.val() << '\n'; } return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;