Problem p02588 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll, ll> P;
typedef pair<double, ll> Pd;
typedef pair<double, double> PD;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;
typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue;

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
const double DEL = 1e-6;

#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());
#define ADD(a, b) a = (a + b) % mod
#define MUL(a, b) a = (a * b) % mod

const ll BIG = 1e9;
struct F {
  ll Two;
  ll Five;
};

bool two_sort(F x, F y) {
  return x.Two != y.Two ? x.Two < y.Two : x.Five < y.Five;
}

bool five_sort(F x, F y) {
  return x.Five != y.Five ? x.Five < y.Five : x.Two < y.Two;
}

F Change(string S) {
  ll Count = 9;
  bool below = false;
  ll ret = 0;
  rep(i, S.size()) {
    if (S[i] == '.')
      below = true;
    else {
      ret *= 10;
      ret += (S[i] - '0');
      if (below)
        Count--;
    }
  }
  ll two = Count, five = Count;
  while (!(ret % 2)) {
    ret /= 2;
    two++;
  }
  while (!(ret % 5)) {
    ret /= 5;
    five++;
  }
  if (two >= 18)
    two = 18;
  if (five >= 18)
    five = 18;
  F RET = {two, five};
  return RET;
}

int main() {
  ll N;
  cin >> N;
  vector<F> d;
  ll Count = 0;
  rep(i, N) {
    string S;
    cin >> S;
    d.pb(Change(S));
    if (d[i].Two >= 9 && d[i].Five >= 9)
      Count++;
  }

  sort(ALL(d), two_sort);
  vec x(19, 0);
  ll memo = N - 1;
  ll ans = 0;
  rep(i, N) {
    while (true) {
      if (d[memo].Two + d[i].Two < 18)
        break;
      x[d[memo].Five]++;
      memo--;
    }
    REP(j, 18 - d[i].Five, 19) ans += x[j];
  }
  ans -= Count;
  ans /= 2;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll, ll> P;
typedef pair<double, ll> Pd;
typedef pair<double, double> PD;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;
typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue;

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
const double DEL = 1e-6;

#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());
#define ADD(a, b) a = (a + b) % mod
#define MUL(a, b) a = (a * b) % mod

const ll BIG = 1e9;
struct F {
  ll Two;
  ll Five;
};

bool two_sort(F x, F y) {
  return x.Two != y.Two ? x.Two < y.Two : x.Five < y.Five;
}

bool five_sort(F x, F y) {
  return x.Five != y.Five ? x.Five < y.Five : x.Two < y.Two;
}

F Change(string S) {
  ll Count = 9;
  bool below = false;
  ll ret = 0;
  rep(i, S.size()) {
    if (S[i] == '.')
      below = true;
    else {
      ret *= 10;
      ret += (S[i] - '0');
      if (below)
        Count--;
    }
  }
  ll two = Count, five = Count;
  while (!(ret % 2)) {
    ret /= 2;
    two++;
  }
  while (!(ret % 5)) {
    ret /= 5;
    five++;
  }
  if (two >= 18)
    two = 18;
  if (five >= 18)
    five = 18;
  F RET = {two, five};
  return RET;
}

int main() {
  ll N;
  cin >> N;
  vector<F> d;
  ll Count = 0;
  rep(i, N) {
    string S;
    cin >> S;
    d.pb(Change(S));
    if (d[i].Two >= 9 && d[i].Five >= 9)
      Count++;
  }

  sort(ALL(d), two_sort);
  vec x(19, 0);
  ll memo = N - 1;
  ll ans = 0;
  rep(i, N) {
    while (memo >= 0) {
      if (d[memo].Two + d[i].Two < 18)
        break;
      x[d[memo].Five]++;
      memo--;
    }
    REP(j, 18 - d[i].Five, 19) ans += x[j];
  }
  ans -= Count;
  ans /= 2;
  cout << ans << endl;
}