Problem p03968 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)

#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)

#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))
#define popcount(n) (__builtin_popcountll(n))

using namespace std;

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

using ll = long long;
using R = long double;
const R EPS = 1e-9; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R &r) { return (r > EPS) - (r < -EPS); }
inline R sq(R x) { return sqrt(max<R>(x, 0.0)); }

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

// Problem Specific Parameter:
#define error(args...)                                                         \
  {                                                                            \
    vector<string> _debug = split(#args, ',');                                 \
    err(begin(_debug), args);                                                  \
  }

vector<string> split(const string &s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c))
    v.emplace_back(x);
  return move(v);
}

void err(vector<string>::iterator it) { cerr << endl; }
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
  cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << " ",
      err(++it, args...);
}

auto normlize(vector<int> ary) {
  vector<int> cmin = ary;

  rep(j, 4) {
    rotate(begin(ary), begin(ary) + 1, end(ary));
    chmin(cmin, ary);
  }

  return cmin;
}

int duplicate(vector<int> ary) {
  int ret = 0;
  vector<int> cur = ary;

  rep(j, 4) {
    rotate(begin(cur), begin(cur) + 1, end(cur));
    if (cur == ary)
      ret++;
  }

  return ret;
}

map<vector<int>, ll> memo;

int main(void) {
  int n;
  cin >> n;
  vector<vector<int>> c(n, vector<int>(4, 0));

  rep(i, n) {
    rep(j, 4) cin >> c[i][j];
    c[i] = normlize(c[i]);
    memo[c[i]]++;
  }

  ll ans = 0LL;

  rep(j, n) rep(i, j) {
    memo[c[i]]--;
    memo[c[j]]--;

    rep(di, 4) rep(dj, 4) {
      vector<int> top = c[i];
      vector<int> bottom = c[j];
      reverse(begin(bottom), end(bottom));

      rotate(begin(top), begin(top) + di, end(top));
      rotate(begin(bottom), begin(bottom) + dj, end(bottom));

      ll cur = 1LL;

      map<vector<int>, ll> tmp;

      rep(dd, 4) {
        vector<int> rot = {bottom[(dd + 0) % 4], bottom[(dd + 1) % 4],
                           top[(dd + 1) % 4], top[(dd + 0) % 4]};
        rot = normlize(rot);
        tmp[rot]++;
      }

      for (auto &it : tmp) {
        const ll coef = duplicate(it.first);

        rep(k, it.second) {
          cur *= coef;
          cur *= (memo[it.first] - k);
        }
      }

      ans += 1LL * cur;
    }

    memo[c[i]]++;
    memo[c[j]]++;
  }

  cout << ans / 12LL << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)

#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)

#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))
#define popcount(n) (__builtin_popcountll(n))

using namespace std;

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

using ll = long long;
using R = long double;
const R EPS = 1e-9; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R &r) { return (r > EPS) - (r < -EPS); }
inline R sq(R x) { return sqrt(max<R>(x, 0.0)); }

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

// Problem Specific Parameter:
#define error(args...)                                                         \
  {                                                                            \
    vector<string> _debug = split(#args, ',');                                 \
    err(begin(_debug), args);                                                  \
  }

vector<string> split(const string &s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c))
    v.emplace_back(x);
  return move(v);
}

void err(vector<string>::iterator it) { cerr << endl; }
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
  cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << " ",
      err(++it, args...);
}

auto normlize(vector<int> ary) {
  vector<int> cmin = ary;

  rep(j, 4) {
    rotate(begin(ary), begin(ary) + 1, end(ary));
    chmin(cmin, ary);
  }

  return cmin;
}

int duplicate(vector<int> ary) {
  int ret = 0;
  vector<int> cur = ary;

  rep(j, 4) {
    rotate(begin(cur), begin(cur) + 1, end(cur));
    if (cur == ary)
      ret++;
  }

  return ret;
}

class vector_hash {
public:
  size_t operator()(const vector<int> &x) const {
    const int C = 1000000007;
    size_t t = 0;
    for (int i = 0; i < x.size(); ++i) {
      t = t * C + x[i];
    }
    return t;
  }
};

unordered_map<vector<int>, ll, vector_hash> memo;

int main(void) {
  int n;
  cin >> n;
  vector<vector<int>> c(n, vector<int>(4, 0));

  rep(i, n) {
    rep(j, 4) cin >> c[i][j];
    c[i] = normlize(c[i]);
    memo[c[i]]++;
  }

  ll ans = 0LL;

  rep(j, n) rep(i, j) {
    memo[c[i]]--;
    memo[c[j]]--;

    rep(di, 4) rep(dj, 4) {
      vector<int> top = c[i];
      vector<int> bottom = c[j];
      reverse(begin(bottom), end(bottom));

      rotate(begin(top), begin(top) + di, end(top));
      rotate(begin(bottom), begin(bottom) + dj, end(bottom));

      ll cur = 1LL;

      map<vector<int>, ll> tmp;

      rep(dd, 4) {
        vector<int> rot = {bottom[(dd + 0) % 4], bottom[(dd + 1) % 4],
                           top[(dd + 1) % 4], top[(dd + 0) % 4]};
        rot = normlize(rot);
        tmp[rot]++;
      }

      for (auto &it : tmp) {
        const ll coef = duplicate(it.first);

        rep(k, it.second) {
          cur *= coef;
          cur *= (memo[it.first] - k);
        }
      }

      ans += 1LL * cur;
    }

    memo[c[i]]++;
    memo[c[j]]++;
  }

  cout << ans / 12LL << endl;
  return 0;
}