Problem p03364 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;

using namespace std;

const double PI = 3.14159265358979323846;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
// #define rep(i, n) for (ll i = 0; i < (n); ++i)
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

double equal(double a, double b) { return fabs(a - b) < DBL_EPSILON; }

std::istream &operator>>(std::istream &in, set<int> &o) {
  int a;
  in >> a;
  o.insert(a);
  return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
  ll a;
  in >> a;
  o.push(a);
  return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

// ofstream outfile("log.txt");
// outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0')
// << rank << endl;
//  std::cout << std::bitset<8>(9);
// const ll mod = 1e10;

typedef priority_queue<ll, vector<ll>, greater<ll>> PQ_ASK;

int main() {
  int n;
  cin >> n;
  vector<vector<char>> board(n, vector<char>(n));
  rep(y, n) rep(x, n) cin >> board[y][x];

  auto mapping = [&](int y, int x) {
    vector<vector<char>> next(n, vector<char>(n));
    rep(yi, n) rep(xi, n) { next[yi][xi] = board[(yi + y) % n][(xi + x) % n]; }
    return next;
  };

  auto check = [&](vector<vector<char>> &next) -> bool {
    rep(y, n) rep(x, n) {
      if (next[y][x] != next[x][y])
        return false;
    }
    return true;
  };

  int ans = 0;
  rep(y, n) {
    rep(x, n) {
      vector<vector<char>> next = mapping(y, x);
      bool ok = check(next);
      if (ok)
        ans++;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;

using namespace std;

const double PI = 3.14159265358979323846;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
// #define rep(i, n) for (ll i = 0; i < (n); ++i)
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

double equal(double a, double b) { return fabs(a - b) < DBL_EPSILON; }

std::istream &operator>>(std::istream &in, set<int> &o) {
  int a;
  in >> a;
  o.insert(a);
  return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
  ll a;
  in >> a;
  o.push(a);
  return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

// ofstream outfile("log.txt");
// outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0')
// << rank << endl;
//  std::cout << std::bitset<8>(9);
// const ll mod = 1e10;

typedef priority_queue<ll, vector<ll>, greater<ll>> PQ_ASK;

int main() {
  int n;
  cin >> n;
  vector<vector<char>> board(n, vector<char>(n));
  rep(y, n) rep(x, n) cin >> board[y][x];

  auto mapping = [&](int y, int x) {
    vector<vector<char>> next(n, vector<char>(n));
    rep(yi, n) rep(xi, n) { next[yi][xi] = board[(yi + y) % n][(xi + x) % n]; }
    return next;
  };

  auto check = [&](vector<vector<char>> &next) -> bool {
    rep(y, n) rep(x, n) {
      if (next[y][x] != next[x][y])
        return false;
    }
    return true;
  };

  int ans = 0;
  rep(y, n) {
    //        rep(x, n) {
    vector<vector<char>> next = mapping(y, 0);
    bool ok = check(next);
    if (ok)
      ans += n;
    //        }
  }
  cout << ans << endl;
}