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

// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,avx512f")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <immintrin.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#ifdef DEBUG
#include "./Lib/Timer.hpp"
#include "./Lib/debug_VC.hpp"
#include "./Lib/sample.hpp"
#else
#define dump(...)
#endif

/* macro */
#define FOR(i, b, e) for (ll i = (ll)(b); i < (ll)(e); ++i)
#define RFOR(i, b, e) for (ll i = (ll)(e - 1); i >= (ll)(b); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define REPC(x, c) for (const auto &x : (c))
#define REPI2(it, b, e) for (auto it = (b); it != (e); ++it)
#define REPI(it, c) REPI2(it, (c).begin(), (c).end())
#define RREPI(it, c) REPI2(it, (c).rbegin(), (c).rend())
#define REPI_ERACE2(it, b, e) for (auto it = (b); it != (e);)
#define REPI_ERACE(it, c) REPI_ERACE2(it, (c).begin(), (c).end())
#define ALL(x) (x).begin(), (x).end()
#define cauto const auto &
/* macro func */
template <class T> inline auto sort(T &t) { std::sort(ALL(t)); }
template <class T> inline auto rsort(T &t) {
  std::sort((t).rbegin(), (t).rend());
}
template <class T> inline auto unique(T &t) {
  (t).erase(unique((t).begin(), (t).end()), (t).end());
}
template <class T, class S> inline auto chmax(T &t, const S &s) {
  if (s > t) {
    t = s;
    return true;
  }
  return false;
}
template <class T, class S> inline auto chmin(T &t, const S &s) {
  if (s < t) {
    t = s;
    return true;
  }
  return false;
}
inline auto BR() { std::cout << "\\n"; }

/* type define */
using ll = long long;
using PAIR = std::pair<ll, ll>;
using VS = std::vector<std::string>;
using VL = std::vector<long long>;
using VVL = std::vector<VL>;
using VVVL = std::vector<VVL>;
using VD = std::vector<double>;
template <class T> using V = std::vector<T>;

/* using std */
using std::cout;
constexpr char endl = '\\n';
using std::bitset;
using std::cin;
using std::list;
using std::map;
using std::multimap;
using std::multiset;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::unordered_map;
using std::unordered_multimap;
using std::unordered_multiset;
using std::unordered_set;
using std::vector;

/* Initial processing  */
struct Preprocessing {
  Preprocessing() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(0);
  };
} _Preprocessing;

/* Remove the source of the bug */
signed pow(signed, signed) {
  assert(false);
  return -1;
}

/* define hash */
namespace std {
template <> class hash<std::pair<ll, ll>> {
public:
  size_t operator()(const std::pair<ll, ll> &x) const {
    return hash<ll>()(1000000000 * x.first + x.second);
  }
};
} // namespace std

/* input */
template <class T> std::istream &operator>>(std::istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

/* constant value */
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;

//=============================================================================================;

std::unordered_map<ll, ll> prime_decomposition(ll n) {
  /*
  ll i = 2;
  std::unordered_map<ll, ll> table{};
  while (i * i <= n) {
          while (n % i == 0) {
                  n /= i;
                  table[i]++;
          }++i;
  }
  if (n > 1) {
          table[n]++;
          return table;
  }
  return table;*/
  unordered_map<ll, ll> mp;
  while (n % 2 == 0) {
    mp[2]++;
    n /= 2;
  }
  while (n % 5 == 0) {
    mp[5]++;
    n /= 5;
  }
  return mp;
}

class CumulativeSum2D {
private:
  const ll h;
  const ll w;
  std::vector<std::vector<ll>> sumList;

public:
  CumulativeSum2D() = delete;
  CumulativeSum2D(const std::vector<std::vector<ll>> &v)
      : h(v.size()), w(v[0].size()), sumList(h + 1, std::vector<ll>(w + 1)) {
    for (ll i = 0; i < h; ++i)
      for (ll j = 0; j < w; ++j) {
        sumList[i + 1][j + 1] = v[i][j];
      }
    for (ll i = 0; i < h; ++i)
      for (ll j = 0; j < w + 1; ++j) {
        sumList[i + 1][j] += sumList[i][j];
      }
    for (ll i = 0; i < h + 1; ++i)
      for (ll j = 0; j < w; ++j) {
        sumList[i][j + 1] += sumList[i][j];
      }
  }
  ll get(ll y, ll x) { return sumList[y + 1][x + 1]; }
  ll get(ll y1, ll x1, ll y2, ll x2) {
    if (y2 < y1 || x2 < x1) {
      return 0LL;
    }
    x1 = std::max(x1, 0LL);
    y1 = std::max(y1, 0LL);
    y2 = std::min(y2, h - 1);
    x2 = std::min(x2, w - 1);
    return sumList[y2 + 1][x2 + 1] + sumList[y1][x1] - sumList[y2 + 1][x1] -
           sumList[y1][x2 + 1];
  }
};

signed main() {
  ll n;
  cin >> n;
  VS v(n);
  cin >> v;

  constexpr ll mx2 = 45;
  constexpr ll mx5 = 20;
  constexpr ll mxd = 20;
  VVL w(mx2, VL(mx5));
  VVL d2(mxd, VL(mx2)), d5(mxd, VL(mx5));
  VL d0(mxd);

  REPC(x, v) {
    bool flg = false;
    ll d = 0;
    ll val = 0;
    REPC(c, x) {
      if (c == '.') {
        flg = true;
        continue;
      }
      if (flg) {
        ++d;
      }
      val *= 10;
      val += (c - '0');
    }
    auto dc = prime_decomposition(val);
    auto f2 = dc.find(2) != dc.end();
    auto f5 = dc.find(5) != dc.end();
    if (f2 && f5) {
      ll mn = std::min(std::min(dc[2], dc[5]), d);
      d -= mn;
      dc[2] -= mn;
      dc[5] -= mn;
      if (dc[2] == 0) {
        f2 = false;
      }
      if (dc[5] == 0) {
        f5 = false;
      }
    }

    if (d == 0) {
      ll mn = std::min(dc[2], dc[5]);
      ++w[dc[2]][dc[5]];
    } else {
      if (f2) {
        ++d2[d][dc[2]];
      } else if (f5) {
        ++d5[d][dc[5]];
      } else {
        ++d0[d];
      }
    }
  }
  auto sum = CumulativeSum2D(w);
  ll ans = 0;
  REP(i, mx2) REP(j, mx5) {
    ans += w[i][j] * (w[i][j] - 1) / 2;
    FOR(l, j + 1, mx5) { ans += w[i][j] * w[i][l]; }
    ans += w[i][j] * sum.get(i + 1, 0, mx2 + 1, mx5 + 1);

    REP(k, mxd) REP(l, mx2) {
      // 2:i, 5:j d:k
      ll mn = std::min(i, j);
      ll d = k - mn;
      if (d <= 0) {
        ans += w[i][j] * d2[k][l];
        continue;
      }
      if (l < d) {
        continue;
      }
      if ((j - mn) >= d) {
        ans += w[i][j] * d2[k][l];
      }
    }
    REP(k, mxd) REP(l, mx5) {
      // 2:i, 5:j d:k
      ll mn = std::min(i, j);
      ll d = k - mn;
      if (d <= 0) {
        ans += w[i][j] * d5[k][l];
        continue;
      }
      if (l < d) {
        continue;
      }
      if ((i - mn) >= d) {
        ans += w[i][j] * d5[k][l];
      }
    }
    REP(k, mxd) {
      ll mn = std::min(i, j);
      ll d = k - mn;
      if (d <= 0) {
        ans += w[i][j] * d0[k];
      }
    }
  }
  REP(i, mxd) REP(j, mx2) REP(k, mxd) REP(l, mx5) {
    ll d = i + k;
    ll u = std::min(j, l);
    if (u >= d) {
      ans += d2[i][j] * d5[k][l];
    }
  }

  cout << ans << endl;
  // dump(w, d2, d5, d0);
}


Error: 0

Fixed submission:

// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,avx512f")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <immintrin.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#ifdef DEBUG
#include "./Lib/Timer.hpp"
#include "./Lib/debug_VC.hpp"
#include "./Lib/sample.hpp"
#else
#define dump(...)
#endif

/* macro */
#define FOR(i, b, e) for (ll i = (ll)(b); i < (ll)(e); ++i)
#define RFOR(i, b, e) for (ll i = (ll)(e - 1); i >= (ll)(b); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define REPC(x, c) for (const auto &x : (c))
#define REPI2(it, b, e) for (auto it = (b); it != (e); ++it)
#define REPI(it, c) REPI2(it, (c).begin(), (c).end())
#define RREPI(it, c) REPI2(it, (c).rbegin(), (c).rend())
#define REPI_ERACE2(it, b, e) for (auto it = (b); it != (e);)
#define REPI_ERACE(it, c) REPI_ERACE2(it, (c).begin(), (c).end())
#define ALL(x) (x).begin(), (x).end()
#define cauto const auto &
/* macro func */
template <class T> inline auto sort(T &t) { std::sort(ALL(t)); }
template <class T> inline auto rsort(T &t) {
  std::sort((t).rbegin(), (t).rend());
}
template <class T> inline auto unique(T &t) {
  (t).erase(unique((t).begin(), (t).end()), (t).end());
}
template <class T, class S> inline auto chmax(T &t, const S &s) {
  if (s > t) {
    t = s;
    return true;
  }
  return false;
}
template <class T, class S> inline auto chmin(T &t, const S &s) {
  if (s < t) {
    t = s;
    return true;
  }
  return false;
}
inline auto BR() { std::cout << "\\n"; }

/* type define */
using ll = long long;
using PAIR = std::pair<ll, ll>;
using VS = std::vector<std::string>;
using VL = std::vector<long long>;
using VVL = std::vector<VL>;
using VVVL = std::vector<VVL>;
using VD = std::vector<double>;
template <class T> using V = std::vector<T>;

/* using std */
using std::cout;
constexpr char endl = '\\n';
using std::bitset;
using std::cin;
using std::list;
using std::map;
using std::multimap;
using std::multiset;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::unordered_map;
using std::unordered_multimap;
using std::unordered_multiset;
using std::unordered_set;
using std::vector;

/* Initial processing  */
struct Preprocessing {
  Preprocessing() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(0);
  };
} _Preprocessing;

/* Remove the source of the bug */
signed pow(signed, signed) {
  assert(false);
  return -1;
}

/* define hash */
namespace std {
template <> class hash<std::pair<ll, ll>> {
public:
  size_t operator()(const std::pair<ll, ll> &x) const {
    return hash<ll>()(1000000000 * x.first + x.second);
  }
};
} // namespace std

/* input */
template <class T> std::istream &operator>>(std::istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

/* constant value */
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;

//=============================================================================================;

std::unordered_map<ll, ll> prime_decomposition(ll n) {
  ll i = 2;
  std::unordered_map<ll, ll> table{};
  while (i * i <= n) {
    while (n % i == 0) {
      n /= i;
      table[i]++;
    }
    ++i;
  }
  if (n > 1) {
    table[n]++;
    return table;
  }
  return table;
}

class CumulativeSum2D {
private:
  const ll h;
  const ll w;
  std::vector<std::vector<ll>> sumList;

public:
  CumulativeSum2D() = delete;
  CumulativeSum2D(const std::vector<std::vector<ll>> &v)
      : h(v.size()), w(v[0].size()), sumList(h + 1, std::vector<ll>(w + 1)) {
    for (ll i = 0; i < h; ++i)
      for (ll j = 0; j < w; ++j) {
        sumList[i + 1][j + 1] = v[i][j];
      }
    for (ll i = 0; i < h; ++i)
      for (ll j = 0; j < w + 1; ++j) {
        sumList[i + 1][j] += sumList[i][j];
      }
    for (ll i = 0; i < h + 1; ++i)
      for (ll j = 0; j < w; ++j) {
        sumList[i][j + 1] += sumList[i][j];
      }
  }
  ll get(ll y, ll x) { return sumList[y + 1][x + 1]; }
  ll get(ll y1, ll x1, ll y2, ll x2) {
    if (y2 < y1 || x2 < x1) {
      return 0LL;
    }
    x1 = std::max(x1, 0LL);
    y1 = std::max(y1, 0LL);
    y2 = std::min(y2, h - 1);
    x2 = std::min(x2, w - 1);
    return sumList[y2 + 1][x2 + 1] + sumList[y1][x1] - sumList[y2 + 1][x1] -
           sumList[y1][x2 + 1];
  }
};

signed main() {
  ll n;
  cin >> n;
  VS v(n);
  cin >> v;

  constexpr ll mx2 = 45;
  constexpr ll mx5 = 15;
  constexpr ll mxd = 15;
  VVL w(mx2, VL(mx5));
  VVL d2(mxd, VL(mx2)), d5(mxd, VL(mx5));
  VL d0(mxd);

  REPC(x, v) {
    bool flg = false;
    ll d = 0;
    ll val = 0;
    REPC(c, x) {
      if (c == '.') {
        flg = true;
        continue;
      }
      if (flg) {
        ++d;
      }
      val *= 10;
      val += (c - '0');
    }
    auto dc = prime_decomposition(val);
    auto f2 = dc.find(2) != dc.end();
    auto f5 = dc.find(5) != dc.end();
    if (f2 && f5) {
      ll mn = std::min(std::min(dc[2], dc[5]), d);
      d -= mn;
      dc[2] -= mn;
      dc[5] -= mn;
      if (dc[2] == 0) {
        f2 = false;
      }
      if (dc[5] == 0) {
        f5 = false;
      }
    }

    if (d == 0) {
      ll mn = std::min(dc[2], dc[5]);
      ++w[dc[2]][dc[5]];
    } else {
      if (f2) {
        ++d2[d][dc[2]];
      } else if (f5) {
        ++d5[d][dc[5]];
      } else {
        ++d0[d];
      }
    }
  }
  auto sum = CumulativeSum2D(w);
  ll ans = 0;
  REP(i, mx2) REP(j, mx5) {
    ans += w[i][j] * (w[i][j] - 1) / 2;
    FOR(l, j + 1, mx5) { ans += w[i][j] * w[i][l]; }
    ans += w[i][j] * sum.get(i + 1, 0, mx2 + 1, mx5 + 1);

    REP(k, mxd) REP(l, mx2) {
      // 2:i, 5:j d:k
      ll mn = std::min(i, j);
      ll d = k - mn;
      if (d <= 0) {
        ans += w[i][j] * d2[k][l];
        continue;
      }
      if (l < d) {
        continue;
      }
      if ((j - mn) >= d) {
        ans += w[i][j] * d2[k][l];
      }
    }
    REP(k, mxd) REP(l, mx5) {
      // 2:i, 5:j d:k
      ll mn = std::min(i, j);
      ll d = k - mn;
      if (d <= 0) {
        ans += w[i][j] * d5[k][l];
        continue;
      }
      if (l < d) {
        continue;
      }
      if ((i - mn) >= d) {
        ans += w[i][j] * d5[k][l];
      }
    }
    REP(k, mxd) {
      ll mn = std::min(i, j);
      ll d = k - mn;
      if (d <= 0) {
        ans += w[i][j] * d0[k];
      }
    }
  }
  REP(i, mxd) REP(j, mx2) REP(k, mxd) REP(l, mx5) {
    ll d = i + k;
    ll u = std::min(j, l);
    if (u >= d) {
      ans += d2[i][j] * d5[k][l];
    }
  }

  cout << ans << endl;
  // dump(w, d2, d5, d0);
}