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

#include <bits/stdc++.h>
// #define int ll
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(a) (a).begin(), (a).end()
#define eb emplace_back

using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
using ILP = pair<int, ll>;
using LIP = pair<ll, int>;
using DP = pair<double, double>;

constexpr int MOD = 1000000007;
constexpr int INF = 1000000000;
constexpr ll LINF = 1000000000000000000;
constexpr int dx[] = {1, 0, -1, 0};
constexpr int dy[] = {0, 1, 0, -1};
constexpr int ddx[] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr int ddy[] = {0, 1, 1, 1, 0, -1, -1, -1};

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i)
      os << ' ';
    os << v.at(i);
  }
  return os;
}

template <typename T> vector<T> &vecin(vector<T> &v, int n) {
  for (int i = 0; i < n; i++) {
    cin >> v.at(i);
  }
  return v;
}

template <typename T> void ln(T a) { cout << a << '\\n'; }

void Yes(bool b) { ln(b ? "Yes" : "No"); }

int extgcd(int a, int b, int &x, int &y) {
  int d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

int inv(int a, int mod) {
  int x, y;
  extgcd(a, mod, x, y);
  return (x + mod) % mod;
}

template <typename T> T free_pow(T x, int n) {
  T res = T(1);
  while (n > 0) {
    if (n & 1)
      res *= x;
    x *= x;
    n >>= 1;
  }
  return res;
}

template <int mod> struct modint {
  int val;

  modint() { val = 0; }
  modint(int _val) { val = ((_val % mod) + mod) % mod; }

  bool operator==(const modint &rhs) const { return val == rhs.val; }
  bool operator<(const modint &rhs) const { return val < rhs.val; }
  modint operator+(const modint &rhs) const {
    return modint<mod>(val + rhs.val);
  }
  modint operator-(const modint &rhs) const {
    return modint<mod>(val - rhs.val);
  }
  modint operator*(const modint &rhs) const {
    return modint<mod>((int)((ll)val * rhs.val % mod));
  }
  modint operator/(const modint &rhs) const {
    return modint<mod>((int)((ll)val * inv(rhs.val, mod) % mod));
  }
  modint &operator+=(const modint &rhs) {
    val = (val + rhs.val) % mod;
    return *this;
  }
  modint &operator-=(const modint &rhs) {
    val = (val - rhs.val + mod) % mod;
    return *this;
  }
  modint &operator*=(const modint &rhs) {
    val = (int)(((ll)val * rhs.val) % mod);
    return *this;
  }
  modint &operator/=(const modint &rhs) {
    val = (int)((ll)val * inv(rhs.val, mod) % mod);
    return *this;
  }
  modint &operator++() {
    val = (val + 1) % mod;
    return *this;
  }
  modint operator++(signed) {
    modint<mod> m = *this;
    val = (val + 1) % mod;
    return m;
  }
  modint &operator--() {
    val = (val + mod - 1) % mod;
    return *this;
  }
  modint operator--(signed) {
    modint<mod> m = *this;
    val = (val + mod - 1) % mod;
    return m;
  }
};

struct UniTree {
  vector<int> par, rank, sz;

  UniTree(int n) {
    par.resize(n);
    rank.resize(n, 0);
    sz.resize(n, 1);
    for (int i = 0; i < n; i++) {
      par.at(i) = i;
    }
  }

  int root(int x) {
    if (par.at(x) == x)
      return x;
    else
      return par.at(x) = root(par.at(x));
  }

  void merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (rank.at(x) < rank.at(y)) {
      par.at(x) = y;
      sz.at(y) += sz.at(x);
    } else {
      par.at(y) = x;
      if (rank.at(x) == rank.at(y))
        rank.at(x)++;
      sz.at(x) += sz.at(y);
    }
  }

  bool same(int x, int y) { return root(x) == root(y); }

  int siz(int x) { return sz.at(root(x)); }
};

template <typename T> struct Vect {
  T x, y;
  Vect() {
    x = 0;
    y = 0;
  }
  Vect(T x, T y) {
    this->x = x;
    this->y = y;
  }

  int posi() const {
    if (x == 0 && y == 0)
      exit(1);
    if (x > 0 && y == 0)
      return 0;
    if (x > 0 && y > 0)
      return 1;
    if (x == 0 && y > 0)
      return 2;
    if (x < 0 && y > 0)
      return 3;
    if (x < 0 && y == 0)
      return 4;
    if (x < 0 && y < 0)
      return 5;
    if (x == 0 && y < 0)
      return 6;
    if (x > 0 && y < 0)
      return 7;
    return -1;
  }

  bool operator<(const Vect &rhs) {
    int p1 = posi();
    int p2 = rhs.posi();
    if (p1 != p2)
      return p1 < p2;
    return rhs.x * y < x * rhs.y;
  }
};

template <typename T> struct SegTree {
  using Func = function<T(T, T)>;
  int n;
  vector<T> dat;
  Func func;
  // T low;

  SegTree(vector<T> a, Func _func) {
    int sz = a.size();
    n = 1;
    // low = _low;
    func = _func;
    while (n < sz)
      n *= 2;
    set<int> u;
    dat.resize(n * 2 - 1, u);
    rep(i, sz) { dat[i + n - 1] = a[i]; }
    for (int i = n - 2; i >= 0; i--) {
      dat[i] = func(dat[i * 2 + 1], dat[i * 2 + 2]);
    }
  }

  void update(int k, T val) {
    k += n - 1;
    dat[k] = val;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = func(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  T _query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) {
      set<int> u;
      return u;
    }
    if (a <= l && r <= b)
      return dat[k];
    T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2);
    T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
    return func(vl, vr);
  }

  T query(int a, int b) { return _query(a, b, 0, 0, n); }
};

set<int> que(set<int> lval, set<int> rval) {
  set<int> ret(lval);
  for (int x : rval) {
    ret.emplace(x);
  }
  return ret;
}

signed main() {
  int n;
  cin >> n;
  vector<int> twos(n, 0);
  vector<int> fives(n, 0);
  rep(i, n) {
    string s;
    cin >> s;
    int ind = s.find(".");
    ll num = -1;
    if (ind == string::npos) {
      num = atol(s.c_str()) * 1e9;
    } else {
      long under = atol(s.substr(ind + 1).c_str());
      rep(j, 10 - s.size() + ind) { under *= 10; }
      num = atol(s.substr(0, ind).c_str()) * 1e9 + under;
    }
    // printf("i=%d,num=%lld\\n", i, num);
    while (num % 2 == 0) {
      twos.at(i)++;
      num /= 2;
    }
    while (num % 5 == 0) {
      fives.at(i)++;
      num /= 5;
    }
    // printf("i=%d,two=%d,five=%d\\n", i, twos.at(i), fives.at(i));
  }

  vector<vector<int>> a(99, vector<int>(99, 0));
  rep(i, n) { a.at(twos.at(i)).at(fives.at(i))++; }

  vector<vector<int>> s(100, vector<int>(100, 0));
  rep(i, 99) {
    rep(j, 99) {
      s.at(i + 1).at(j + 1) =
          s.at(i).at(j + 1) + s.at(i + 1).at(j) - s.at(i).at(j) + a.at(i).at(j);
    }
  }

  ll ans = 0;
  rep(i, n) {
    int needtwo = 18 - twos.at(i);
    if (needtwo < 0)
      needtwo = 0;
    int needfive = 18 - fives.at(i);
    if (needfive < 0)
      needfive = 0;
    ll tmp = s.at(99).at(99) - s.at(needtwo).at(99) - s.at(99).at(needfive) +
             s.at(needtwo).at(needfive);
    if (twos.at(i) >= 9 && fives.at(i) >= 9)
      tmp--;
    // printf("i=%d,tmp=%lld\\n", i, tmp);
    ans += tmp;
  }
  ln(ans / 2);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #define int ll
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(a) (a).begin(), (a).end()
#define eb emplace_back

using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
using ILP = pair<int, ll>;
using LIP = pair<ll, int>;
using DP = pair<double, double>;

constexpr int MOD = 1000000007;
constexpr int INF = 1000000000;
constexpr ll LINF = 1000000000000000000;
constexpr int dx[] = {1, 0, -1, 0};
constexpr int dy[] = {0, 1, 0, -1};
constexpr int ddx[] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr int ddy[] = {0, 1, 1, 1, 0, -1, -1, -1};

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i)
      os << ' ';
    os << v.at(i);
  }
  return os;
}

template <typename T> vector<T> &vecin(vector<T> &v, int n) {
  for (int i = 0; i < n; i++) {
    cin >> v.at(i);
  }
  return v;
}

template <typename T> void ln(T a) { cout << a << '\\n'; }

void Yes(bool b) { ln(b ? "Yes" : "No"); }

int extgcd(int a, int b, int &x, int &y) {
  int d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

int inv(int a, int mod) {
  int x, y;
  extgcd(a, mod, x, y);
  return (x + mod) % mod;
}

template <typename T> T free_pow(T x, int n) {
  T res = T(1);
  while (n > 0) {
    if (n & 1)
      res *= x;
    x *= x;
    n >>= 1;
  }
  return res;
}

template <int mod> struct modint {
  int val;

  modint() { val = 0; }
  modint(int _val) { val = ((_val % mod) + mod) % mod; }

  bool operator==(const modint &rhs) const { return val == rhs.val; }
  bool operator<(const modint &rhs) const { return val < rhs.val; }
  modint operator+(const modint &rhs) const {
    return modint<mod>(val + rhs.val);
  }
  modint operator-(const modint &rhs) const {
    return modint<mod>(val - rhs.val);
  }
  modint operator*(const modint &rhs) const {
    return modint<mod>((int)((ll)val * rhs.val % mod));
  }
  modint operator/(const modint &rhs) const {
    return modint<mod>((int)((ll)val * inv(rhs.val, mod) % mod));
  }
  modint &operator+=(const modint &rhs) {
    val = (val + rhs.val) % mod;
    return *this;
  }
  modint &operator-=(const modint &rhs) {
    val = (val - rhs.val + mod) % mod;
    return *this;
  }
  modint &operator*=(const modint &rhs) {
    val = (int)(((ll)val * rhs.val) % mod);
    return *this;
  }
  modint &operator/=(const modint &rhs) {
    val = (int)((ll)val * inv(rhs.val, mod) % mod);
    return *this;
  }
  modint &operator++() {
    val = (val + 1) % mod;
    return *this;
  }
  modint operator++(signed) {
    modint<mod> m = *this;
    val = (val + 1) % mod;
    return m;
  }
  modint &operator--() {
    val = (val + mod - 1) % mod;
    return *this;
  }
  modint operator--(signed) {
    modint<mod> m = *this;
    val = (val + mod - 1) % mod;
    return m;
  }
};

struct UniTree {
  vector<int> par, rank, sz;

  UniTree(int n) {
    par.resize(n);
    rank.resize(n, 0);
    sz.resize(n, 1);
    for (int i = 0; i < n; i++) {
      par.at(i) = i;
    }
  }

  int root(int x) {
    if (par.at(x) == x)
      return x;
    else
      return par.at(x) = root(par.at(x));
  }

  void merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (rank.at(x) < rank.at(y)) {
      par.at(x) = y;
      sz.at(y) += sz.at(x);
    } else {
      par.at(y) = x;
      if (rank.at(x) == rank.at(y))
        rank.at(x)++;
      sz.at(x) += sz.at(y);
    }
  }

  bool same(int x, int y) { return root(x) == root(y); }

  int siz(int x) { return sz.at(root(x)); }
};

template <typename T> struct Vect {
  T x, y;
  Vect() {
    x = 0;
    y = 0;
  }
  Vect(T x, T y) {
    this->x = x;
    this->y = y;
  }

  int posi() const {
    if (x == 0 && y == 0)
      exit(1);
    if (x > 0 && y == 0)
      return 0;
    if (x > 0 && y > 0)
      return 1;
    if (x == 0 && y > 0)
      return 2;
    if (x < 0 && y > 0)
      return 3;
    if (x < 0 && y == 0)
      return 4;
    if (x < 0 && y < 0)
      return 5;
    if (x == 0 && y < 0)
      return 6;
    if (x > 0 && y < 0)
      return 7;
    return -1;
  }

  bool operator<(const Vect &rhs) {
    int p1 = posi();
    int p2 = rhs.posi();
    if (p1 != p2)
      return p1 < p2;
    return rhs.x * y < x * rhs.y;
  }
};

template <typename T> struct SegTree {
  using Func = function<T(T, T)>;
  int n;
  vector<T> dat;
  Func func;
  // T low;

  SegTree(vector<T> a, Func _func) {
    int sz = a.size();
    n = 1;
    // low = _low;
    func = _func;
    while (n < sz)
      n *= 2;
    set<int> u;
    dat.resize(n * 2 - 1, u);
    rep(i, sz) { dat[i + n - 1] = a[i]; }
    for (int i = n - 2; i >= 0; i--) {
      dat[i] = func(dat[i * 2 + 1], dat[i * 2 + 2]);
    }
  }

  void update(int k, T val) {
    k += n - 1;
    dat[k] = val;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = func(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  T _query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) {
      set<int> u;
      return u;
    }
    if (a <= l && r <= b)
      return dat[k];
    T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2);
    T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
    return func(vl, vr);
  }

  T query(int a, int b) { return _query(a, b, 0, 0, n); }
};

set<int> que(set<int> lval, set<int> rval) {
  set<int> ret(lval);
  for (int x : rval) {
    ret.emplace(x);
  }
  return ret;
}

signed main() {
  int n;
  cin >> n;
  vector<int> twos(n, 0);
  vector<int> fives(n, 0);
  rep(i, n) {
    string s;
    cin >> s;
    int ind = s.find(".");
    ll num = -1;
    if (ind == string::npos) {
      num = atol(s.c_str()) * 1e9;
    } else {
      long under = atol(s.substr(ind + 1).c_str());
      rep(j, 10 - s.size() + ind) { under *= 10; }
      num = atol(s.substr(0, ind).c_str()) * 1e9 + under;
    }
    // printf("i=%d,num=%lld\\n", i, num);
    while (num % 2 == 0) {
      twos.at(i)++;
      num /= 2;
    }
    while (num % 5 == 0) {
      fives.at(i)++;
      num /= 5;
    }
    // printf("i=%d,two=%d,five=%d\\n", i, twos.at(i), fives.at(i));
  }

  vector<vector<int>> a(99, vector<int>(99, 0));
  rep(i, n) { a.at(twos.at(i)).at(fives.at(i))++; }

  vector<vector<int>> s(100, vector<int>(100, 0));
  rep(i, 99) {
    rep(j, 99) {
      s.at(i + 1).at(j + 1) =
          s.at(i).at(j + 1) + s.at(i + 1).at(j) - s.at(i).at(j) + a.at(i).at(j);
    }
  }

  ll ans = 0;
  rep(i, n) {
    int needtwo = 18 - twos.at(i);
    int needfive = 18 - fives.at(i);
    ll tmp = s.at(24).at(24) - s.at(needtwo).at(24) - s.at(24).at(needfive) +
             s.at(needtwo).at(needfive);
    if (twos.at(i) >= 9 && fives.at(i) >= 9)
      tmp--;
    // printf("i=%d,tmp=%lld\\n", i, tmp);
    ans += tmp;
  }
  ln(ans / 2);
}