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

#include <bits/stdc++.h>

#define st first
#define nd second
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 2e5 + 10;
char s[50];
vector<int> ig;
vector<pii> m2, m5, req;

int qs[10][10], sum[10];
bool isTos[N];
int isInt(int len) {
  bool force = false;
  int pos = -1;
  for (int i = 0; i < len; i++) {
    if (s[i] == '.') {
      pos = i;
      force = true;
    } else if (force && s[i] != '0') {
      return -pos;
    }
  }
  if (pos == -1)
    pos = len;
  return pos;
}
ll toInt(int beg, int tar) {
  ll num = 0;
  for (int i = beg; i < tar; i++) {
    if (s[i] == '.')
      continue;
    num *= 10LL;
    num += s[i] - '0';
  }
  return num;
}
int main() {
  int n, cnt = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s);
    int k = strlen(s);
    int pos = isInt(k);
    if (pos > 0) {
      cnt++;
      ig.push_back(toInt(0, pos));
    } else {
      pos *= -1;
      ll x = toInt(0, k);
      int len = k - pos - 1;
      pii p = {len, len};
      for (int i = 0; i < len; i++) {
        if (x % 2 == 0) {
          x /= 2;
          p.st--;
        } else
          break;
      }
      for (int i = 0; i < len; i++) {
        if (x % 5 != 0)
          break;
        x /= 5;
        p.nd--;
      }
      if (x % 2 == 0) {
        for (int i = 1; i <= 9; i++) {
          if (x % 2 != 0)
            break;
          p.st++;
          x /= 2;
        }
        m2.push_back(p);
      } else if (x % 5 == 0) {
        for (int i = 1; i <= 9; i++) {
          if (x % 5 != 0)
            break;
          p.nd++;
          x /= 5;
        }
        m5.push_back(p);
      } else
        req.push_back(p);
    }
  }
  int bi = 1;
  for (int i = 0; i <= 9; i++) {
    int val = bi;
    for (int j = 0; j <= 9; j++) {
      for (auto it : ig) {
        if (it % val == 0) {
          qs[i][j]++;
        }
      }
      if (qs[i][j] == 0)
        break;
      val *= 5;
    }
    if (qs[i][0] == 0)
      break;
    bi *= 2;
  }
  ll ans = 1LL * cnt * (cnt - 1) / 2;
  for (auto it : req) {
    ans += qs[it.st][it.nd];
  }
  sort(m2.begin(), m2.end());
  sort(m5.begin(), m5.end());
  int pt = 0;
  for (auto it : m2) {
    for (; pt < m5.size() && m5[pt].st <= it.st; pt++) {
      for (int i = m5[pt].nd; i > 0; i--) {
        sum[i]++;
      }
    }
    ans += sum[it.nd] + qs[0][it.nd];
  }
  for (auto it : m5) {
    ans += qs[it.st][0];
  }
  printf("%lld", ans);
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define st first
#define nd second
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 2e5;
char s[50];

vector<pii> req;

int all[N], qs[50][50];
int isInt(int len) {
  bool force = false;
  int pos = -1;
  for (int i = 0; i < len; i++) {
    if (s[i] == '.') {
      pos = i;
      force = true;
    } else if (force && s[i] != '0') {
      return -pos;
    }
  }
  if (pos == -1)
    pos = len;
  return pos;
}
int toInt(int beg, int tar) {
  int num = 0;
  for (int i = beg; i < tar; i++) {
    num *= 10;
    num += s[i] - '0';
  }
  return num;
}
int main() {
  int n, cnt = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s);
    int k = strlen(s);
    int pos = isInt(k);
    if (pos > 0) {
      cnt++;
      all[i] = toInt(0, pos);
    } else {
      pos *= -1;
      int x = toInt(pos + 1, k);
      int len = k - pos - 1;
      pii p = {len, len};
      for (int i = 0; i < len; i++) {
        if (x % 2 == 0) {
          x /= 2;
          p.st--;
        } else
          break;
      }
      for (int i = 0; i < len; i++) {
        if (x % 5 != 0)
          break;
        x /= 5;
        p.nd--;
      }
      all[i] = x;
      req.push_back(p);
    }
  }
  int bi = 1;
  for (int i = 0; i <= 23; i++) {
    int val = bi;
    for (int j = 0; j <= 13; j++) {
      for (int ii = 1; ii <= n; ii++) {
        if (all[ii] % val == 0)
          qs[i][j]++;
      }
      if (qs[i][j] == 0)
        break;
      val *= 5;
    }
    bi <<= 1;
  }
  ll ans = 1LL * cnt * (cnt - 1) / 2LL;
  for (auto it : req) {
    // printf("at %d %d = %d\\n",it.st, it.nd, qs[it.st][it.nd]);
    ans += qs[it.st][it.nd];
  }
  printf("%lld", ans);
}