Problem p02947 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
// typedef long double ld;

typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef set<int> si;
typedef set<ll> sll;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<vll> vvll;

constexpr ll MOD = 1e9 + 7;
constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;

#define Sort(x) sort(x.begin(), x.end())
#define Reverse(x) reverse(x.begin(), x.end())
#define ABS(a, b) ((a) < (b) ? (b) - (a) : (a) - (b))
#define nbit(n) (1ll << (n))

// sort(begin(x), end(x), greater<ll>());
// lower_bound(a.begin(), a.end(), val): a[i] ≥ valである最左の位置
// upper_bound(a.begin(), a.end(), val): a[i] > valである最左の位置

// Returns minimum of a and b.
// If a is less b, a is set to b.
template <typename T> T chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
  return a;
}

// Returns maximum of a and b.
// If a is less b, a is set to b.
template <typename T> T chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
  return a;
}

// a = (a+b)%MOD;
void addmod(ll &a, ll b) { a = (a + b) % MOD; }

template <typename T> void dumpContents(const T &v, const string &msg) {
  cerr << "### " << msg << " ###\n";
  for (const auto &x : v) {
    cerr << x << " ";
  }
  cerr << endl;
}

struct before_main_function {
  before_main_function() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(20) << fixed;
#define endl "\n"
  }
} before_main_function;

// ========== end of template ==========
// x!
ll factorial_r(ll x) {
  assert(x >= 0);
  ll ret = 1;
  while (x > 1) {
    ret *= x;
    x--;
  }
  return ret;
}

// xCy
ll combi_r(ll x, ll y) {
  assert(x >= 0);
  assert(y >= 0);
  assert(x >= y);
  return factorial_r(x) / factorial_r(x - y) / factorial_r(y);
}

int main(int argc, char **argv) {
  int N;
  cin >> N;

  vector<vector<char>> S(N, vector<char>(11));
  map<string, int> counts;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 10; j++) {
      cin >> S[i][j];
    }
    S[i][10] = '\0';
    sort(begin(S[i]), begin(S[i]) + 10);
    string s(&S[i][0]);
    counts[s] += 1;
  }

  ll ret = 0;
  for (auto kv : counts) {
    ll v = kv.second;
    if (v < 2)
      continue;
    ret += combi_r(v, 2);
  }

  cout << ret << endl;

  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
// typedef long double ld;

typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef set<int> si;
typedef set<ll> sll;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<vll> vvll;

constexpr ll MOD = 1e9 + 7;
constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;

#define Sort(x) sort(x.begin(), x.end())
#define Reverse(x) reverse(x.begin(), x.end())
#define ABS(a, b) ((a) < (b) ? (b) - (a) : (a) - (b))
#define nbit(n) (1ll << (n))

// sort(begin(x), end(x), greater<ll>());
// lower_bound(a.begin(), a.end(), val): a[i] ≥ valである最左の位置
// upper_bound(a.begin(), a.end(), val): a[i] > valである最左の位置

// Returns minimum of a and b.
// If a is less b, a is set to b.
template <typename T> T chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
  return a;
}

// Returns maximum of a and b.
// If a is less b, a is set to b.
template <typename T> T chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
  return a;
}

// a = (a+b)%MOD;
void addmod(ll &a, ll b) { a = (a + b) % MOD; }

template <typename T> void dumpContents(const T &v, const string &msg) {
  cerr << "### " << msg << " ###\n";
  for (const auto &x : v) {
    cerr << x << " ";
  }
  cerr << endl;
}

struct before_main_function {
  before_main_function() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(20) << fixed;
#define endl "\n"
  }
} before_main_function;

// ========== end of template ==========
// x!
ll factorial_r(ll x) {
  assert(x >= 0);
  ll ret = 1;
  while (x > 1) {
    ret *= x;
    x--;
  }
  return ret;
}

// xCy
ll combi_r(ll x, ll y) {
  assert(x >= 0);
  assert(y >= 0);
  assert(x >= y);
  ll ret = 1;
  for (int i = 0; i < y; i++) {
    ret *= (x - i);
  }
  return ret / factorial_r(y);
}

int main(int argc, char **argv) {
  int N;
  cin >> N;

  vector<vector<char>> S(N, vector<char>(11));
  map<string, int> counts;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 10; j++) {
      cin >> S[i][j];
    }
    S[i][10] = '\0';
    sort(begin(S[i]), begin(S[i]) + 10);
    string s(&S[i][0]);
    counts[s] += 1;
  }

  ll ret = 0;
  for (auto kv : counts) {
    ll v = kv.second;
    if (v < 2)
      continue;
    ret += combi_r(v, 2);
  }

  cout << ret << endl;

  return 0;
}