結果

問題 No.3567 Modulo Grid
コンテスト
ユーザー risujiroh
提出日時 2026-06-05 22:55:07
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 4,425 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,874 ms
コンパイル使用メモリ 351,652 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-06-05 22:55:15
合計ジャッジ時間 5,293 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20 WA * 5
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#if __INCLUDE_LEVEL__ == 0

#include __BASE_FILE__

Array2D<int> Solve(int h, int w) {
  if (h == 1 && w == 1)
    return Array2D<int>(h, w);

  int p = 2;
  while (h % p && w % p)
    ++p;

  int nh = h;
  int eh = 0;
  while (nh % p == 0) {
    nh /= p;
    ++eh;
  }

  int nw = w;
  int ew = 0;
  while (nw % p == 0) {
    nw /= p;
    ++ew;
  }

  if (eh > ew) {
    Array2D<int> ret(h, w);
    auto tmp = Solve(w, h);
    for (int i : Rep(0, h))
      for (int j : Rep(0, w))
        ret[i][j] = tmp[j][i];
    return ret;
  }

  assert(eh <= ew);
  assert(ew > 0);

  if (eh > 0) {
    Array2D<int> ret(h, w);
    auto tmp = Solve(nh, w);
    int q = (h * w) / (nh * nw);
    int qh = h / nh;
    for (int i : Rep(0, h))
      for (int j : Rep(0, w)) {
        int v = tmp[i % nh][j];
        ret[i][j] = int(atcoder::crt({v % (q / qh) + i / nh * (q / qh), v % (nh * nw)}, {q, nh * nw}).first);
      }
    return ret;
  }

  vector<vector<int>> rs(2);
  for (int r : Rep(0, w / nw))
    rs[r % p != 0].push_back(r);

  Array2D<int> ret(h, w);
  auto tmp = Solve(h, nw);
  for (int i : Rep(0, h)) {
    int j = 0;
    for (int ri : Rep(0, 2))
      for (int nj : Rep(0, nw)) {
        if (ri)
          nj = nw - nj - 1;
        for (int r : rs[ri]) {
          int v = tmp[i][nj];
          ret[i][j] = int(atcoder::crt({v % (h * nw), r}, {h * nw, w / nw}).first);
          ++j;
        }
      }
  }
  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 <bits/stdc++.h>

#include <atcoder/math.hpp>

template <class T>
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<T>(data_.data() + i * w_, w_); }
  auto operator[](int i) const { return std::span<const T>(data_.data() + i * w_, w_); }

 private:
  int h_;
  int w_;
  std::vector<T> data_;
};

template <class T>
concept MyRange =
    std::ranges::range<T> &&
    !std::convertible_to<T, std::string_view> &&
    !std::convertible_to<T, std::filesystem::path>;

template <class T>
concept MyTuple = std::__is_tuple_like<T>::value && !MyRange<T>;

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<decltype(e)>(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 T>
class OneBased {
 public:
  explicit OneBased(T&& x) : ref_(std::forward<T>(x)) {}

  template <class... Ts>
  requires(sizeof...(Ts) > 1)
      OneBased(Ts&&... xs) : ref_(std::forward_as_tuple(std::forward<Ts>(xs)...)) {}

  friend std::istream& operator>>(std::istream& is, OneBased x) {
    if constexpr (MyRange<T>) {
      for (auto&& e : x.ref_) {
        is >> ::OneBased(e);
      }
    } else if constexpr (MyTuple<T>) {
      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<T>) {
      auto f = [](auto&& e) { return ::OneBased(std::forward<decltype(e)>(e)); };
      os << (x.ref_ | std::views::transform(f));
    } else if constexpr (MyTuple<T>) {
      std::apply([&](auto&... xs) { os << std::tuple(::OneBased(xs)...); }, x.ref_);
    } else {
      os << ++x.ref_;
      --x.ref_;
    }
    return os;
  }

 private:
  T ref_;
};

template <class T>
OneBased(T&&) -> OneBased<T>;

template <class... Ts>
OneBased(Ts&&...) -> OneBased<std::tuple<Ts...>>;

using namespace std;

#define Rep(...) [](int l, int r) { return views::iota(min(l, r), r); }(__VA_ARGS__)
#define IN(...) (cin >> forward_as_tuple(__VA_ARGS__))
#define OUT(...) (cout << forward_as_tuple(__VA_ARGS__) << '\n')

#endif  // __INCLUDE_LEVEL__ == 1
0