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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri, x, n) for (int ri = (int)(n - 1); ri >= (int)(x); ri--)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

void fc(string &s, string &res) {
  int index = 0;
  rep(i, s.size()) {
    if (s.at(i) == '.')
      index = i;
  }
  res = s.substr(index + 1);
  if (index != 0) {
    rep2(i, res.size(), 9) { res.push_back('0'); }
    res = s.substr(0, index) + res;
  } else {
    res = s;
    rep(i, 9) res.push_back('0');
  }
}

ll tolonglong(string s) {
  ll res = 0;
  rep(i, s.size()) {
    res *= 10;
    res += s.at(i) - '0';
  }
  return res;
}

void make_p2p5(ll n, int &p2, int &p5) {
  p2 = p5 = 0;
  while (n != 0 && n % 2 == 0) {
    p2++;
    n /= 2;
  }
  while (n != 0 && n % 5 == 0) {
    p5++;
    n /= 5;
  }
}

void update(vector<vector<ll>> &sums, pair<int, int> p) {
  rrep(i, p.first + 1) {
    rrep(j, p.second + 1) { sums.at(i).at(j)++; }
  }
}

int main() {
  int n;
  cin >> n;
  vector<string> v;
  rep(i, n) {
    string s;
    cin >> s;
    string add;
    fc(s, add);
    v.push_back(add);
  }
  vector<ll> vv(v.size());
  rep(i, v.size()) vv.at(i) = tolonglong(v.at(i));

  vector<vector<ll>> sums(55, vector<ll>(25, 0));
  vector<pair<int, int>> p2p5(vv.size());
  rep(i, vv.size()) {
    int p2, p5;
    make_p2p5(vv.at(i), p2, p5);
    p2p5.at(i) = {p2, p5};
  }

  // rrep(i, sums.size()){
  //   rrep(j, sums.at(i).size()){
  //     cerr << sums.at(i).at(j) << " ";
  //     if(i != sums.size()-1 && j != sums.at(i).size()-1) sums.at(i).at(j) -=
  //     sums.at(i+1).at(j+1); if(i != 0) sums.at(i-1).at(j) +=
  //     sums.at(i).at(j); if(j != 0) sums.at(i).at(j-1) += sums.at(i).at(j);
  //   }
  //   cerr << endl;
  // }

  ll ans = 0;
  // int co = 0;
  for (auto p : p2p5) {
    int np2 = 18 - p.first;
    int np5 = 18 - p.second;
    if (np2 < 0)
      np2 = 0;
    if (np5 < 0)
      np5 = 0;
    // cerr << vv.at(co++) << " " << np2 << " " << np5 << " " <<
    // sums.at(np2).at(np5) << endl;
    ans += sums.at(np2).at(np5);
    update(sums, p);
    // rep(i, sums.size()){
    //   rep(j, sums.at(i).size()){
    //     printf("%2d ", sums.at(i).at(j));
    //   }
    //   cerr << endl;
    // }
    // cerr << "----------------------" << endl;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri, x, n) for (int ri = (int)(n - 1); ri >= (int)(x); ri--)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

void fc(string &s, string &res) {
  int index = 0;
  rep(i, s.size()) {
    if (s.at(i) == '.')
      index = i;
  }
  res = s.substr(index + 1);
  if (index != 0) {
    rep2(i, res.size(), 9) { res.push_back('0'); }
    res = s.substr(0, index) + res;
  } else {
    res = s;
    rep(i, 9) res.push_back('0');
  }
}

ll tolonglong(string s) {
  ll res = 0;
  rep(i, s.size()) {
    res *= 10;
    res += s.at(i) - '0';
  }
  return res;
}

void make_p2p5(ll n, int &p2, int &p5) {
  p2 = p5 = 0;
  while (n != 0 && n % 2 == 0) {
    p2++;
    n /= 2;
  }
  while (n != 0 && n % 5 == 0) {
    p5++;
    n /= 5;
  }
}

void update(vector<vector<ll>> &sums, pair<int, int> p) {
  rrep(i, p.first + 1) {
    rrep(j, p.second + 1) { sums.at(i).at(j)++; }
  }
}

int main() {
  int n;
  cin >> n;
  vector<string> v;
  rep(i, n) {
    string s;
    cin >> s;
    string add;
    fc(s, add);
    v.push_back(add);
  }
  vector<ll> vv(v.size());
  rep(i, v.size()) vv.at(i) = tolonglong(v.at(i));

  vector<vector<ll>> sums(60, vector<ll>(30, 0));
  vector<pair<int, int>> p2p5(vv.size());
  rep(i, vv.size()) {
    int p2, p5;
    make_p2p5(vv.at(i), p2, p5);
    p2p5.at(i) = {p2, p5};
  }

  // rrep(i, sums.size()){
  //   rrep(j, sums.at(i).size()){
  //     cerr << sums.at(i).at(j) << " ";
  //     if(i != sums.size()-1 && j != sums.at(i).size()-1) sums.at(i).at(j) -=
  //     sums.at(i+1).at(j+1); if(i != 0) sums.at(i-1).at(j) +=
  //     sums.at(i).at(j); if(j != 0) sums.at(i).at(j-1) += sums.at(i).at(j);
  //   }
  //   cerr << endl;
  // }

  ll ans = 0;
  // int co = 0;
  for (auto p : p2p5) {
    int np2 = 18 - p.first;
    int np5 = 18 - p.second;
    // cerr << vv.at(co++) << " " << np2 << " " << np5 << " " <<
    // sums.at(np2).at(np5) << endl;
    ans += sums.at(np2).at(np5);
    update(sums, p);
    // rep(i, sums.size()){
    //   rep(j, sums.at(i).size()){
    //     printf("%2d ", sums.at(i).at(j));
    //   }
    //   cerr << endl;
    // }
    // cerr << "----------------------" << endl;
  }
  cout << ans << endl;
  return 0;
}