結果
問題 | No.2529 Treasure Hunter |
ユーザー | suisen |
提出日時 | 2023-11-03 22:24:09 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 7 ms / 2,000 ms |
コード長 | 25,696 bytes |
コンパイル時間 | 2,209 ms |
コンパイル使用メモリ | 213,784 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-25 20:47:18 |
合計ジャッジ時間 | 2,820 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 7 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 2 ms
5,376 KB |
testcase_12 | AC | 1 ms
5,376 KB |
testcase_13 | AC | 2 ms
5,376 KB |
testcase_14 | AC | 2 ms
5,376 KB |
testcase_15 | AC | 2 ms
5,376 KB |
testcase_16 | AC | 1 ms
5,376 KB |
testcase_17 | AC | 1 ms
5,376 KB |
testcase_18 | AC | 1 ms
5,376 KB |
testcase_19 | AC | 2 ms
5,376 KB |
testcase_20 | AC | 2 ms
5,376 KB |
testcase_21 | AC | 2 ms
5,376 KB |
testcase_22 | AC | 2 ms
5,376 KB |
ソースコード
#include <bits/stdc++.h> namespace suisen { template <class T> bool chmin(T& x, const T& y) { return y >= x ? false : (x = y, true); } template <class T> bool chmax(T& x, const T& y) { return y <= x ? false : (x = y, true); } template <class T> constexpr int pow_m1(T n) { return -(n & 1) | 1; } template <class T> constexpr T fld(const T x, const T y) { T q = x / y, r = x % y; return q - ((x ^ y) < 0 and (r != 0)); } template <class T> constexpr T cld(const T x, const T y) { T q = x / y, r = x % y; return q + ((x ^ y) > 0 and (r != 0)); } } namespace suisen::macro { #define IMPL_REPITER(cond) auto& begin() { return *this; } auto end() { return nullptr; } auto& operator*() { return _val; } auto& operator++() { return _val += _step, *this; } bool operator!=(std::nullptr_t) { return cond; } template <class Int, class IntL = Int, class IntStep = Int, std::enable_if_t<(std::is_signed_v<Int> == std::is_signed_v<IntL>), std::nullptr_t> = nullptr> struct rep_impl { Int _val; const Int _end, _step; rep_impl(Int n) : rep_impl(0, n) {} rep_impl(IntL l, Int r, IntStep step = 1) : _val(l), _end(r), _step(step) {} IMPL_REPITER((_val < _end)) }; template <class Int, class IntL = Int, class IntStep = Int, std::enable_if_t<(std::is_signed_v<Int> == std::is_signed_v<IntL>), std::nullptr_t> = nullptr> struct rrep_impl { Int _val; const Int _end, _step; rrep_impl(Int n) : rrep_impl(0, n) {} rrep_impl(IntL l, Int r) : _val(r - 1), _end(l), _step(-1) {} rrep_impl(IntL l, Int r, IntStep step) : _val(l + fld<Int>(r - l - 1, step) * step), _end(l), _step(-step) {} IMPL_REPITER((_val >= _end)) }; template <class Int, class IntStep = Int> struct repinf_impl { Int _val; const Int _step; repinf_impl(Int l, IntStep step = 1) : _val(l), _step(step) {} IMPL_REPITER((true)) }; #undef IMPL_REPITER } #include <iostream> #include <limits> #include <type_traits> namespace suisen { template <typename ...Constraints> using constraints_t = std::enable_if_t<std::conjunction_v<Constraints...>, std::nullptr_t>; template <typename T, typename = std::nullptr_t> struct bitnum { static constexpr int value = 0; }; template <typename T> struct bitnum<T, constraints_t<std::is_integral<T>>> { static constexpr int value = std::numeric_limits<std::make_unsigned_t<T>>::digits; }; template <typename T> static constexpr int bitnum_v = bitnum<T>::value; template <typename T, size_t n> struct is_nbit { static constexpr bool value = bitnum_v<T> == n; }; template <typename T, size_t n> static constexpr bool is_nbit_v = is_nbit<T, n>::value; template <typename T, typename = std::nullptr_t> struct safely_multipliable { using type = T; }; template <typename T> struct safely_multipliable<T, constraints_t<std::is_signed<T>, is_nbit<T, 32>>> { using type = long long; }; template <typename T> struct safely_multipliable<T, constraints_t<std::is_signed<T>, is_nbit<T, 64>>> { using type = __int128_t; }; template <typename T> struct safely_multipliable<T, constraints_t<std::is_unsigned<T>, is_nbit<T, 32>>> { using type = unsigned long long; }; template <typename T> struct safely_multipliable<T, constraints_t<std::is_unsigned<T>, is_nbit<T, 64>>> { using type = __uint128_t; }; template <typename T> using safely_multipliable_t = typename safely_multipliable<T>::type; template <typename T, typename = void> struct rec_value_type { using type = T; }; template <typename T> struct rec_value_type<T, std::void_t<typename T::value_type>> { using type = typename rec_value_type<typename T::value_type>::type; }; template <typename T> using rec_value_type_t = typename rec_value_type<T>::type; template <typename T> class is_iterable { template <typename T_> static auto test(T_ e) -> decltype(e.begin(), e.end(), std::true_type{}); static std::false_type test(...); public: static constexpr bool value = decltype(test(std::declval<T>()))::value; }; template <typename T> static constexpr bool is_iterable_v = is_iterable<T>::value; template <typename T> class is_writable { template <typename T_> static auto test(T_ e) -> decltype(std::declval<std::ostream&>() << e, std::true_type{}); static std::false_type test(...); public: static constexpr bool value = decltype(test(std::declval<T>()))::value; }; template <typename T> static constexpr bool is_writable_v = is_writable<T>::value; template <typename T> class is_readable { template <typename T_> static auto test(T_ e) -> decltype(std::declval<std::istream&>() >> e, std::true_type{}); static std::false_type test(...); public: static constexpr bool value = decltype(test(std::declval<T>()))::value; }; template <typename T> static constexpr bool is_readable_v = is_readable<T>::value; } // namespace suisen namespace suisen::io { template <typename IStream, std::enable_if_t<std::conjunction_v<std::is_base_of<std::istream, std::remove_reference_t<IStream>>, std::negation<std::is_const<std::remove_reference_t<IStream>>>>, std::nullptr_t> = nullptr> struct InputStream { private: using istream_type = std::remove_reference_t<IStream>; IStream is; struct { InputStream* is; template <typename T> operator T() { T e; *is >> e; return e; } } _reader{ this }; public: template <typename IStream_> InputStream(IStream_ &&is) : is(std::move(is)) {} template <typename IStream_> InputStream(IStream_ &is) : is(is) {} template <typename T> InputStream& operator>>(T& e) { if constexpr (suisen::is_readable_v<T>) is >> e; else _read(e); return *this; } auto read() { return _reader; } template <typename Head, typename... Tail> void read(Head& head, Tail &...tails) { ((*this >> head) >> ... >> tails); } istream_type& get_stream() { return is; } private: static __uint128_t _stou128(const std::string& s) { __uint128_t ret = 0; for (char c : s) if ('0' <= c and c <= '9') ret = 10 * ret + c - '0'; return ret; } static __int128_t _stoi128(const std::string& s) { return (s[0] == '-' ? -1 : +1) * _stou128(s); } void _read(__uint128_t& v) { v = _stou128(std::string(_reader)); } void _read(__int128_t& v) { v = _stoi128(std::string(_reader)); } template <typename T, typename U> void _read(std::pair<T, U>& a) { *this >> a.first >> a.second; } template <size_t N = 0, typename ...Args> void _read(std::tuple<Args...>& a) { if constexpr (N < sizeof...(Args)) *this >> std::get<N>(a), _read<N + 1>(a); } template <typename Iterable, std::enable_if_t<suisen::is_iterable_v<Iterable>, std::nullptr_t> = nullptr> void _read(Iterable& a) { for (auto& e : a) *this >> e; } }; template <typename IStream> InputStream(IStream &&) -> InputStream<IStream>; template <typename IStream> InputStream(IStream &) -> InputStream<IStream&>; InputStream cin{ std::cin }; auto read() { return cin.read(); } template <typename Head, typename... Tail> void read(Head& head, Tail &...tails) { cin.read(head, tails...); } } // namespace suisen::io namespace suisen { using io::read; } // namespace suisen namespace suisen::io { template <typename OStream, std::enable_if_t<std::conjunction_v<std::is_base_of<std::ostream, std::remove_reference_t<OStream>>, std::negation<std::is_const<std::remove_reference_t<OStream>>>>, std::nullptr_t> = nullptr> struct OutputStream { private: using ostream_type = std::remove_reference_t<OStream>; OStream os; public: template <typename OStream_> OutputStream(OStream_ &&os) : os(std::move(os)) {} template <typename OStream_> OutputStream(OStream_ &os) : os(os) {} template <typename T> OutputStream& operator<<(const T& e) { if constexpr (suisen::is_writable_v<T>) os << e; else _print(e); return *this; } void print() { *this << '\n'; } template <typename Head, typename... Tail> void print(const Head& head, const Tail &...tails) { *this << head, ((*this << ' ' << tails), ...), *this << '\n'; } template <typename Iterable, std::enable_if_t<suisen::is_iterable_v<Iterable>, std::nullptr_t> = nullptr> void print_all(const Iterable& v, std::string sep = " ", std::string end = "\n") { for (auto it = v.begin(); it != v.end();) if (*this << *it; ++it != v.end()) *this << sep; *this << end; } ostream_type& get_stream() { return os; } private: void _print(__uint128_t value) { char buffer[41], *d = std::end(buffer); do *--d = '0' + (value % 10), value /= 10; while (value); os.rdbuf()->sputn(d, std::end(buffer) - d); } void _print(__int128_t value) { if (value < 0) *this << '-'; _print(__uint128_t(value < 0 ? -value : value)); } template <typename T, typename U> void _print(const std::pair<T, U>& a) { *this << a.first << ' ' << a.second; } template <size_t N = 0, typename ...Args> void _print(const std::tuple<Args...>& a) { if constexpr (N < std::tuple_size_v<std::tuple<Args...>>) { if constexpr (N) *this << ' '; *this << std::get<N>(a), _print<N + 1>(a); } } template <typename Iterable, std::enable_if_t<suisen::is_iterable_v<Iterable>, std::nullptr_t> = nullptr> void _print(const Iterable& a) { print_all(a, " ", ""); } }; template <typename OStream_> OutputStream(OStream_ &&) -> OutputStream<OStream_>; template <typename OStream_> OutputStream(OStream_ &) -> OutputStream<OStream_&>; OutputStream cout{ std::cout }, cerr{ std::cerr }; template <typename... Args> void print(const Args &... args) { cout.print(args...); } template <typename Iterable, std::enable_if_t<suisen::is_iterable_v<Iterable>, std::nullptr_t> = nullptr> void print_all(const Iterable& v, const std::string& sep = " ", const std::string& end = "\n") { cout.print_all(v, sep, end); } } // namespace suisen::io namespace suisen { using io::print, io::print_all; } // namespace suisen namespace suisen { template <class T, class ToKey, class CompKey = std::less<>, std::enable_if_t<std::conjunction_v<std::is_invocable<ToKey, T>, std::is_invocable_r<bool, CompKey, std::invoke_result_t<ToKey, T>, std::invoke_result_t<ToKey, T>>>, std::nullptr_t> = nullptr> auto comparator(const ToKey& to_key, const CompKey& comp_key = std::less<>()) { return [=](const T& x, const T& y) { return comp_key(to_key(x), to_key(y)); }; } template <class Compare, std::enable_if_t<std::is_invocable_r_v<bool, Compare, int, int>, std::nullptr_t> = nullptr> std::vector<int> sorted_indices(int n, const Compare& compare) { std::vector<int> p(n); return std::iota(p.begin(), p.end(), 0), std::sort(p.begin(), p.end(), compare), p; } template <class ToKey, std::enable_if_t<std::is_invocable_v<ToKey, int>, std::nullptr_t> = nullptr> std::vector<int> sorted_indices(int n, const ToKey& to_key) { return sorted_indices(n, comparator<int>(to_key)); } template <class T, class Comparator> auto priority_queue_with_comparator(const Comparator& comparator) { return std::priority_queue<T, std::vector<T>, Comparator>{ comparator }; } template <class Iterable, std::enable_if_t<suisen::is_iterable_v<Iterable>, std::nullptr_t> = nullptr> void sort_unique_erase(Iterable& a) { std::sort(a.begin(), a.end()), a.erase(std::unique(a.begin(), a.end()), a.end()); } template <size_t D> struct Dim : std::array<int, D> { template <typename ...Ints> Dim(const Ints& ...ns) : std::array<int, D>::array{ static_cast<int>(ns)... } {} }; template <typename ...Ints> Dim(const Ints& ...) -> Dim<sizeof...(Ints)>; template <class T, size_t D, size_t I = 0> auto ndvec(const Dim<D> &ns, const T& value = {}) { if constexpr (I + 1 < D) { return std::vector(ns[I], ndvec<T, D, I + 1>(ns, value)); } else { return std::vector<T>(ns[I], value); } } } namespace suisen { using int128 = __int128_t; using uint128 = __uint128_t; template <class T> using min_priority_queue = std::priority_queue<T, std::vector<T>, std::greater<T>>; template <class T> using max_priority_queue = std::priority_queue<T, std::vector<T>, std::less<T>>; } namespace suisen { const std::string Yes = "Yes", No = "No", YES = "YES", NO = "NO"; } #ifdef LOCAL # define debug(...) debug_impl(#__VA_ARGS__, __VA_ARGS__) template <class H, class... Ts> void debug_impl(const char* s, const H& h, const Ts&... t) { suisen::io::cerr << "[\033[32mDEBUG\033[m] " << s << ": " << h, ((suisen::io::cerr << ", " << t), ..., (suisen::io::cerr << "\n")); } #else # define debug(...) void(0) #endif #define FOR(e, v) for (auto &&e : v) #define CFOR(e, v) for (const auto &e : v) #define REP(i, ...) CFOR(i, suisen::macro::rep_impl(__VA_ARGS__)) #define RREP(i, ...) CFOR(i, suisen::macro::rrep_impl(__VA_ARGS__)) #define REPINF(i, ...) CFOR(i, suisen::macro::repinf_impl(__VA_ARGS__)) #define LOOP(n) for ([[maybe_unused]] const auto& _ : suisen::macro::rep_impl(n)) #define ALL(iterable) std::begin(iterable), std::end(iterable) using namespace suisen; using namespace std; struct io_setup { io_setup(int precision = 20) { std::ios::sync_with_stdio(false), std::cin.tie(nullptr); std::cout << std::fixed << std::setprecision(precision); } } io_setup_{}; constexpr int iinf = std::numeric_limits<int>::max() / 2; constexpr long long linf = std::numeric_limits<long long>::max() / 2; #include <atcoder/modint> using mint = atcoder::modint998244353; namespace atcoder { std::istream& operator>>(std::istream& in, mint& a) { long long e; in >> e; a = e; return in; } std::ostream& operator<<(std::ostream& out, const mint& a) { out << a.val(); return out; } } // namespace atcoder #include <array> #include <cassert> #include <optional> namespace suisen { namespace default_operator { template <typename T> auto zero() -> decltype(T { 0 }) { return T { 0 }; } template <typename T> auto one() -> decltype(T { 1 }) { return T { 1 }; } template <typename T> auto add(const T &x, const T &y) -> decltype(x + y) { return x + y; } template <typename T> auto sub(const T &x, const T &y) -> decltype(x - y) { return x - y; } template <typename T> auto mul(const T &x, const T &y) -> decltype(x * y) { return x * y; } template <typename T> auto div(const T &x, const T &y) -> decltype(x / y) { return x / y; } template <typename T> auto mod(const T &x, const T &y) -> decltype(x % y) { return x % y; } template <typename T> auto neg(const T &x) -> decltype(-x) { return -x; } template <typename T> auto inv(const T &x) -> decltype(one<T>() / x) { return one<T>() / x; } } // default_operator namespace default_operator_noref { template <typename T> auto zero() -> decltype(T { 0 }) { return T { 0 }; } template <typename T> auto one() -> decltype(T { 1 }) { return T { 1 }; } template <typename T> auto add(T x, T y) -> decltype(x + y) { return x + y; } template <typename T> auto sub(T x, T y) -> decltype(x - y) { return x - y; } template <typename T> auto mul(T x, T y) -> decltype(x * y) { return x * y; } template <typename T> auto div(T x, T y) -> decltype(x / y) { return x / y; } template <typename T> auto mod(T x, T y) -> decltype(x % y) { return x % y; } template <typename T> auto neg(T x) -> decltype(-x) { return -x; } template <typename T> auto inv(T x) -> decltype(one<T>() / x) { return one<T>() / x; } } // default_operator } // namespace suisen namespace suisen { template < typename T, size_t N, size_t M, T(*_add)(T, T) = default_operator_noref::add<T>, T(*_neg)(T) = default_operator_noref::neg<T>, T(*_zero)() = default_operator_noref::zero<T>, T(*_mul)(T, T) = default_operator_noref::mul<T>, T(*_inv)(T) = default_operator_noref::inv<T>, T(*_one)() = default_operator_noref::one<T> > struct ArrayMatrix : public std::array<std::array<T, M>, N> { private: template <typename DummyType = void> static constexpr bool is_square_v = N == M; template <size_t X, size_t Y> using MatrixType = ArrayMatrix<T, X, Y, _add, _neg, _zero, _mul, _inv, _one>; public: using base_type = std::array<std::array<T, M>, N>; using container_type = base_type; using row_type = std::array<T, M>; using base_type::base_type; ArrayMatrix(T diag_val = _zero()) { for (size_t i = 0; i < N; ++i) for (size_t j = 0; j < M; ++j) { (*this)[i][j] = (i == j ? diag_val : _zero()); } } ArrayMatrix(const container_type& c) : base_type{ c } {} ArrayMatrix(const std::initializer_list<row_type>& c) { assert(c.size() == N); size_t i = 0; for (const auto& row : c) { for (size_t j = 0; j < M; ++j) (*this)[i][j] = row[j]; ++i; } } static ArrayMatrix e0() { return ArrayMatrix(_zero()); } static MatrixType<M, M> e1() { return MatrixType<M, M>(_one()); } int size() const { static_assert(is_square_v<>); return N; } std::pair<int, int> shape() const { return { N, M }; } int row_size() const { return N; } int col_size() const { return M; } ArrayMatrix operator+() const { return *this; } ArrayMatrix operator-() const { ArrayMatrix A; for (size_t i = 0; i < N; ++i) for (size_t j = 0; j < M; ++j) A[i][j] = _neg((*this)[i][j]); return A; } friend ArrayMatrix& operator+=(ArrayMatrix& A, const ArrayMatrix& B) { for (size_t i = 0; i < N; ++i) for (size_t j = 0; j < M; ++j) A[i][j] = _add(A[i][j], B[i][j]); return A; } friend ArrayMatrix& operator-=(ArrayMatrix& A, const ArrayMatrix& B) { for (size_t i = 0; i < N; ++i) for (size_t j = 0; j < M; ++j) A[i][j] = _add(A[i][j], _neg(B[i][j])); return A; } template <size_t K> friend MatrixType<N, K>& operator*=(ArrayMatrix& A, const MatrixType<M, K>& B) { return A = A * B; } friend ArrayMatrix& operator*=(ArrayMatrix& A, const T& val) { for (size_t i = 0; i < N; ++i) for (size_t j = 0; j < M; ++j) A[i][j] = _mul(A[i][j], val); return A; } friend ArrayMatrix& operator/=(ArrayMatrix& A, const ArrayMatrix& B) { static_assert(is_square_v<>); return A *= *B.inv(); } friend ArrayMatrix& operator/=(ArrayMatrix& A, const T& val) { return A *= _inv(val); } friend ArrayMatrix operator+(ArrayMatrix A, const ArrayMatrix& B) { A += B; return A; } friend ArrayMatrix operator-(ArrayMatrix A, const ArrayMatrix& B) { A -= B; return A; } template <size_t K> friend MatrixType<N, K> operator*(const ArrayMatrix& A, const MatrixType<M, K>& B) { MatrixType<N, K> C; for (size_t i = 0; i < N; ++i) { C[i].fill(_zero()); for (size_t j = 0; j < M; ++j) for (size_t k = 0; k < K; ++k) C[i][k] = _add(C[i][k], _mul(A[i][j], B[j][k])); } return C; } friend ArrayMatrix operator*(ArrayMatrix A, const T& val) { A *= val; return A; } friend ArrayMatrix operator*(const T& val, ArrayMatrix A) { for (size_t i = 0; i < N; ++i) for (size_t j = 0; j < M; ++j) A[i][j] = _mul(val, A[i][j]); return A; } friend std::array<T, N> operator*(const ArrayMatrix& A, const std::array<T, M>& x) { std::array<T, N> b; b.fill(_zero()); for (size_t i = 0; i < N; ++i) for (size_t j = 0; j < M; ++j) b[i] = _add(b[i], _mul(A[i][j], x[j])); return b; } friend ArrayMatrix operator/(ArrayMatrix A, const ArrayMatrix& B) { static_assert(is_square_v<>); return A * B.inv(); } friend ArrayMatrix operator/(ArrayMatrix A, const T& val) { A /= val; return A; } friend ArrayMatrix operator/(const T& val, ArrayMatrix A) { return A.inv() *= val; } ArrayMatrix pow(long long b) const { static_assert(is_square_v<>); assert(b >= 0); ArrayMatrix res(e1()), p(*this); for (; b; b >>= 1) { if (b & 1) res *= p; p *= p; } return res; } std::optional<ArrayMatrix> safe_inv() const { static_assert(is_square_v<>); std::array<std::array<T, 2 * N>, N> data; for (size_t i = 0; i < N; ++i) { for (size_t j = 0; j < N; ++j) { data[i][j] = (*this)[i][j]; data[i][N + j] = i == j ? _one() : _zero(); } } for (size_t i = 0; i < N; ++i) { for (size_t k = i; k < N; ++k) if (data[k][i] != _zero()) { data[i].swap(data[k]); T c = _inv(data[i][i]); for (size_t j = i; j < 2 * N; ++j) data[i][j] = _mul(c, data[i][j]); break; } if (data[i][i] == _zero()) return std::nullopt; for (size_t k = 0; k < N; ++k) if (k != i and data[k][i] != _zero()) { T c = data[k][i]; for (size_t j = i; j < 2 * N; ++j) data[k][j] = _add(data[k][j], _neg(_mul(c, data[i][j]))); } } ArrayMatrix res; for (size_t i = 0; i < N; ++i) std::copy(data[i].begin() + N, data[i].begin() + 2 * N, res[i].begin()); return res; } ArrayMatrix inv() const { return *safe_inv(); } T det() const { static_assert(is_square_v<>); ArrayMatrix A = *this; bool sgn = false; for (size_t j = 0; j < N; ++j) for (size_t i = j + 1; i < N; ++i) if (A[i][j] != _zero()) { std::swap(A[j], A[i]); T q = _mul(A[i][j], _inv(A[j][j])); for (size_t k = j; k < N; ++k) A[i][k] = _add(A[i][k], _neg(_mul(A[j][k], q))); sgn = not sgn; } T res = sgn ? _neg(_one()) : _one(); for (size_t i = 0; i < N; ++i) res = _mul(res, A[i][i]); return res; } T det_arbitrary_mod() const { static_assert(is_square_v<>); ArrayMatrix A = *this; bool sgn = false; for (size_t j = 0; j < N; ++j) for (size_t i = j + 1; i < N; ++i) { for (; A[i][j].val(); sgn = not sgn) { std::swap(A[j], A[i]); T q = A[i][j].val() / A[j][j].val(); for (size_t k = j; k < N; ++k) A[i][k] -= A[j][k] * q; } } T res = sgn ? -1 : +1; for (size_t i = 0; i < N; ++i) res *= A[i][i]; return res; } }; template < typename T, size_t N, T(*_add)(T, T) = default_operator_noref::add<T>, T(*_neg)(T) = default_operator_noref::neg<T>, T(*_zero)() = default_operator_noref::zero<T>, T(*_mul)(T, T) = default_operator_noref::mul<T>, T(*_inv)(T) = default_operator_noref::inv<T>, T(*_one)() = default_operator_noref::one<T> > using SquareArrayMatrix = ArrayMatrix<T, N, N, _add, _neg, _zero, _mul, _inv, _one>; } // namespace suisen void solve() { long long n, m; read(m, n); if (m % 2 == 0) { array<mint, 4> pd{}; pd[0] = 1; mint k = m / 2 - 1; ArrayMatrix<mint, 4, 4> T; if (m >= 4) { T = {{ 1, 1, 1, 1, m, m - 1, m - 2, m - 2, m * (m - 1) / 2 - m - m / 2, (m - 1) * (m - 2) / 2 - (m - 2) - (m / 2 - 1), (k - 1) * (m - 4) + 1, (k - 1) * (m - 4), m / 2, m / 2 - 1, m / 2 - 2, m / 2 - 1 }}; } else { T = {{ 1, 1, 1, 1, m, m - 1, m - 2, m - 2, 0, 0, 0, 0, 0, 0, 0, 0 }}; } array y = T.pow(n) * array<mint, 4>{ 1, 0, 0, 0 }; print(accumulate(ALL(y), mint(0))); } else { array<mint, 3> pd{}; pd[0] = 1; mint k = m / 2 - 1; ArrayMatrix<mint, 3, 3> T; if (m >= 4) { T = {{ 1, 1, 1, m, m - 1, m - 2, m * (m - 1) / 2 - m, (m - 1) * (m - 2) / 2 - (m - 2), k * (m - 4) + 1 }}; } else { T = {{ 1, 1, 1, m, m - 1, m - 2, 0, 0, 0 }}; } array y = T.pow(n) * array<mint, 3>{ 1, 0, 0 }; print(accumulate(ALL(y), mint(0))); } } int main() { int test_case_num = 1; read(test_case_num); LOOP(test_case_num) solve(); return 0; }