Problem p02588 (C++) — buggy submission, verdict: Runtime Error

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << p.first << ' ' << p.second;
}

const int N = 2e5 + 10;
const int X = 60;

// head

int n;
ld a[N];
int cnt[60][60];
ll ans;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n) {
    ll now = a[i] * 1e9 + 0.1;
    int b = 0;
    while (now % 5 == 0) {
      now /= 5;
      b++;
    }
    cnt[__builtin_ctzll(now)][b]++;
  }
  rep(i, X) rep(j, X) rep(ii, X)
      rep(jj, X) if (P(i, j) <= P(ii, jj) and i + ii >= 18 and j + jj >= 18) {
    if (P(i, j) == P(ii, jj)) {
      ans += (ll)cnt[i][j] * (cnt[i][j] - 1) >> 1;
    } else {
      ans += (ll)cnt[i][j] * cnt[ii][jj];
    }
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << p.first << ' ' << p.second;
}

template <typename T> struct BIT2D {
  int H, W;
  vector<vector<T>> bit; // データの格納先
  BIT2D(int H_, int W_) { init(H_, W_); }
  void init(int H_, int W_) {
    H = H_ + 1;
    W = W_ + 1;
    bit.assign(H, vector<T>(W, 0));
  }

  void add(int h, int w, T x) {
    for (int i = h; i < H; i += (i & -i)) {
      for (int j = w; j < W; j += (j & -j)) {
        bit[i][j] += x;
      }
    }
  }
  // 1≦i≦h かつ 1≦j≦w
  T sum(int h, int w) {
    T s(0);
    for (int i = h; i > 0; i -= (i & -i)) {
      for (int j = w; j > 0; j -= (j & -j)) {
        s += bit[i][j];
      }
    }
    return s;
  }

  // h1≦i<h2 かつ w1≦j<w2
  T query(int h1, int w1, int h2, int w2) {
    return sum(h2 - 1, w2 - 1) - sum(h2 - 1, w1 - 1) - sum(h1 - 1, w2 - 1) +
           sum(h1 - 1, w1 - 1);
  }
};

const int jk = 9;
const int N = 1e4 + 10;
const ld eps = 1e-14;
// head

int n;
ld a[N];
BIT2D<int> bit(jk * 2 + 1, jk * 2 + 1);
ll ans;
bool check(ld x) {
  ll x_ = x + eps;
  if (abs(x - x_) < eps)
    return true;
  else
    return false;
}
P change(ll x) {
  P res = P(0, 0);
  while (~x & 1) {
    res.first++;
    x >>= 1;
  }
  while (x % 5 == 0) {
    res.second++;
    x /= 5;
  }
  chmin(res.first, 9);
  chmin(res.second, 9);
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  rep(i, n) cin >> a[i];

  rep(i, n) {
    ld c = a[i];
    bool ok = true;
    rep(i, jk + 1) {
      ld hg = c;
      c *= 2;
      rep(j, jk + 1) {
        if (check(hg)) {
          ll b = hg + eps;
          P gg = change(b);
          gg.first -= i;
          gg.second -= j;
          // cout << i << ' ' << gg << endl;
          ans += bit.sum(gg.first + jk + 1, gg.second + jk + 1);
          bit.add(jk - gg.first + 1, jk - gg.second + 1, 1);
          ok = false;
          break;
        }
        hg *= 5;
      }
      if (!ok)
        break;
    }
  }

  cout << ans << endl;
}