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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const ll LINF = 1LL << 60;
const int MOD = 1000000007;

template <typename T> void print(const T &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << ' ';
    cout << v[i];
  }
  cout << endl;
}

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

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

int N;
string A[200010];
int p2[200010], p5[200010];
int cnt[61][31];

P f(string s) {
  ll num = 0;
  bool flag = true;
  int root = 8;
  for (char c : s) {
    if (flag) {
      if (c != '.') {
        num += c - '0';
        num *= 10;
      } else {
        flag = false;
      }
    } else {
      num += c - '0';
      num *= 10;
      --root;
    }
  }
  num *= pow(10, root);
  if (num == 0) {
    return (make_pair(18, 18));
  }
  int p2 = 0, p5 = 0;
  while (num % 2 == 0) {
    ++p2;
    num /= 2;
  }
  while (num % 5 == 0) {
    ++p5;
    num /= 5;
  }

  return make_pair(p2, p5);
}

void solve() {
  memset(cnt, 0, sizeof(cnt));
  for (int i = 0; i < N; i++) {
    auto ps = f(A[i]);
    p2[i] = ps.first;
    p5[i] = ps.second;
    ++cnt[p2[i]][p5[i]];
  }
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    int need2 = max(18 - p2[i], 0);
    int need5 = max(18 - p5[i], 0);
    for (int j = need2; j <= 60; j++) {
      for (int k = need5; k <= 30; k++) {
        ans += cnt[j][k];
      }
    }
    // 自身の分を取り除く
    if (need2 <= p2[i] && need5 <= p5[i]) {
      --ans;
    }
  }
  ans /= 2;
  cout << ans << endl;
  return;
}

void input() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
}

int main() {
  input();
  solve();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const ll LINF = 1LL << 60;
const int MOD = 1000000007;

template <typename T> void print(const T &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << ' ';
    cout << v[i];
  }
  cout << endl;
}

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

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

int N;
string A[20010];
int p2[20010], p5[20010];
int cnt[61][31];

P f(string s) {
  ll num = 0;
  bool flag = true;
  int root = 8;
  for (char c : s) {
    if (flag) {
      if (c != '.') {
        num += c - '0';
        num *= 10;
      } else {
        flag = false;
      }
    } else {
      num += c - '0';
      num *= 10;
      --root;
    }
  }
  num *= pow(10, root);
  if (num == 0) {
    return (make_pair(18, 18));
  }
  int p2 = 0, p5 = 0;
  while (num % 2 == 0) {
    ++p2;
    num /= 2;
  }
  while (num % 5 == 0) {
    ++p5;
    num /= 5;
  }

  return make_pair(p2, p5);
}

void solve() {
  memset(cnt, 0, sizeof(cnt));
  for (int i = 0; i < N; i++) {
    auto ps = f(A[i]);
    p2[i] = ps.first;
    p5[i] = ps.second;
    ++cnt[p2[i]][p5[i]];
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    int need2 = max(18 - p2[i], 0);
    int need5 = max(18 - p5[i], 0);
    for (int j = need2; j <= 60; j++) {
      for (int k = need5; k <= 30; k++) {
        ans += cnt[j][k];
      }
    }
    // 自身の分を取り除く
    if (need2 <= p2[i] && need5 <= p5[i]) {
      --ans;
    }
  }
  ans /= 2;
  cout << ans << endl;
  return;
}

void input() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
}

int main() {
  input();
  solve();
}