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

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define fst first
#define sc second
#define snd second
#define sz(X) (int)(X).size()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define fornr(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define forab(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(X) X.begin(), X.end()

typedef long long ll;
typedef vector<pair<ll, ll>> vpll;
typedef pair<int, int> pii;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ld;
typedef vector<int> vi;

int cnt2(ll x) {
  int ans = 0;
  while (x % 2 == 0) {
    ans++;
    x /= 2;
  }
  return ans;
}

int cnt5(ll x) {
  int ans = 0;
  while (x % 5 == 0) {
    ans++;
    x /= 5;
  }
  return ans;
}

int main() {
  // ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(20);
  int n;
  cin >> n;
  ll ans = 0;
  map<pii, ll> cnt;
  string s;
  forn(i, n) {
    cin >> s;
    ll x = 0;
    bool f = false;
    for (char c : s) {
      if (c != '.')
        x = x * 10 + (c - '0');
      else
        f = true;
    }
    ll y = 1;
    if (f) {
      ll p = sz(s) - s.find('.') - 1;
      forn(j, p) y *= 10;
    }
    ll g = __gcd(x, y);
    x /= g, y /= g;
    cnt[{cnt2(x) - cnt2(y), cnt5(x) - cnt5(y)}]++;
  }
  for (const auto &p1 : cnt) {
    int x = p1.fst.fst, y = p1.fst.snd;
    if (x >= 0 && y >= 0)
      ans += p1.snd * (p1.snd - 1);
    for (const auto &p2 : cnt) {
      if (p1.fst == p2.fst)
        continue;
      int a = p2.fst.fst, b = p2.fst.snd;
      if (a + x >= 0 && b + y >= 0)
        ans += p1.snd * p2.snd;
    }
  }
  ans /= 2;
  cout << ans << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define fst first
#define sc second
#define snd second
#define sz(X) (int)(X).size()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define fornr(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define forab(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(X) X.begin(), X.end()

typedef long long ll;
typedef vector<pair<ll, ll>> vpll;
typedef pair<int, int> pii;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ld;
typedef vector<int> vi;

int main() {
  // ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(20);
  int n;
  cin >> n;
  vi a;
  vector<ld> b;
  ll ans = 0;
  forn(i, n) {
    ld x;
    cin >> x;
    if (x - int(x) < 1e-10)
      a.pb(x);
    else
      b.pb(x);
  }
  ans += sz(a) * (sz(a) - 1) / 2;
  unordered_map<int, int> cnt;
  forn(i, sz(a)) {
    forab(j, 1, int(sqrt(a[i])) + 1) {
      if (a[i] % j == 0)
        cnt[j]++, cnt[a[i] / j]++;
    }
  }
  vector<vi> d(20, vi(20));
  forn(i, sz(b)) {
    ll x = (ll)(b[i] * 1e10), y = 1e10;
    ll g = __gcd(x, y);
    x /= g, y /= g;
    ans += cnt[y];
    int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0;
    ll dv = 5;
    while (x % dv == 0)
      cnt1++, dv *= 5;
    dv = 2;
    while (x % dv == 0)
      cnt2++, dv *= 2;
    dv = 5;
    while (y % dv == 0)
      cnt3++, dv *= 5;
    dv = 2;
    while (y % dv == 0)
      cnt4++, dv *= 2;
    forn(j, 20) {
      forn(k, 20) {
        if (j >= cnt3 && k >= cnt4)
          ans += d[j][k];
      }
    }
    // cout << b[i] << " " << cnt1 << " " << cnt2 << " " << cnt3 << " " << cnt4
    // << "\\n";
    d[cnt1][cnt2]++;
  }
  cout << ans << "\\n";
  return 0;
}