Problem p02588 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

const long long INF = 1e9;
// const ll mod = 1000000007;
ll A[201000];
ll N;

ll f(string S) {
  int idx = -1;
  for (int i = 0; i < S.size(); i++) {
    if (S[i] == '.')
      idx = i;
  }
  if (idx == -1) {
    ll tmp = stoll(S);
    tmp *= INF;
    return tmp;
  }
  ll num = S.size() - idx - 1;
  string T = S.substr(0, idx);
  string Q = S.substr(idx + 1, num);
  ll tmp = stoll(T);
  for (int i = 0; i < num; i++)
    tmp *= 10;
  tmp += stoll(Q);
  for (int i = num; i < 9; i++)
    tmp *= 10;
  return tmp;
}

vector<ll> Div;
ll num[20][20];
ll five[201000], two[201000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  for (int i = 0; i < N; i++) {
    string S;
    cin >> S;
    A[i] = f(S);
    while (A[i] % 2 == 0) {
      A[i] /= 2;
      two[i]++;
    }
    while (A[i] % 5 == 0) {
      A[i] /= 5;
      five[i]++;
    }
    num[two[i]][five[i]]++;
    // cerr << A[i] << " ";
  }
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 20; j++) {
      for (int k = 0; k < 20; k++) {
        if (two[i] + j >= 18 and five[i] + k >= 18) {
          ans += num[j][k];
        }
      }
    }
    if (2 * two[i] >= 18 and 2 * five[i] >= 18) {
      ans -= 1;
    }
  }
  ans /= 2;
  cout << ans << endl;
  // cerr << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

const long long INF = 1e9;
// const ll mod = 1000000007;
ll A[201000];
ll N;

ll f(string S) {
  int idx = -1;
  for (int i = 0; i < S.size(); i++) {
    if (S[i] == '.')
      idx = i;
  }
  if (idx == -1) {
    ll tmp = stoll(S);
    tmp *= INF;
    return tmp;
  }
  ll num = S.size() - idx - 1;
  string T = S.substr(0, idx);
  string Q = S.substr(idx + 1, num);
  ll tmp = stoll(T);
  for (int i = 0; i < num; i++)
    tmp *= 10;
  tmp += stoll(Q);
  for (int i = num; i < 9; i++)
    tmp *= 10;
  return tmp;
}

vector<ll> Div;
ll num[20][20];
ll five[201000], two[201000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  for (int i = 0; i < N; i++) {
    string S;
    cin >> S;
    A[i] = f(S);
    while (A[i] % 2 == 0) {
      A[i] /= 2;
      two[i]++;
    }
    while (A[i] % 5 == 0) {
      A[i] /= 5;
      five[i]++;
    }
    chmin(two[i], 18LL);
    chmin(five[i], 18LL);
    num[two[i]][five[i]]++;
    // cerr << A[i] << " ";
  }
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 20; j++) {
      for (int k = 0; k < 20; k++) {
        if (two[i] + j >= 18 and five[i] + k >= 18) {
          ans += num[j][k];
        }
      }
    }
    if (2 * two[i] >= 18 and 2 * five[i] >= 18) {
      ans -= 1;
    }
  }
  ans /= 2;
  cout << ans << endl;
  // cerr << endl;
  return 0;
}