#if __INCLUDE_LEVEL__ == 0 #include __BASE_FILE__ Array2D Solve(int h, int w) { if (h == 1 && w == 1) return Array2D(h, w); int p = 2; while (h % p && w % p) ++p; int Bh = h; int Ch = 1; while (Bh % p == 0) { Bh /= p; Ch *= p; } int Bw = w; int Cw = 1; while (Bw % p == 0) { Bw /= p; Cw *= p; } Array2D rem(Ch, Cw); for (int i : Rep(0, Ch)) for (int j : Rep(0, Cw)) rem[i][j] = i * Cw + j; { vector> order_h(Ch); for (int i : Rep(0, Ch)) { if (i == 0) { order_h[i].first = INF; } else { int t = i; while (t % p == 0) { t /= p; ++order_h[i].first; } } order_h[i].second = i; } ranges::sort(order_h, greater{}); vector> order_w(Cw); for (int i : Rep(0, Cw)) { if (i == 0) { order_w[i].first = INF; } else { int t = i; while (t % p == 0) { t /= p; ++order_w[i].first; } } order_w[i].second = i; } ranges::sort(order_w, greater{}); Array2D new_rem(Ch, Cw); for (int i : Rep(0, Ch)) for (int j : Rep(0, Cw)) new_rem[i][j] = rem[order_h[i].second][order_w[j].second]; rem = move(new_rem); } auto tmp = Solve(Bh, Bw); Array2D ret(h, w); for (int i : Rep(0, h)) for (int j : Rep(0, w)) { int v = tmp[i / Bh & 1 ? Bh - i % Bh - 1 : i % Bh][j / Bw & 1 ? Bw - j % Bw - 1 : j % Bw]; ret[i][j] = int(atcoder::crt({rem[i / Bh][j / Bw], v}, {Ch * Cw, Bh * Bw}).first); } return ret; } void Solve() { int h, w; IN(h, w); auto ans = Solve(h, w); for (int i : Rep(0, h)) { ranges::replace(ans[i], 0, h * w); OUT(ans[i]); } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); Solve(); } #elif __INCLUDE_LEVEL__ == 1 #include #include template class Array2D { public: Array2D() : Array2D(0, 0) {} Array2D(int h, int w, T v = {}) : h_(h), w_(w), data_(h * w, std::move(v)) {} auto operator[](int i) { return std::span(data_.data() + i * w_, w_); } auto operator[](int i) const { return std::span(data_.data() + i * w_, w_); } private: int h_; int w_; std::vector data_; }; template concept MyRange = std::ranges::range && !std::convertible_to && !std::convertible_to; template concept MyTuple = std::__is_tuple_like::value && !MyRange; namespace std { istream& operator>>(istream& is, MyRange auto&& r) { for (auto&& e : r) { is >> e; } return is; } istream& operator>>(istream& is, MyTuple auto&& t) { apply([&](auto&... xs) { (is >> ... >> xs); }, t); return is; } ostream& operator<<(ostream& os, MyRange auto&& r) { auto sep = ""; for (auto&& e : r) { os << exchange(sep, " ") << forward(e); } return os; } ostream& operator<<(ostream& os, MyTuple auto&& t) { auto sep = ""; apply([&](auto&... xs) { ((os << exchange(sep, " ") << xs), ...); }, t); return os; } } // namespace std template class OneBased { public: explicit OneBased(T&& x) : ref_(std::forward(x)) {} template requires(sizeof...(Ts) > 1) OneBased(Ts&&... xs) : ref_(std::forward_as_tuple(std::forward(xs)...)) {} friend std::istream& operator>>(std::istream& is, OneBased x) { if constexpr (MyRange) { for (auto&& e : x.ref_) { is >> ::OneBased(e); } } else if constexpr (MyTuple) { std::apply([&](auto&... xs) { (is >> ... >> ::OneBased(xs)); }, x.ref_); } else { is >> x.ref_; --x.ref_; } return is; } friend std::ostream& operator<<(std::ostream& os, OneBased x) { if constexpr (MyRange) { auto f = [](auto&& e) { return ::OneBased(std::forward(e)); }; os << (x.ref_ | std::views::transform(f)); } else if constexpr (MyTuple) { std::apply([&](auto&... xs) { os << std::tuple(::OneBased(xs)...); }, x.ref_); } else { os << ++x.ref_; --x.ref_; } return os; } private: T ref_; }; template OneBased(T&&) -> OneBased; template OneBased(Ts&&...) -> OneBased>; using namespace std; #define Rep(...) [](int l, int r) { return views::iota(min(l, r), r); }(__VA_ARGS__) #define INF (INT_MAX / 2) #define IN(...) (cin >> forward_as_tuple(__VA_ARGS__)) #define OUT(...) (cout << forward_as_tuple(__VA_ARGS__) << '\n') #endif // __INCLUDE_LEVEL__ == 1