#include #define MOD_IS_998 1 #if __has_include("template.hpp") #include "template.hpp" #else // template start #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // often use #include #include #include using namespace std; using ll = long long; using pall = pair; template using vec = vector; template using veve = vec>; using vell = vec; using vest = vec; using vebo = basic_string; using vevell = veve; template using mset = multiset; template using priority_queue_ascend = priority_queue, greater>; const ll inf = numeric_limits::max(); const string sp = " "; const string lf = "\n"; const auto &npos = string::npos; #ifdef MOD_IS_998 constexpr ll MOD = 998244353; #else constexpr ll MOD = 1e9 + 7; #endif const vec grid_move4 = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}}; const vec grid_move8 = [] { auto ret = grid_move4; ret.insert(ret.end(), {{-1, 1}, {-1, -1}, {1, -1}, {1, 1}}); return ret; }(); #define cont continue #define br break #define whlie while #define foR for #define auot auto #define uato auto #define cosnt const #define conts const #define ocnst const auto &ciN = cin; auto &icn = cin; auto &icN = cin; constexpr bool ture = true; constexpr bool flase = false; using namespace atcoder; using mint = static_modint; #define times(N) \ static_assert(is_integral_v, \ "times(): N must be integral"); \ for(typedef decltype((N) + 0) _int; \ [[maybe_unused]] const _int _i : views::iota((_int)0, (N))) #define tiems times #define itmes times struct debug_stream { template debug_stream &operator<<([[maybe_unused]] const T &x) { #ifndef ONLINE_JUDGE clog << x; #endif return *this; } debug_stream &operator<<([[maybe_unused]] ostream &(*f)(ostream &)) { #ifndef ONLINE_JUDGE clog << f; #endif return *this; } }; template concept out_stream = same_as || same_as; debug_stream clog_; #define clog clog_ template istream &operator>>(istream &in, static_modint &i) { intmax_t tmp; in >> tmp; i = tmp; return in; } template decltype(auto) operator<<(out_stream auto &out, const static_modint &i) { return out << i.val(); } template istream &operator>>(istream &in, pair &p) { return in >> p.first >> p.second; } template decltype(auto) operator<<(out_stream auto &out, const pair &p) { return out << p.first << sp << p.second; } template istream &operator>>(istream &in, vec &v) { for(auto &&e : v) { in >> e; } return in; } namespace myinput { template istream &in(Ts &...vecs) { static_assert(sizeof...(vecs) != 0, "myfunc::in(): At least one vector must be provided"); const set sizes = {vecs.size()...}; if(sizes.size() > 1) { throw invalid_argument("myfunc::in(): All vectors must have the same size"); } times(*sizes.begin()) { ((cin >> vecs[_i]), ...); } return cin; } } // namespace myinput using myinput::in; void out(ranges::range auto v, const string delim, out_stream auto &out) { for(auto &&e : v) { out << e << delim; } } void out(ranges::range auto v, const string delim) { out(v, delim, cout); } [[nodiscard]] const string &yesno(const bool cond, const string &yes = "Yes", const string &no = "No") noexcept { if(cond) return yes; return no; } // [mi, ma) [[nodiscard]] uint64_t randint(const uint64_t mi, const uint64_t ma) noexcept { static random_device seed; static mt19937_64 mt(seed()); if(mi > ma) [[unlikely]] return randint(ma, mi); if(mi == ma) [[unlikely]] return mi; const uint64_t w = ma - mi; uint64_t r; do { r = mt(); } while(mt.max() - mt.max() % w <= r); return r % w + mi; } template requires common_with [[nodiscard]] constexpr auto min(T &&a, U &&b) noexcept { return std::min>(std::forward(a), std::forward(b)); } template requires common_with [[nodiscard]] constexpr auto max(T &&a, U &&b) noexcept { return std::max>(std::forward(a), std::forward(b)); } template [[nodiscard]] const T &min(const vec &v) { return *ranges::min_element(v); } template [[nodiscard]] const T &max(const vec &v) { return *ranges::max_element(v); } template [[nodiscard]] auto reduce(const ranges::range auto &v, Args... args) { return reduce(v.begin(), v.end(), args...); } [[nodiscard]] constexpr ll powll(ll a, ll b, const ll m = inf) { if(b < 0) [[unlikely]] throw invalid_argument("powll(): exponent less than zero"); if(m < 1) [[unlikely]] throw invalid_argument("powll(): modulo less than one"); a %= m; ll ret = 1; while(b) { if(b % 2) ret *= a, ret %= m; a *= a, a %= m; b /= 2; } return ret; } template requires assignable_from && totally_ordered_with bool mini(T &var, U &&val) noexcept { const bool cmp = var > val; if(cmp) var = val; return cmp; } template requires assignable_from && totally_ordered_with bool maxi(T &var, U &&val) noexcept { const bool cmp = var < val; if(cmp) var = val; return cmp; } namespace myclass { class [[nodiscard]] grid_base { public: grid_base(const ll h, const ll w) noexcept : height(h), width(w) {} [[nodiscard]] ll operator()(const ll i, const ll j) const noexcept { if(! isvalid(i, j)) return -1; return i * width + j; } [[nodiscard]] ll operator()(const pall &p) const noexcept { return (*this)(p.first, p.second); } protected: bool isvalid(const ll i, const ll j) const noexcept { return 0 <= i && 0 <= j && i < height && j < width; } const ll height, width; }; class [[nodiscard]] grid_seen : public myclass::grid_base { public: grid_seen(const ll h, const ll w) : grid_base(h, w) { visited = vebo(h * w, false); } [[nodiscard]] bool &seen(const ll i, const ll j) & { if(! isvalid(i, j)) [[unlikely]] throw out_of_range("grid::seen(): out of range"); return visited[i * width + j]; } [[nodiscard]] bool &seen(const pall &p) & { return seen(p.first, p.second); } private: vebo visited; }; } // namespace myclass using grid_lite = myclass::grid_base; using myclass::grid_seen; template auto erase_single(mset &mset, T &&v) { const auto it = mset.find(v); if(it == mset.end()) [[unlikely]] throw invalid_argument("erase_single(): why v not in mset!?!?"); return mset.erase(it); } void solve(); // template end #endif int main(void) { cin.tie(nullptr); ios::sync_with_stdio(false); ll t = 1; // cin >> t; times(t) solve(); return 0; } using ararll = array, 2>; ll k, n; #include ararll matmul(const ararll &m, const ararll &n) { ararll a{}; for(intmax_t i = 0; i < intmax_t(2); i++) { for(intmax_t j = 0; j < intmax_t(2); j++) { for(intmax_t l = 0; l < intmax_t(2); l++) { a[i][j] += m[i][l] * n[l][j]; a[i][j] %= k; } } } return a; } void nrml(ll &x) { x = (x % k + k) % k; } void solve() { cin >> k >> n; segtree t(n); for(intmax_t i = 0; i < intmax_t(n); i++) { ararll a; for(intmax_t j = 0; j < intmax_t(2); j++) { for(intmax_t k = 0; k < intmax_t(2); k++) { cin >> a[j][k]; nrml(a[j][k]); } } t.set(i, a); } ll q; cin >> q; times(q) { ll i; cin >> i; i--; ll l, r; cin >> l >> r; l--; ararll y{}; for(intmax_t i = 0; i < intmax_t(2); i++) { for(intmax_t j = 0; j < intmax_t(2); j++) { cin >> y[i][j]; nrml(y[i][j]); } } t.set(i, y); const auto ans = t.prod(l, r); out(ans[0], sp), cout << lf, out(ans[1], sp), cout << lf; } }