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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()

string remove_zero(string s) {
  int index = s.size() - 1;
  while (true) {
    if (s[index] == '0')
      index--;
    else
      break;
  }
  return s.substr(0, index + 1);
}

pii get_25(ll n) {
  int ret2 = 0, ret5 = 0;
  while (n % 10 == 0)
    n /= 10;
  while (n % 2 == 0) {
    ret2++;
    n /= 2;
  }
  while (n % 5 == 0) {
    ret5++;
    n /= 5;
  }
  return {ret2, ret5};
}

int main() {
  ll N;
  int cnt[13][50][50];
  REP(i, 13) REP(j, 50) REP(k, 50) cnt[i][j][k] = 0;
  cin >> N;
  REP(i, N) {
    string s;
    cin >> s;
    auto it = s.find(".");
    if (it == string::npos) {
      int zeroc = 0;
      RREP(j, s.size()) {
        if (s[j] == '0')
          zeroc++;
        else
          break;
      }
      pii ret = get_25(atoll(s.c_str()));
      cnt[3 - zeroc][ret.first][ret.second]++;
    } else {
      bool all_zero = true;
      FOR(j, it + 1, s.size()) {
        if (s[j] != '0')
          all_zero = false;
      }
      if (all_zero) {
        int zeroc = 0;
        RREP(j, it) {
          if (s[j] == '0')
            zeroc++;
          else
            break;
        }
        pii ret = get_25(atoll(s.c_str()));
        cnt[3 - zeroc][ret.first][ret.second]++;
      } else {
        s = remove_zero(s);
        pii ret = get_25(atoll(s.erase(it, 1).c_str()));
        cnt[3 + (s.size() - it)][ret.first][ret.second]++;
      }
    }
  }

  ll ans1 = 0, ans2 = 0;
  REP(si, 13) REP(sj, 50) REP(sk, 50) {
    ll sc = cnt[si][sj][sk];
    if (sc == 0)
      continue;

    REP(di, 13) REP(dj, 50) REP(dk, 50) {
      ll dc = cnt[di][dj][dk];
      if (dc == 0)
        continue;

      int digit = si + di - 6 - min(sj, dk) - min(sk, dj);
      if (digit <= 0) {
        if (si == di && sj == dj && sk == dk) {
          ans1 += sc * (sc - 1) / 2;
        } else {
          ans2 += sc * dc;
        }
      }
    }
  }
  cout << ans1 + ans2 / 2 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()

string remove_zero(string s) {
  int index = s.size() - 1;
  while (true) {
    if (s[index] == '0')
      index--;
    else
      break;
  }
  return s.substr(0, index + 1);
}

pii get_25(ll n) {
  int ret2 = 0, ret5 = 0;
  while (n % 10 == 0)
    n /= 10;
  while (n % 2 == 0) {
    ret2++;
    n /= 2;
  }
  while (n % 5 == 0) {
    ret5++;
    n /= 5;
  }
  return {ret2, ret5};
}

int main() {
  ll N;
  int cnt[12][50][50];
  REP(i, 12) REP(j, 50) REP(k, 50) cnt[i][j][k] = 0;
  cin >> N;
  REP(i, N) {
    string s;
    cin >> s;
    auto it = s.find(".");
    if (it == string::npos) {
      int zeroc = 0;
      RREP(j, s.size()) {
        if (s[j] == '0')
          zeroc++;
        else
          break;
      }
      pii ret = get_25(atoll(s.c_str()));
      cnt[3 - zeroc][ret.first][ret.second]++;
    } else {
      bool all_zero = true;
      FOR(j, it + 1, s.size()) {
        if (s[j] != '0')
          all_zero = false;
      }
      if (all_zero) {
        int zeroc = 0;
        RREP(j, it) {
          if (s[j] == '0')
            zeroc++;
          else
            break;
        }
        pii ret = get_25(atoll(s.c_str()));
        cnt[3 - zeroc][ret.first][ret.second]++;
      } else {
        s = remove_zero(s);
        pii ret = get_25(atoll(s.erase(it, 1).c_str()));
        cnt[3 + (s.size() - it)][ret.first][ret.second]++;
      }
    }
  }

  ll ans1 = 0, ans2 = 0;
  REP(si, 12) REP(sj, 50) REP(sk, 50) {
    ll sc = cnt[si][sj][sk];
    if (sc == 0)
      continue;

    REP(di, 12) REP(dj, 50) REP(dk, 50) {
      ll dc = cnt[di][dj][dk];
      if (dc == 0)
        continue;

      int digit = si + di - 6 - min(sj, dk) - min(sk, dj);
      if (digit <= 0) {
        if (si == di && sj == dj && sk == dk) {
          ans1 += sc * (sc - 1) / 2;
        } else {
          ans2 += sc * dc;
        }
      }
    }
  }
  cout << ans1 + ans2 / 2 << endl;
  return 0;
}