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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\\n'
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll INF = 1e18;
const ll mod = 1e9 + 7;

int main() {
  ll n;
  cin >> n;
  vector<pll> divcnt(n);
  vector<pll> two, five;
  vector<ll> b(18);
  vector<vector<ll>> sum(50);
  ll v = 1;
  rep(i, 18) {
    b[i] = v;
    v *= 10;
  }
  rep(i, n) {
    string s;
    cin >> s;
    ll p = s.length();
    ll cnt = 0;
    string t = "";
    bool good = false;
    rep(j, p) {
      if (s[j] == '.') {
        good = true;
        continue;
      } else {
        t += s[j];
      }
      if (good) {
        cnt++;
      }
    }
    ll val = 0;
    rep(j, 10 - cnt) { t += '0'; }
    ll u = t.length();
    // cout << t << endl;
    rep(j, u) { val += (ll)(t[j] - '0') * b[u - 1 - j]; }
    // cout << val << endl;
    ll cnt1 = 0, cnt2 = 0;
    while (val % 2 == 0) {
      cnt1++;
      val /= 2;
    }
    while (val % 5 == 0) {
      cnt2++;
      val /= 5;
    }
    // cout << val << endl;
    two.push_back({cnt1, i});
    five.push_back({cnt2, i});
    divcnt[i].fi = cnt1;
    divcnt[i].se = cnt2;
    sum[cnt2].push_back(cnt1);
  }

  rep(i, 20) { sort(sum[i].begin(), sum[i].end()); }
  sort(two.begin(), two.end());
  sort(five.begin(), five.end());
  ll ans = 0;
  rep(i, n) {
    ll x = divcnt[i].fi;
    ll y = divcnt[i].se;
    ll tot = 0;
    for (ll j = 20 - y; j < 50; j++) {
      if (sum[j].size() == 0)
        continue;
      auto it =
          lower_bound(sum[j].begin(), sum[j].end(), 20 - x) - sum[j].begin();
      tot += sum[j].size() - it;
    }
    if (2 * x >= 20 && 2 * y >= 20)
      tot--;
    ans += tot;
  }
  ans /= 2;
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\\n'
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll INF = 1e18;
const ll mod = 1e9 + 7;

int main() {
  ll n;
  cin >> n;
  vector<pll> divcnt(n);
  vector<pll> two, five;
  vector<ll> b(18);
  vector<vector<ll>> sum(50);
  ll v = 1;
  rep(i, 18) {
    b[i] = v;
    v *= 10;
  }
  rep(i, n) {
    string s;
    cin >> s;
    ll p = s.length();
    ll cnt = 0;
    string t = "";
    bool good = false;
    rep(j, p) {
      if (s[j] == '.') {
        good = true;
        continue;
      } else {
        t += s[j];
      }
      if (good) {
        cnt++;
      }
    }
    ll val = 0;
    rep(j, 10 - cnt) { t += '0'; }
    ll u = t.length();
    // cout << t << endl;
    rep(j, u) { val += (ll)(t[j] - '0') * b[u - 1 - j]; }
    // cout << val << endl;
    ll cnt1 = 0, cnt2 = 0;
    while (val % 2 == 0) {
      cnt1++;
      val /= 2;
    }
    while (val % 5 == 0) {
      cnt2++;
      val /= 5;
    }
    // cout << val << endl;
    two.push_back({cnt1, i});
    five.push_back({cnt2, i});
    divcnt[i].fi = cnt1;
    divcnt[i].se = cnt2;
    sum[cnt1].push_back(cnt2);
  }
  rep(i, 50) { sort(sum[i].begin(), sum[i].end()); }
  sort(two.begin(), two.end());
  sort(five.begin(), five.end());
  ll ans = 0;
  rep(i, n) {
    ll x = divcnt[i].fi;
    ll y = divcnt[i].se;
    ll tot = 0;
    for (ll j = 20 - x; j < 50; j++) {
      if (sum[j].size() == 0)
        continue;
      auto it =
          lower_bound(sum[j].begin(), sum[j].end(), 20 - y) - sum[j].begin();
      tot += sum[j].size() - it;
    }
    if (2 * x >= 20 && 2 * y >= 20)
      tot--;
    ans += tot;
  }
  ans /= 2;
  cout << ans << endl;
  return 0;
}