Problem p00645 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define dump(...) cout << "# " << #__VA_ARGS__ << '=' << (__VA_ARGS__) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}

template <typename Tuple> void print_tuple(ostream &, const Tuple &) {}
template <typename Car, typename... Cdr, typename Tuple>
void print_tuple(ostream &os, const Tuple &t) {
  print_tuple<Cdr...>(os, t);
  os << (sizeof...(Cdr) ? "," : "") << get<sizeof...(Cdr)>(t);
}
template <typename... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  print_tuple<Args...>(os << '(', t);
  return os << ')';
}

template <typename Ch, typename Tr, typename C, typename = decltype(begin(C()))>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os, const C &c) {
  os << '[';
  for (auto i = begin(c); i != end(c); ++i)
    os << (i == begin(c) ? "" : " ") << *i;
  return os << ']';
}

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

inline int ctz(uint x) {
  int i = 0;
  if (!(x & 0x0000ffff))
    i += 16, x >>= 16;
  if (!(x & 0x000000ff))
    i += 8, x >>= 8;
  if (!(x & 0x0000000f))
    i += 4, x >>= 4;
  if (!(x & 0x00000003))
    i += 2, x >>= 2;
  if (!(x & 0x00000001))
    i += 1, x >>= 1;
  return i + !x;
}

int solve(int x0) {
  static array<int, 225> ms;
  {
    int k = 0;
    rep(t, 5) repi(b, t, 5) rep(l, 5) repi(r, l, 5) {
      int m = 0;
      repi(i, t, b + 1) repi(j, l, r + 1) m |= 1 << (i * 5 + j);
      ms[k++] = m;
    }
  }

  queue<tuple<int, int>> q;
  q.emplace(x0, 0);
  unordered_set<int> vis;
  while (q.size()) {
    int x, c;
    tie(x, c) = q.front();
    q.pop();
    if (x == 0)
      return c;
    if (!vis.insert(x).second)
      continue;
    for (int m : ms)
      if (ctz(x) == ctz(m))
        q.emplace(x ^ m, c + 1);
  }
  return -1;
}

int main() {
  for (int n; cin >> n && n;) {
    int x0 = 0;
    rep(i, n) rep(j, n) {
      int b;
      cin >> b;
      x0 |= b << (i * 5 + j);
    }
    string res;
    per(_, solve(x0)) res += "myon";
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define dump(...) cout << "# " << #__VA_ARGS__ << '=' << (__VA_ARGS__) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}

template <typename Tuple> void print_tuple(ostream &, const Tuple &) {}
template <typename Car, typename... Cdr, typename Tuple>
void print_tuple(ostream &os, const Tuple &t) {
  print_tuple<Cdr...>(os, t);
  os << (sizeof...(Cdr) ? "," : "") << get<sizeof...(Cdr)>(t);
}
template <typename... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  print_tuple<Args...>(os << '(', t);
  return os << ')';
}

template <typename Ch, typename Tr, typename C, typename = decltype(begin(C()))>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os, const C &c) {
  os << '[';
  for (auto i = begin(c); i != end(c); ++i)
    os << (i == begin(c) ? "" : " ") << *i;
  return os << ']';
}

constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr double EPS = 1e-9;

template <typename I1, typename I2>
bool is_intersection(I1 first1, I1 last1, I2 first2, I2 last2) {
  while (first1 != last1 && first2 != last2) {
    if (*first1 == *first2)
      return true;
    (*first1 < *first2 ? first1 : first2)++;
  }
  return false;
}

int main() {
  static array<int, 225> ms;
  {
    int k = 0;
    rep(t, 5) repi(b, t, 5) rep(l, 5) repi(r, l, 5) {
      int m = 0;
      repi(i, t, b + 1) repi(j, l, r + 1) m |= 1 << (i * 5 + j);
      ms[k++] = m;
    }
  }

  static array<uint8_t, 1 << 25> ds;
  ds.fill(-1);
  ds[0] = 0;
  for (int i = 0; count(all(ds), i); i++)
    rep(j, 1 << 25) if (ds[j] == i) for (int m : ms) ds[j ^ m] =
        min<uint8_t>(ds[j ^ m], i + 1);

  for (int n; cin >> n && n;) {
    int x = 0;
    rep(i, n) rep(j, n) {
      int b;
      cin >> b;
      x |= b << (i * 5 + j);
    }
    string res;
    rep(i, ds[x]) res += "myon";
    cout << res << endl;
  }
}