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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
// #pragma optimize GCC("O3")
typedef long long ll;
typedef long double ld;
using namespace std;

const int abc = 26;
ll answ = 0;
class trie {
  class node {
  public:
    vector<int> son;
    vector<int> f;
    node() {
      son.resize(abc, -1);
      f.resize(abc, 0);
      end = false;
    }
    bool end;
  };

public:
  trie() { v.resize(1); }
  vector<node> v; // aby sme nemuseli mat smerniky :P
  void pridaj(int nd, const string &s, int i) {
    if (i == s.size()) {
      v[nd].end = true;
      return;
    }
    if (v[nd].son[s[i] - 'a'] == -1) {
      v.resize(v.size() + 1);
      v[nd].son[s[i] - 'a'] = v.size() - 1;
    }
    pridaj(v[nd].son[s[i] - 'a'], s, i + 1);
  }
  int set_f(int nd)
  // kolko celkove slov tam mame
  {
    int sum = 0;
    for (int i = 0; i < abc; i++) {
      if (v[nd].son[i] != -1) {
        int tmp = set_f(v[nd].son[i]);
        v[nd].f[i] += tmp;
        sum += tmp;
        for (int j = 0; j < abc; j++)
          if (i != j)
            v[nd].f[j] += v[v[nd].son[i]].f[j];
      }
    }
    if (v[nd].end)
      sum++;
    return sum;
  }
  int answer_word(int nd, const string &s, int i) {
    if (i == s.size() - 1)
      return v[nd].f[s[i] - 'a'] - 1;
    return answer_word(v[nd].son[s[i] - 'a'], s, i + 1);
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // Urobime si trie reverznutych slov
  // V kazdom vrchole si budeme pamatat, ci tu konci nejake
  // slovo, a ak ano, kolko slov je pod nim, a ze kolko slov obsahujucich
  // pismeno x v zvysku je pod nim.
  int n;
  cin >> n;
  vector<string> v(n);
  trie t;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    reverse(v[i].begin(), v[i].end());
    t.pridaj(0, v[i], 0);
  }
  t.set_f(0);
  for (int i = 0; i < n; i++)
    answ += 1ll * t.answer_word(0, v[i], 0);
  cout << answ << "\\n";
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::invalid_argument'
  what():  stoll


Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
// #pragma optimize GCC("O3")
typedef long long ll;
typedef long double ld;
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  // kazde cislo zo vstupu sa da zapisat
  // ako x/(2^a*5^b)
  // Kazde x mozme upravit do takehoto tvaru pomocou gcd.
  // Nech b[i] = 10^9 * a[i]. potom a[i] = b[i]/(2^9*5^9)
  // Zaujimaju nas len mocniny 2 a 5 v b.
  vector<pair<ll, ll>> a(n);
  multiset<pair<ll, ll>> m;
  const ll c = 9;
  ll good = 0;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int b = -1;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == '.') {
        b = j;
        break;
      }
    }
    if (b == -1) {
      s.push_back(b);
      b = s.size() - 1;
    }
    s.resize(b + 10, '0');
    s.erase(s.begin() + b);
    ll num = stoll(s);
    // cout << num << endl;
    a[i].first = -c;
    a[i].second = -c;
    while (num % 2 == 0) {
      num /= 2;
      a[i].first++;
    }
    while (num % 5 == 0) {
      num /= 5;
      a[i].second++;
    }
    a[i].first = min(a[i].first, c);   // viac ako 9 nebudeme potrebovat
    a[i].second = min(a[i].second, c); // viac ako 9 nebudeme potrebovat
    for (int c2 = -c; c2 <= c; c2++) {
      for (int c5 = -c; c5 <= c; c5++) {
        if (a[i].first + c2 >= 0 && a[i].second + c5 >= 0) {
          good += m.count(make_pair(c2, c5));
        }
      }
    }
    m.insert(a[i]);
  }
  cout << good << "\\n";
  return 0;
}