Problem p03968 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop                                                                   \
  char nyaa;                                                                   \
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-11;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
typedef unsigned long long ul;
int n;
ll t1000[4];
void init() {
  t1000[0] = 1;
  rep(i, 3) { t1000[i + 1] = t1000[i] * 1000; }
}
ll calc(ll a, ll b, ll c, ll d) {
  ll res = INF;
  vector<ll> v = {a, b, c, d};
  rep(j, 4) {
    ll sum = 0;
    rep(k, 4) { sum += v[k] * t1000[k]; }
    res = min(res, sum);
    v.push_back(v[0]);
    v.erase(v.begin());
  }
  return res;
}
ll c[400];
ll turn(ll x) {
  vector<ll> v;
  per(j, 4) {
    v.push_back(x / t1000[j]);
    x %= t1000[j];
  }
  reverse(v.begin(), v.end());
  v.push_back(v[0]);
  v.erase(v.begin());
  ll res = 0;
  rep(j, 4) { res += v[j] * t1000[j]; }
  return res;
}
vector<ll> query(ll k) {
  vector<ll> res;
  per(j, 4) {
    res.push_back(k / t1000[j]);
    k %= t1000[j];
  }
  reverse(res.begin(), res.end());
  return res;
}
map<ll, int> mp, num;
int main() {
  init();
  cin >> n;
  rep(i, n) {
    ll x, y, z, w;
    cin >> x >> y >> z >> w;
    c[i] = calc(x, y, z, w);
    mp[c[i]]++;
    ll t = c[i];
    int cnt = 0;
    rep(j, 4) {
      t = turn(t);
      if (t == c[i])
        cnt++;
    }
    num[c[i]] = cnt;
    // cout << c[i] << endl;
  }
  ll ans = 0;
  rep(i, n) {
    rep(j, n) {
      if (i == j)
        continue;
      rep(k, n) {
        if (i == k || j == k)
          continue;
        vector<ll> a1 = query(c[i]);
        ll t1 = c[j], t2 = c[k];
        rep(l, 4) {
          t1 = turn(t1);
          vector<ll> a2 = query(t1);
          if (a1[0] != a2[1] || a1[3] != a2[2])
            continue;
          rep(m, 4) {
            t2 = turn(t2);
            vector<ll> a3 = query(t2);
            if (a1[1] != a3[0] || a1[2] != a3[3])
              continue;
            // cout << t1 << " " << t2 << endl;
            ll z[3];
            z[0] = calc(a3[1], a2[0], a2[3], a3[2]);
            z[1] = calc(a2[2], a3[3], a3[2], a2[3]);
            z[2] = calc(a2[0], a3[1], a3[0], a2[1]);
            ll c1 = mp[z[0]];
            // cout << z[0] << endl;
            // cout << c1 << endl;
            if (z[0] == c[i])
              c1--;
            if (z[0] == c[j])
              c1--;
            if (z[0] == c[k])
              c1--;
            ll c2 = mp[z[1]];
            // cout << c2 << endl;
            if (z[1] == c[i])
              c2--;
            if (z[1] == c[j])
              c2--;
            if (z[1] == c[k])
              c2--;
            if (z[0] == z[1])
              c2--;
            ll c3 = mp[z[2]];
            // cout << c3 << endl;
            if (z[2] == c[i])
              c3--;
            if (z[2] == c[j])
              c3--;
            if (z[2] == c[k])
              c3--;
            if (z[0] == z[2])
              c3--;
            if (z[1] == z[2])
              c3--;
            c1 *= num[z[0]];
            c2 *= num[z[1]];
            c3 *= num[z[2]];
            // cout << c1 << " " << c2 << " " << c3 << endl;
            ans += c1 * c2 * c3;
          }
        }
      }
    }
  }
  // cout << ans << endl;
  cout << ans / 6 << endl;
  // stop
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop                                                                   \
  char nyaa;                                                                   \
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-11;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
typedef unsigned long long ul;
int n;
ll t1000[4];
void init() {
  t1000[0] = 1;
  rep(i, 3) { t1000[i + 1] = t1000[i] * 1000; }
}
ll calc(ll a, ll b, ll c, ll d) {
  ll res = INF;
  vector<ll> v = {a, b, c, d};
  rep(j, 4) {
    ll sum = 0;
    rep(k, 4) { sum += v[k] * t1000[k]; }
    res = min(res, sum);
    v.push_back(v[0]);
    v.erase(v.begin());
  }
  return res;
}
ll c[400];
ll turn(ll x) {
  vector<ll> v;
  per(j, 4) {
    v.push_back(x / t1000[j]);
    x %= t1000[j];
  }
  reverse(v.begin(), v.end());
  v.push_back(v[0]);
  v.erase(v.begin());
  ll res = 0;
  rep(j, 4) { res += v[j] * t1000[j]; }
  return res;
}
vector<ll> query(ll k) {
  vector<ll> res;
  per(j, 4) {
    res.push_back(k / t1000[j]);
    k %= t1000[j];
  }
  reverse(res.begin(), res.end());
  return res;
}
map<ll, int> mp, num;
int main() {
  init();
  cin >> n;
  rep(i, n) {
    ll x, y, z, w;
    cin >> x >> y >> z >> w;
    c[i] = calc(x, y, z, w);
    mp[c[i]]++;
    ll t = c[i];
    int cnt = 0;
    rep(j, 4) {
      t = turn(t);
      if (t == c[i])
        cnt++;
    }
    num[c[i]] = cnt;
    // cout << c[i] << endl;
  }
  ll ans = 0;
  rep(i, n) {
    rep(j, n) {
      if (i == j)
        continue;
      ll t = c[j];
      // cout << i << " " << j << endl;
      rep(k, 4) {
        t = turn(t);
        vector<ll> a1 = query(c[i]), a2 = query(t);
        ll z1 = calc(a1[3], a1[2], a2[1], a2[0]);
        ll z2 = calc(a1[2], a1[1], a2[2], a2[1]);
        ll z3 = calc(a1[1], a1[0], a2[3], a2[2]);
        ll z4 = calc(a1[0], a1[3], a2[0], a2[3]);
        // cout << z1 << endl;
        ll c1 = mp[z1];
        if (z1 == c[i])
          c1--;
        if (z1 == c[j])
          c1--;
        ll c2 = mp[z2];
        if (z2 == c[i])
          c2--;
        if (z2 == c[j])
          c2--;
        if (z2 == z1)
          c2--;
        ll c3 = mp[z3];
        if (z3 == c[i])
          c3--;
        if (z3 == c[j])
          c3--;
        if (z3 == z1)
          c3--;
        if (z3 == z2)
          c3--;
        ll c4 = mp[z4];
        if (z4 == c[i])
          c4--;
        if (z4 == c[j])
          c4--;
        if (z4 == z1)
          c4--;
        if (z4 == z2)
          c4--;
        if (z4 == z3)
          c4--;
        c1 *= num[z1];
        c2 *= num[z2];
        c3 *= num[z3];
        c4 *= num[z4];
        // cout << c1 << " " << c2 << " " << c3 << " " << c4 << endl;
        ans += c1 * c2 * c3 * c4;
      }
    }
  }
  // cout << ans << endl;
  cout << ans / 6 << endl;
  // stop
  return 0;
}