結果
問題 | No.2747 Permutation Adjacent Sum |
ユーザー | Rubikun |
提出日時 | 2024-12-25 20:41:26 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 41,458 bytes |
コンパイル時間 | 7,167 ms |
コンパイル使用メモリ | 334,516 KB |
実行使用メモリ | 40,252 KB |
最終ジャッジ日時 | 2024-12-25 20:42:20 |
合計ジャッジ時間 | 24,469 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 243 ms
22,568 KB |
testcase_01 | AC | 27 ms
8,976 KB |
testcase_02 | AC | 156 ms
15,884 KB |
testcase_03 | AC | 28 ms
8,832 KB |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | AC | 240 ms
22,440 KB |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | AC | 64 ms
11,728 KB |
testcase_11 | AC | 232 ms
22,484 KB |
testcase_12 | AC | 14 ms
8,192 KB |
testcase_13 | AC | 131 ms
15,148 KB |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | AC | 244 ms
22,540 KB |
testcase_17 | RE | - |
testcase_18 | WA | - |
testcase_19 | AC | 41 ms
10,020 KB |
testcase_20 | RE | - |
testcase_21 | RE | - |
testcase_22 | RE | - |
testcase_23 | AC | 260 ms
21,772 KB |
testcase_24 | AC | 238 ms
22,084 KB |
testcase_25 | AC | 129 ms
15,692 KB |
testcase_26 | RE | - |
testcase_27 | RE | - |
testcase_28 | RE | - |
testcase_29 | RE | - |
testcase_30 | RE | - |
testcase_31 | RE | - |
testcase_32 | RE | - |
testcase_33 | RE | - |
testcase_34 | RE | - |
testcase_35 | AC | 11 ms
8,192 KB |
testcase_36 | AC | 10 ms
8,320 KB |
testcase_37 | AC | 10 ms
8,192 KB |
testcase_38 | AC | 10 ms
8,192 KB |
testcase_39 | AC | 10 ms
8,064 KB |
testcase_40 | AC | 10 ms
8,192 KB |
testcase_41 | AC | 9 ms
8,320 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; typedef long long ll; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; } #define vi vector<int> #define vl vector<ll> #define vii vector<pair<int,int>> #define vll vector<pair<ll,ll>> #define vvi vector<vector<int>> #define vvl vector<vector<ll>> #define vvii vector<vector<pair<int,int>>> #define vvll vector<vector<pair<ll,ll>>> #define vst vector<string> #define pii pair<int,int> #define pll pair<ll,ll> #define pb push_back #define all(x) (x).begin(),(x).end() #define mkunique(x) sort(all(x));(x).erase(unique(all(x)),(x).end()) #define fi first #define se second #define mp make_pair #define si(x) int(x.size()) const int mod=998244353,MAX=400005,INF=15<<26; // FPS 全部載せ // from: https://gist.github.com/yosupo06/ddd51afb727600fd95d9d8ad6c3c80c9 // (based on AtCoder STL) #include <algorithm> #include <array> #ifdef _MSC_VER #include <intrin.h> #endif namespace atcoder { namespace internal { int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } int bsf(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } } // namespace internal } // namespace atcoder #include <utility> namespace atcoder { namespace internal { constexpr long long safe_mod(long long x, long long m) { x %= m; if (x < 0) x += m; return x; } struct barrett { unsigned int _m; unsigned long long im; barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {} unsigned int umod() const { return _m; } unsigned int mul(unsigned int a, unsigned int b) const { unsigned long long z = a; z *= b; #ifdef _MSC_VER unsigned long long x; _umul128(z, im, &x); #else unsigned long long x = (unsigned long long)(((unsigned __int128)(z)*im) >> 64); #endif unsigned int v = (unsigned int)(z - x * _m); if (_m <= v) v += _m; return v; } }; constexpr long long pow_mod_constexpr(long long x, long long n, int m) { if (m == 1) return 0; unsigned int _m = (unsigned int)(m); unsigned long long r = 1; unsigned long long y = safe_mod(x, m); while (n) { if (n & 1) r = (r * y) % _m; y = (y * y) % _m; n >>= 1; } return r; } constexpr bool is_prime_constexpr(int n) { if (n <= 1) return false; if (n == 2 || n == 7 || n == 61) return true; if (n % 2 == 0) return false; long long d = n - 1; while (d % 2 == 0) d /= 2; for (long long a : {2, 7, 61}) { long long t = d; long long y = pow_mod_constexpr(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = y * y % n; t <<= 1; } if (y != n - 1 && t % 2 == 0) { return false; } } return true; } template <int n> constexpr bool is_prime = is_prime_constexpr(n); constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) { a = safe_mod(a, b); if (a == 0) return {b, 0}; long long s = b, t = a; long long m0 = 0, m1 = 1; while (t) { long long u = s / t; s -= t * u; m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b auto tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } if (m0 < 0) m0 += b / s; return {s, m0}; } constexpr int primitive_root_constexpr(int m) { if (m == 2) return 1; if (m == 167772161) return 3; if (m == 469762049) return 3; if (m == 754974721) return 11; if (m == 998244353) return 3; int divs[20] = {}; divs[0] = 2; int cnt = 1; int x = (m - 1) / 2; while (x % 2 == 0) x /= 2; for (int i = 3; (long long)(i)*i <= x; i += 2) { if (x % i == 0) { divs[cnt++] = i; while (x % i == 0) { x /= i; } } } if (x > 1) { divs[cnt++] = x; } for (int g = 2;; g++) { bool ok = true; for (int i = 0; i < cnt; i++) { if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) { ok = false; break; } } if (ok) return g; } } template <int m> constexpr int primitive_root = primitive_root_constexpr(m); } // namespace internal } // namespace atcoder #include <cassert> #include <numeric> #include <type_traits> namespace atcoder { namespace internal { #ifndef _MSC_VER template <class T> using is_signed_int128 = typename std::conditional<std::is_same<T, __int128_t>::value || std::is_same<T, __int128>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int128 = typename std::conditional<std::is_same<T, __uint128_t>::value || std::is_same<T, unsigned __int128>::value, std::true_type, std::false_type>::type; template <class T> using make_unsigned_int128 = typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t, unsigned __int128>; template <class T> using is_integral = typename std::conditional<std::is_integral<T>::value || is_signed_int128<T>::value || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_signed_int = typename std::conditional<(is_integral<T>::value && std::is_signed<T>::value) || is_signed_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<(is_integral<T>::value && std::is_unsigned<T>::value) || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional< is_signed_int128<T>::value, make_unsigned_int128<T>, typename std::conditional<std::is_signed<T>::value, std::make_unsigned<T>, std::common_type<T>>::type>::type; #else template <class T> using is_integral = typename std::is_integral<T>; template <class T> using is_signed_int = typename std::conditional<is_integral<T>::value && std::is_signed<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<is_integral<T>::value && std::is_unsigned<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional<is_signed_int<T>::value, std::make_unsigned<T>, std::common_type<T>>::type; #endif template <class T> using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>; template <class T> using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>; template <class T> using to_unsigned_t = typename to_unsigned<T>::type; } // namespace internal } // namespace atcoder #include <cassert> #include <numeric> #include <type_traits> #ifdef _MSC_VER #include <intrin.h> #endif namespace atcoder { namespace internal { struct modint_base {}; struct static_modint_base : modint_base {}; template <class T> using is_modint = std::is_base_of<modint_base, T>; template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>; } // namespace internal template <int m, std::enable_if_t<(1 <= m)>* = nullptr> struct static_modint : internal::static_modint_base { using mint = static_modint; public: static constexpr int mod() { return m; } static mint raw(int v) { mint x; x._v = v; return x; } static_modint() : _v(0) {} template <class T, internal::is_signed_int_t<T>* = nullptr> static_modint(T v) { long long x = (long long)(v % (long long)(umod())); if (x < 0) x += umod(); _v = (unsigned int)(x); } template <class T, internal::is_unsigned_int_t<T>* = nullptr> static_modint(T v) { _v = (unsigned int)(v % umod()); } static_modint(bool v) { _v = ((unsigned int)(v) % umod()); } unsigned int val() const { return _v; } mint& operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint& operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint& operator+=(const mint& rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint& operator-=(const mint& rhs) { _v -= rhs._v; if (_v >= umod()) _v += umod(); return *this; } mint& operator*=(const mint& rhs) { unsigned long long z = _v; z *= rhs._v; _v = (unsigned int)(z % umod()); return *this; } mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { if (prime) { assert(_v); return pow(umod() - 2); } else { auto eg = internal::inv_gcd(_v, m); assert(eg.first == 1); return eg.second; } } friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint& lhs, const mint& rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint& lhs, const mint& rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static constexpr unsigned int umod() { return m; } static constexpr bool prime = internal::is_prime<m>; }; template <int id> struct dynamic_modint : internal::modint_base { using mint = dynamic_modint; public: static int mod() { return (int)(bt.umod()); } static void set_mod(int m) { assert(1 <= m); bt = internal::barrett(m); } static mint raw(int v) { mint x; x._v = v; return x; } dynamic_modint() : _v(0) {} template <class T, internal::is_signed_int_t<T>* = nullptr> dynamic_modint(T v) { long long x = (long long)(v % (long long)(mod())); if (x < 0) x += mod(); _v = (unsigned int)(x); } template <class T, internal::is_unsigned_int_t<T>* = nullptr> dynamic_modint(T v) { _v = (unsigned int)(v % mod()); } dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); } unsigned int val() const { return _v; } mint& operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint& operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint& operator+=(const mint& rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint& operator-=(const mint& rhs) { _v += mod() - rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint& operator*=(const mint& rhs) { _v = bt.mul(_v, rhs._v); return *this; } mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { auto eg = internal::inv_gcd(_v, mod()); assert(eg.first == 1); return eg.second; } friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint& lhs, const mint& rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint& lhs, const mint& rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static internal::barrett bt; static unsigned int umod() { return bt.umod(); } }; template <int id> internal::barrett dynamic_modint<id>::bt = 998244353; using modint998244353 = static_modint<998244353>; using modint1000000007 = static_modint<1000000007>; using modint = dynamic_modint<-1>; namespace internal { template <class T> using is_static_modint = std::is_base_of<internal::static_modint_base, T>; template <class T> using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>; template <class> struct is_dynamic_modint : public std::false_type {}; template <int id> struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {}; template <class T> using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>; } // namespace internal } // namespace atcoder #include <cassert> #include <type_traits> #include <vector> namespace atcoder { namespace internal { template <class mint, internal::is_static_modint_t<mint>* = nullptr> void butterfly(std::vector<mint>& a) { static constexpr int g = internal::primitive_root<mint::mod()>; int n = int(a.size()); int h = internal::ceil_pow2(n); static bool first = true; static mint sum_e[30]; // sum_e[i] = ies[0] * ... * ies[i - 1] * es[i] if (first) { first = false; mint es[30], ies[30]; // es[i]^(2^(2+i)) == 1 int cnt2 = bsf(mint::mod() - 1); mint e = mint(g).pow((mint::mod() - 1) >> cnt2), ie = e.inv(); for (int i = cnt2; i >= 2; i--) { es[i - 2] = e; ies[i - 2] = ie; e *= e; ie *= ie; } mint now = 1; for (int i = 0; i < cnt2 - 2; i++) { sum_e[i] = es[i] * now; now *= ies[i]; } } for (int ph = 1; ph <= h; ph++) { int w = 1 << (ph - 1), p = 1 << (h - ph); mint now = 1; for (int s = 0; s < w; s++) { int offset = s << (h - ph + 1); for (int i = 0; i < p; i++) { auto l = a[i + offset]; auto r = a[i + offset + p] * now; a[i + offset] = l + r; a[i + offset + p] = l - r; } now *= sum_e[bsf(~(unsigned int)(s))]; } } } template <class mint, internal::is_static_modint_t<mint>* = nullptr> void butterfly_inv(std::vector<mint>& a) { static constexpr int g = internal::primitive_root<mint::mod()>; int n = int(a.size()); int h = internal::ceil_pow2(n); static bool first = true; static mint sum_ie[30]; // sum_ie[i] = es[0] * ... * es[i - 1] * ies[i] if (first) { first = false; mint es[30], ies[30]; // es[i]^(2^(2+i)) == 1 int cnt2 = bsf(mint::mod() - 1); mint e = mint(g).pow((mint::mod() - 1) >> cnt2), ie = e.inv(); for (int i = cnt2; i >= 2; i--) { es[i - 2] = e; ies[i - 2] = ie; e *= e; ie *= ie; } mint now = 1; for (int i = 0; i < cnt2 - 2; i++) { sum_ie[i] = ies[i] * now; now *= es[i]; } } for (int ph = h; ph >= 1; ph--) { int w = 1 << (ph - 1), p = 1 << (h - ph); mint inow = 1; for (int s = 0; s < w; s++) { int offset = s << (h - ph + 1); for (int i = 0; i < p; i++) { auto l = a[i + offset]; auto r = a[i + offset + p]; a[i + offset] = l + r; a[i + offset + p] = (unsigned long long)(mint::mod() + l.val() - r.val()) * inow.val(); } inow *= sum_ie[bsf(~(unsigned int)(s))]; } } } } // namespace internal template <class mint, internal::is_static_modint_t<mint>* = nullptr> std::vector<mint> convolution(std::vector<mint> a, std::vector<mint> b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; if (std::min(n, m) <= 60) { if (n < m) { std::swap(n, m); std::swap(a, b); } std::vector<mint> ans(n + m - 1); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ans[i + j] += a[i] * b[j]; } } return ans; } int z = 1 << internal::ceil_pow2(n + m - 1); a.resize(z); internal::butterfly(a); b.resize(z); internal::butterfly(b); for (int i = 0; i < z; i++) { a[i] *= b[i]; } internal::butterfly_inv(a); a.resize(n + m - 1); mint iz = mint(z).inv(); for (int i = 0; i < n + m - 1; i++) a[i] *= iz; return a; } template <unsigned int mod = 998244353, class T, std::enable_if_t<internal::is_integral<T>::value>* = nullptr> std::vector<T> convolution(const std::vector<T>& a, const std::vector<T>& b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; using mint = static_modint<mod>; std::vector<mint> a2(n), b2(m); for (int i = 0; i < n; i++) { a2[i] = mint(a[i]); } for (int i = 0; i < m; i++) { b2[i] = mint(b[i]); } auto c2 = convolution(move(a2), move(b2)); std::vector<T> c(n + m - 1); for (int i = 0; i < n + m - 1; i++) { c[i] = c2[i].val(); } return c; } std::vector<long long> convolution_ll(const std::vector<long long>& a, const std::vector<long long>& b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; static constexpr unsigned long long MOD1 = 754974721; // 2^24 static constexpr unsigned long long MOD2 = 167772161; // 2^25 static constexpr unsigned long long MOD3 = 469762049; // 2^26 static constexpr unsigned long long M2M3 = MOD2 * MOD3; static constexpr unsigned long long M1M3 = MOD1 * MOD3; static constexpr unsigned long long M1M2 = MOD1 * MOD2; static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3; static constexpr unsigned long long i1 = internal::inv_gcd(MOD2 * MOD3, MOD1).second; static constexpr unsigned long long i2 = internal::inv_gcd(MOD1 * MOD3, MOD2).second; static constexpr unsigned long long i3 = internal::inv_gcd(MOD1 * MOD2, MOD3).second; auto c1 = convolution<MOD1>(a, b); auto c2 = convolution<MOD2>(a, b); auto c3 = convolution<MOD3>(a, b); std::vector<long long> c(n + m - 1); for (int i = 0; i < n + m - 1; i++) { unsigned long long x = 0; x += (c1[i] * i1) % MOD1 * M2M3; x += (c2[i] * i2) % MOD2 * M1M3; x += (c3[i] * i3) % MOD3 * M1M2; long long diff = c1[i] - internal::safe_mod((long long)(x), (long long)(MOD1)); if (diff < 0) diff += MOD1; static constexpr unsigned long long offset[5] = { 0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3}; x -= offset[diff % 5]; c[i] = x; } return c; } } // namespace atcoder using mint=atcoder::modint998244353; vector<mint> prebat(vector<mint> S,int szsum){ int z = 1 << atcoder::internal::ceil_pow2(szsum-1); auto res=S; res.resize(z); atcoder::internal::butterfly(res); return res; } // szsum = aの配列の長さ + bの配列の長さ vector<mint> sufbat(vector<mint> S,int szsum){ int z = 1 << atcoder::internal::ceil_pow2(szsum-1); auto res=S; atcoder::internal::butterfly_inv(res); res.resize(szsum-1); mint iz = mint(z).inv(); for (int i = 0; i < szsum - 1; i++) res[i] *= iz; return res; } // szsum = aの配列の長さ + bの配列の長さ mint inv[MAX],fac[MAX],finv[MAX]; void make(){ fac[0]=fac[1]=1; finv[0]=finv[1]=1; inv[1]=1; for(int i=2;i<MAX;i++){ inv[i]=-inv[mod%i]*(mod/i); fac[i]=fac[i-1]*i; finv[i]=finv[i-1]*inv[i]; } } mint comb(ll a,ll b){ if(a<b) return 0; return fac[a]*finv[b]*finv[a-b]; } mint perm(ll a,ll b){ if(a<b) return 0; return fac[a]*finv[a-b]; } vector<mint> bibun(vector<mint> F,int deg){ vector<mint> res(deg+1); for(int i=1;i<si(F)&&i-1<=deg;i++){ res[i-1]=F[i]*i; } return res; } vector<mint> sekibun(vector<mint> F,int deg){ vector<mint> res(deg+1); for(int i=0;i<min(si(F),deg);i++){ res[i+1]=F[i]*inv[i+1]; } return res; } vector<mint> invv(vector<mint> F,int deg){ assert(F[0]!=0); mint kake=mint(F[0]).inv(); for(int i=0;i<si(F);i++){ F[i]*=kake; } vector<mint> G(1,1); int len=1; while(len<=deg){ vector<mint> f=F;f.resize(len*2); vector<mint> g=G;g.resize(len*2); atcoder::internal::butterfly(f); atcoder::internal::butterfly(g); for(int i=0;i<len*2;i++) f[i]*=g[i]; atcoder::internal::butterfly_inv(f); vector<mint> nf(len*2); for(int i=len;i<2*len;i++) nf[i-len]=f[i]; f=nf; atcoder::internal::butterfly(f); for(int i=0;i<len*2;i++) f[i]*=g[i]; atcoder::internal::butterfly_inv(f); mint iz=mint(len*2).inv(); mint coe=-iz*iz; G.resize(len*2); for(int i=0;i<len;i++) G[len+i]=f[i]*coe; len*=2; } G.resize(deg+1); for(int i=0;i<=deg;i++) G[i]*=kake; return G; }//1/Tのdeg次以下を返す vector<mint> logg(vector<mint> F,int deg){ assert(F[0]==1); vector<mint> FF=bibun(F,deg); vector<mint> waru=invv(F,deg); vector<mint> G=atcoder::convolution(FF,waru); G=sekibun(G,deg); return G; } // F0 = 1 vector<mint> expp(vector<mint> F,int deg){ if(si(F)){ assert(F[0]==0); } vector<mint> G(1,1); int len=1; while(len<=deg){ vector<mint> nex=logg(G,len*2-1); for(int i=0;i<si(nex);i++) nex[i]*=(-1); for(int i=0;i<si(nex);i++){ if(i<si(F)) nex[i]+=F[i]; } nex[0]++; nex=atcoder::convolution(nex,G); nex.resize(len*2); len*=2; G=nex; } G.resize(deg+1); return G; } // F0 = 0 vector<mint> poww(vector<mint> F,int deg,ll K){ if(K==0){ vector<mint> res(deg+1); res[0]=1; return res; } if(si(F)==0){ vector<mint> res(deg+1); return res; } ll geta=-1; mint kake=0; for(int i=0;i<si(F);i++){ if(F[i]!=0){ geta=i; kake=F[i].inv(); break; } } if(geta==-1){ vector<mint> res(deg+1); return res; } if(geta>1000000000LL/K){ vector<mint> res(deg+1); return res; } if(geta*K>deg){ vector<mint> res(deg+1); return res; } vector<mint> nF(si(F)-geta); for(int i=geta;i<si(F);i++){ nF[i-geta]=(F[i]*kake); } F=nF; vector<mint> FF=logg(nF,deg-geta*K); for(int i=0;i<si(FF);i++) FF[i]*=K; vector<mint> G=expp(FF,deg-geta*K); kake=kake.inv(); kake=kake.pow(K); vector<mint> res(deg+1); for(int i=0;i<si(G);i++){ res[geta*K+i]=G[i]*kake; } return res; } vector<mint> sqrtt(vector<mint> F,int deg){ assert(F[0]==1); // 本当はmod_sqrt必要そう int len=1; vector<mint> res={1}; mint r2=mint(2).inv(); while(len<=deg){ vector<mint> nex(len+len); for(int i=0;i<len;i++) nex[i]+=res[i]; res=invv(res,len+len); vector<mint> kake(len+len); for(int i=0;i<min(len+len,si(F));i++) kake[i]=F[i]; res=atcoder::convolution(res,kake); for(int i=0;i<min(si(res),len+len);i++) nex[i]+=res[i]; for(int i=0;i<len+len;i++){ nex[i]*=r2; } res=nex; len*=2; } res.resize(deg+1); return res; } mint senkeizenka(vector<mint> A,vector<mint> C,ll K){ if(K<si(A)) return A[K]; int D=si(A); assert(si(A)==si(C)); vector<mint> Q(D+1); Q[0]=1; for(int i=1;i<=D;i++) Q[i]=-C[i-1]; auto P=atcoder::convolution(A,Q); P.resize(D); while(K){ auto Qneg=Q; for(int i=1;i<si(Qneg);i+=2) Qneg[i]=-Qneg[i]; auto x=atcoder::convolution(P,Qneg); auto y=atcoder::convolution(Q,Qneg); P.clear(); Q.clear(); for(int i=(K&1);i<si(x);i+=2) P.push_back(x[i]); for(int i=0;i<si(y);i+=2) Q.push_back(y[i]); K/=2; } return P[0]/Q[0]; } //a[0],...,a[d-1] //c[1],...,c[d] mint senkeizenka2(vector<mint> P,vector<mint> Q,ll K){ while(K){ auto Qneg=Q; for(int i=1;i<si(Qneg);i+=2) Qneg[i]=-Qneg[i]; auto x=atcoder::convolution(P,Qneg); auto y=atcoder::convolution(Q,Qneg); P.clear(); Q.clear(); for(int i=(K&1);i<si(x);i+=2) P.push_back(x[i]); for(int i=0;i<si(y);i+=2) Q.push_back(y[i]); K/=2; } return P[0]/Q[0]; } // P/Q // make() を呼ばないとsekibun呼ぶやつで一部バグる // MAX=2*deg ぐらい必要な気がする pair<vector<mint>,vector<mint>> warizan(vector<mint> P,vector<mint> Q){ if(si(P)<si(Q)) return mp(vector<mint>{},P); auto revP=P;reverse(all(revP)); auto revQ=Q;reverse(all(revQ)); revQ=invv(revQ,si(P)-si(Q)); auto shou=atcoder::convolution(revP,revQ); shou.resize(si(P)-si(Q)+1); reverse(all(shou)); auto hiku=atcoder::convolution(Q,shou); vector<mint> amari(si(P)); for(int i=0;i<si(P);i++){ amari[i]=P[i]-hiku[i]; } while(si(shou)&&shou.back()==0) shou.pop_back(); while(si(amari)&&amari.back()==0) amari.pop_back(); return mp(shou,amari); } // 最高位が0でないようにしている(0のときは空) // 多項式での除算 vector<mint> multieval(vector<mint> P,vector<mint> que){ if(si(que)==0) return {}; int N=si(que),n=1; while(n<N) n*=2; que.resize(n); vector<vector<mint>> Atree(n+n-1),Btree(n+n-1); for(int i=0;i<n;i++) Atree[n-1+i]={-que[i],1}; for(int i=n-2;i>=0;i--){ Atree[i]=atcoder::convolution(Atree[2*i+1],Atree[2*i+2]); } Btree[0]=warizan(P,Atree[0]).se; for(int i=1;i<n+n-1;i++){ Btree[i]=warizan(Btree[(i-1)/2],Atree[i]).se; } vector<mint> res(N,0); for(int i=0;i<N;i++){ if(si(Btree[n-1+i])) res[i]=Btree[n-1+i][0]; } return res; } vector<mint> multieval_touhi(vector<mint> P,mint w,int M){ if(M==0) return {}; int N=si(P); if(N==0) return vector<mint>(M,0); if(w==0){ vector<mint> res(M,P[0]); res[0]=0; for(int i=0;i<N;i++) res[0]+=P[i]; return res; } vector<mint> y(N),v(N+M-1); for(ll i=0;i<N;i++) y[i]=P[i]/w.pow(i*(i-1)/2); for(ll i=0;i<N+M-1;i++) v[i]=w.pow(i*(i-1)/2); reverse(all(y)); auto z=atcoder::convolution(y,v); vector<mint> res(M); for(ll i=0;i<M;i++){ res[i]=z[N-1+i]/w.pow(i*(i-1)/2); } return res; } // w^0,...,w^(M-1)まで答える // 0^0=1 vector<mint> Bernoulli(int N){ vector<mint> F(N+1); for(int i=0;i<=N;i++) F[i]=finv[i+1]; F=invv(F,N); for(int i=0;i<=N;i++){ F[i]*=fac[i]; } return F; } vector<mint> Taylor_Shift(vector<mint> F,ll c){ int N=si(F); vector<mint> A(N),B(N); for(int i=0;i<N;i++){ A[i]=F[N-1-i]*fac[N-1-i]; B[i]=finv[i]*mint(c).pow(i); } vector<mint> p=atcoder::convolution(A,B); for(int i=0;i<N;i++) p[i]*=finv[N-1-i]; vector<mint> res(N); for(int i=0;i<N;i++) res[i]=p[N-1-i]; return res; } vector<mint> manyproduct(vector<vector<mint>> S){ deque<vector<mint>> deq; for(auto a:S) deq.push_back(a); while(si(deq)>1){ auto a=deq.front();deq.pop_front(); auto b=deq.front();deq.pop_front(); deq.push_back(atcoder::convolution(a,b)); } return deq[0]; } vector<mint> PrefixSum(vector<mint> p){ int N=si(p); vector<mint> f(N); for(int i=1;i<N;i++) f[i]=p[i]*fac[i]; vector<mint> Be=Bernoulli(N); if(si(Be)>1) Be[1]=-Be[1]; vector<mint> g(N); for(int j=0;j<N;j++) g[j]=Be[j]*finv[j]; reverse(all(g)); auto h=atcoder::convolution(f,g); vector<mint> res(N+1); for(int i=1;i<=N;i++){ res[i]=h[N-2+i]*finv[i]; } res[0]+=p[0]; res[1]+=p[0]; return res; } vector<mint> BerlekampMassey(vector<mint> s) { int N=si(s); vector<mint> b,c; b.reserve(N+1); c.reserve(N+1); b.pb(1); c.pb(1); mint y=1; for(int ed=1;ed<=N;ed++){ int l=si(c),m=si(b); mint x=0; for(int i=0;i<l;i++){ x+=c[i]*s[ed-l+i]; } b.pb(0); m++; if(x==0) continue; mint freq=x/y; if(l<m){ auto tmp=c; c.insert(begin(c),m-l,0); for(int i=0;i<m;i++) c[m-1-i]-=freq*b[m-1-i]; b=tmp; y=x; }else{ for(int i=0;i<m;i++) c[l-1-i]-=freq*b[m-1-i]; } } reverse(begin(c),end(c)); c.erase(c.begin()); for(int i=0;i<si(c);i++) c[i]*=-1; return c; // https://nyaannyaan.github.io/library/fps/berlekamp-massey.hpp } mint ESPER(vector<mint> S,ll K){ if(K<si(S)) return S[K]; auto X=BerlekampMassey(S); S.resize(si(X)); return senkeizenka(S,X,K); } int main(){ std::ifstream in("text.txt"); std::cin.rdbuf(in.rdbuf()); cin.tie(0); ios::sync_with_stdio(false); make(); ll N,K;cin>>N>>K; vector<mint> mae={1,373341033,45596018,834980587,623627864,428937595,442819817,499710224,833655840,83857087,295201906,788488293,671639287,849315549,597398273,813259672,732727656,244038325,122642896,310517972,160030060,483239722,683879839,712910418,384710263,433880730,844360005,513089677,101492974,959253371,957629942,678615452,34035221,56734233,524027922,31729117,102311167,330331487,8332991,832392662,545208507,594075875,318497156,859275605,300738984,767818091,864118508,878131539,316588744,812496962,213689172,584871249,980836133,54096741,417876813,363266670,335481797,730839588,393495668,435793297,760025067,811438469,720976283,650770098,586537547,117371703,566486504,749562308,708205284,932912293,939830261,983699513,206579820,301188781,593164676,770845925,247687458,41047791,266419267,937835947,506268060,6177705,936268003,166873118,443834893,328979964,470135404,954410105,117565665,832761782,39806322,478922755,394880724,821825588,468705875,512554988,232240472,876497899,356048018,895187265,808258749,575505950,68190615,939065335,552199946,694814243,385460530,529769387,640377761,916128300,440133909,362216114,826373774,502324157,457648395,385510728,904737188,78988746,454565719,623828097,686156489,713476044,63602402,570334625,681055904,222059821,477211096,343363294,833792655,461853093,741797144,74731896,930484262,268372735,941222802,677432735,474842829,700451655,400176109,697644778,390377694,790010794,360642718,505712943,946647976,339045014,715797300,251680896,70091750,40517433,12629586,850635539,110877109,571935891,695965747,634938288,69072133,155093216,749696762,963086402,544711799,724471925,334646013,574791029,722417626,377929821,743946412,988034679,405207112,18063742,104121967,638607426,607304611,751377777,35834555,313632531,18058363,656121134,40763559,562910912,495867250,48767038,210864657,659137294,715390025,865854329,324322857,388911184,286059202,636456178,421290700,832276048,726437551,526417714,252522639,386147469,674313019,274769381,226519400,272047186,117153405,712896591,486826649,119444874,338909703,18536028,41814114,245606459,140617938,250512392,57084755,157807456,261113192,40258068,194807105,325341339,884328111,896332013,880836012,737358206,202713771,785454372,399586250,485457499,640827004,546969497,749602473,159788463,159111724,218592929,675932866,314795475,811539323,246883213,696818315,759880589,4302336,353070689,477909706,559289160,79781699,878094972,840903973,367416824,973366814,848259019,462421750,667227759,897917455,81800722,956276337,942686845,420541799,417005912,272641764,941778993,217214373,192220616,267901132,50530621,652678397,354880856,164289049,781023184,105376215,315094878,607856504,733905911,457743498,992735713,35212756,231822660,276036750,734558079,424180850,433186147,308380947,18333316,12935086,351491725,655645460,535812389,521902115,67016984,48682076,64748124,489360447,361275315,786336279,805161272,468129309,645091350,887284732,913004502,358814684,281295633,328970139,395955130,164840186,820902807,761699708,246274415,592331769,913846362,866682684,600130702,903837674,529462989,90612675,526540127,533047427,110008879,674279751,801920753,645226926,676886948,752481486,474034007,457790341,166813684,287671032,188118664,244731384,404032157,269766986,423996017,182948540,356801634,737863144,652014069,206068022,504569410,919894484,593398649,963768176,882517476,702523597,949028249,128957299,171997372,50865043,20937461,690959202,581356488,369182214,993580422,193500140,540665426,365786018,743731625,144980423,979536721,773259009,617053935,247670131,843705280,30419459,985463402,261585206,237885042,111276893,488166208,137660292,720784236,244467770,26368504,792857103,666885724,670313309,905683034,259415897,512017253,826265493,111960112,633652060,918048438,516432938,386972415,996212724,610073831,444094191,72480267,665038087,11584804,301029012,723617861,113763819,778259899,937766095,535448641,593907889,783573565,673298635,599533244,655712590,173350007,868198597,169013813,585161712,697502214,573994984,285943986,675831407,3134056,965907646,401920943,665949756,236277883,612745912,813282113,892454686,901222267,624900982,927122298,686321335,84924870,927606072,506664166,353631992,165913238,566073550,816674343,864877926,171259407,908752311,874007723,803597299,613676466,880336545,282280109,128761001,58852065,474075900,434816091,364856903,149123648,388854780,314693916,423183826,419733481,888483202,238933227,336564048,757103493,100189123,855479832,51370348,403061033,496971759,831753030,251718753,272779384,683379259,488844621,881783783,659478190,445719559,740782647,546525906,985524427,548033568,333772553,331916427,752533273,730387628,93829695,655989476,930661318,334885743,466041862,428105027,888238707,232218076,769865249,730641039,616996159,231721356,326973501,426068899,722403656,742756734,663270261,364187931,350431704,671823672,633125919,226166717,386814657,237594135,451479365,546182474,119366536,465211069,605313606,728508871,249619035,663053607,900453742,48293872,229958401,62402409,69570431,71921532,960467929,537087913,514588945,513856225,415497414,286592050,645469437,102052166,163298189,873938719,617583886,986843080,962390239,580971332,665147020,88900164,89866970,826426395,616059995,443012312,659160562,229855967,687413213,59809521,398599610,325666688,154765991,159186619,210830877,386454418,84493735,974220646,820097297,2191828,481459931,729073424,551556379,926316039,151357011,808637654,218058015,786112034,850407126,84202800,94214098,30019651,121701603,176055335,865461951,553631971,286620803,984061713,888573766,302767023,977070668,110954576,83922475,51568171,60949367,19533020,510592752,615419476,341370469,912573425,286207526,206707897,384156962,414163604,193301813,749570167,366933789,11470970,600191572,391667731,328736286,30645366,215162519,604947226,236199953,718439098,411423177,803407599,632441623,766760224,263006576,757681534,61082578,681666415,947466395,12206799,659767098,933746852,978860867,59215985,161179205,439197472,259779111,511621808,145770512,882749888,943124465,872053396,631078482,166861622,743415395,772287179,602427948,924112080,385643091,794973480,883782693,869723371,805963889,313106351,262132854,400034567,488248149,265769800,791715397,408753255,468381897,415812467,172922144,64404368,281500398,512318142,288791777,955559118,242484726,536413695,205340854,707803527,576699812,218525078,875554190,46283078,833841915,763148293,807722138,788080170,556901372,150896699,253151120,97856807,918256774,771557187,582547026,472709375,911615063,743371401,641382840,446540967,184639537,157247760,775930891,939702814,499082462,19536133,548753627,593243221,563850263,185475971,687419227,396799323,657976136,864535682,433009242,860830935,33107339,517661450,467651311,812398757,202133852,431839017,709549400,99643620,773282878,290471030,61134552,129206504,929147251,837008968,422332597,353775281,469563025,62265336,835064501,851685235,21197005,264793769,326416680,118842991,84257200,763248924,687559609,150907932,401832452,242726978,766752066,959173604,390269102,992293822,744816299,476631694,177284763,702429415,374065901,169855231,629007616,719169602,564737074,475119050,714502830,40993711,820235888,749063595,239329111,612759169,18591377,419142436,442202439,941600951,158013406,637073231,471564060,447222237,701248503,599797734,577221870,69656699,51052704,6544303,10958310,554955500,943192237,192526269,897983911,961628039,240232720,627280533,710239542,70255649,261743865,228474833,776408079,304180483,63607040,953297493,758058902,395529997,156010331,825833840,539880795,234683685,52626619,751843490,116909119,62806842,574857555,353417551,40061330,822203768,681051568,490913702,9322961,766631257,124794668,37844313,163524507,729108319,490867505,47035168,682765157,53842115,817965276,757179922,339238384,909741023,150530547,158444563,140949492,993302799,551621442,137578883,475122706,443869843,605400098,689361523,769596520,801661499,474900284,586624857,349960501,134084537,650564083,877097974,379857427,887890124,159436401,133274277,986182139,729720334,568925901,459461496,499309445,493171177,460958750,380694152,168836226,840160881,141116880,225064950,109618190,842341383,85305729,759273275,97369807,669317759,766247510,829017039,550323884,261274540,918239352,29606025,870793828,293683814,378510746,367270918,481292028,813097823,798448487,230791733,899305835,504040630,162510533,479367951,275282274,806951470,462774647,56473153,184659008,905122161,664034750,109726629,59372704,325795100,486860143,843736533,924723613,880348000,801252478,616515290,776142608,284803450,583439582,274826676,6018349,377403437,244041569,527081707,544763288,708818585,354033051,904309832,589922898,673933870,682858433,945260111,899893421,515264973,911685911,9527148,239480646,524126897,48259065,578214879,118677219,786127243,869205770,923276513,937928886,802186160,12198440,638784295,34200904,758925811,185027790,80918046,120604699,610456697,573601211,208296321,49743354,653691911,490750754,674335312,887877110,875880304,308360096,414636410,886100267,8525751,636257427,558338775,500159951,696213291,97268896,364983542,937928436,641582714,586211304,345265657,994704486,443549763,207259440,302122082,166055224,623250998,239642551,476337075,283167364,211328914,68064804,950202136,187552679,18938709,646784245,598764068,538505481,610424991,864445053,390248689,278395191,686098470,935957187,868529577,329970687,804930040,84992079,474569269,810762228,573258936,756464212,155080225,286966169,283614605,19283401,24257676,871831819,612689791,846988741,617120754,971716517,979541482,297910784,991087897,783825907,214821357,689498189,405026419,946731704,609346370,707669156,457703127,957341187,980735523,649367684,791011898,82098966,234729712,105002711,130614285,291032164,193188049,363211260,58108651,100756444,954947696,346032213,863300806,36876722,622610957,289232396,667938985,734886266,395881057,417188702,183092975,887586469,83334648,797819763,100176902,781587414,841864935,371674670,18247584,0,0}; auto Z=Bernoulli(K+5); //for(auto x:Z) cout<<x.val()<<endl; mint ans=0; for(ll j=0;j<=K;j++){ ans+=comb(K+1,j)*Z[j]*mint(N).pow(K-j+1)*N*inv[K+1]; } for(ll j=0;j<=K+1;j++){ ans-=comb(K+2,j)*Z[j]*mint(N).pow(K+1-j+1)*inv[K+2]; } ans*=2*(N-1); ans*=mae[(N-2)/1000000]; for(ll s=(N-2)/1000000*1000000+1;s<=N-2;s++) ans*=s; //ans*=2*(N-1)*fac[N-2]; cout<<ans.val()<<endl; }