Problem p02856 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <ciso646>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define inf 0x3f3f3f3f3f3f3f3f
#define ALL(a) (a).begin(), (a).end()
#define DEBUG(x) // cerr<<#x<<": "<<x<<endl
#define ll long long
#define ull unsigned long long
using pii = pair<ll, ll>;
#define eps 1e-14
#define SETUP                                                                  \
  cin.tie(0), ios::sync_with_stdio(false),                                     \
      cout << setprecision(15) << std::fixed;
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)

template <class T> using vec2 = std::vector<vector<T>>;

//// bit ////
#ifdef _MSC_VER
#ifdef _WIN32
inline unsigned int __builtin_ctz(unsigned int x) {
  unsigned long r;
  _BitScanForward(&r, x);
  return r;
}
inline unsigned int __builtin_clz(unsigned int x) {
  unsigned long r;
  _BitScanReverse(&r, x);
  return 31 - r;
}
inline unsigned int __builtin_ffs(unsigned int x) {
  unsigned long r;
  return _BitScanForward(&r, x) ? r + 1 : 0;
}
// inline unsigned int __builtin_popcount(unsigned int x){ return __popcnt(x); }
#ifdef _WIN64
inline unsigned long long __builtin_ctzll(unsigned long long x) {
  unsigned long r;
  _BitScanForward64(&r, x);
  return r;
}
inline unsigned long long __builtin_clzll(unsigned long long x) {
  unsigned long r;
  _BitScanReverse64(&r, x);
  return 63 - r;
}
inline unsigned long long __builtin_ffsll(unsigned long long x) {
  unsigned long r;
  return _BitScanForward64(&r, x) ? r + 1 : 0;
}
inline unsigned long long __builtin_popcountll(unsigned long long x) {
  return __popcnt64(x);
}
#else
inline unsigned int hidword(unsigned long long x) {
  return static_cast<unsigned int>(x >> 32);
}
inline unsigned int lodword(unsigned long long x) {
  return static_cast<unsigned int>(x & 0xFFFFFFFF);
}
inline unsigned long long __builtin_ctzll(unsigned long long x) {
  return lodword(x) ? __builtin_ctz(lodword(x))
                    : __builtin_ctz(hidword(x)) + 32;
}
inline unsigned long long __builtin_clzll(unsigned long long x) {
  return hidword(x) ? __builtin_clz(hidword(x))
                    : __builtin_clz(lodword(x)) + 32;
}
inline unsigned long long __builtin_ffsll(unsigned long long x) {
  return lodword(x)   ? __builtin_ffs(lodword(x))
         : hidword(x) ? __builtin_ffs(hidword(x)) + 32
                      : 0;
}
// inline unsigned long long __builtin_popcountll(unsigned long long x) { return
// __builtin_popcount(lodword(x)) + __builtin_popcount(hidword(x)); }
#endif // _WIN64
#endif // _WIN32
#endif // _MSC_VER

namespace {
struct input_returnner {
  ll N;
  input_returnner(ll N_ = 0) : N(N_) {}
  template <typename T> operator vector<T>() const {
    vector<T> res(N);
    for (auto &a : res)
      cin >> a;
    return std::move(res);
  }
  template <typename T> operator T() const {
    T res;
    cin >> res;
    return res;
  }
  template <typename T> T operator-(T right) {
    return T(input_returnner()) - right;
  }
  template <typename T> T operator+(T right) {
    return T(input_returnner()) + right;
  }
  template <typename T> T operator*(T right) {
    return T(input_returnner()) * right;
  }
  template <typename T> T operator/(T right) {
    return T(input_returnner()) / right;
  }
  template <typename T> T operator<<(T right) {
    return T(input_returnner()) << right;
  }
  template <typename T> T operator>>(T right) {
    return T(input_returnner()) >> right;
  }
};
template <typename T> input_returnner in() { return in<T>(); }
input_returnner in() { return input_returnner(); }
input_returnner in(ll N) { return std::move(input_returnner(N)); }
} // namespace

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

template <typename T> struct is_vector : std::false_type {};

template <typename T> struct is_vector<std::vector<T>> : std::true_type {};

template <typename T> constexpr bool is_vector_v = is_vector<T>::value;

template <typename T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  if (!v.empty()) {
    for (int i = 0; i < v.size(); ++i) {
      out << v[i] << (i == v.size() - 1 ? "\n" : (is_vector_v<T> ? "" : ", "));
    }
  }
  return out;
}

namespace std {
// ref:
// https://stackoverflow.com/questions/7110301/generic-hash-for-tuples-in-unordered-map-unordered-set
template <class T> inline void hash_combine(std::size_t &seed, T const &v) {
  seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}

// Recursive template code derived from Matthieu M.
template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
struct HashValueImpl {
  static void apply(size_t &seed, Tuple const &tuple) {
    HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
    hash_combine(seed, std::get<Index>(tuple));
  }
};

template <class Tuple> struct HashValueImpl<Tuple, 0> {
  static void apply(size_t &seed, Tuple const &tuple) {
    hash_combine(seed, std::get<0>(tuple));
  }
};
template <typename... TT> struct hash<std::tuple<TT...>> {
  size_t operator()(std::tuple<TT...> const &tt) const {
    size_t seed = 0;
    HashValueImpl<std::tuple<TT...>>::apply(seed, tt);
    return seed;
  }
};

template <class T, class U> class hash<std::pair<T, U>> {
public:
  size_t operator()(const std::pair<T, U> &x) const {
    return hash<std::tuple<T, U>>()(std::tie(x.first, x.second));
  }
};
} // namespace std

// ref: https://stackoverflow.com/questions/6245735/pretty-print-stdtuple
namespace aux {
template <std::size_t...> struct seq {};

template <std::size_t N, std::size_t... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};

template <std::size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {};

template <class Ch, class Tr, class Tuple, std::size_t... Is>
void print_tuple(std::basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) {
  using swallow = int[];
  (void)swallow{0,
                (void(os << (Is == 0 ? "" : ", ") << std::get<Is>(t)), 0)...};
}
} // namespace aux

template <class Ch, class Tr, class... Args>
auto operator<<(std::basic_ostream<Ch, Tr> &os, std::tuple<Args...> const &t)
    -> std::basic_ostream<Ch, Tr> & {
  os << "(";
  aux::print_tuple(os, t, aux::gen_seq<sizeof...(Args)>());
  return os << ")";
}

template <class S, class T>
std::ostream &operator<<(std::ostream &os, const std::pair<S, T> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}

// ref:
// https://stackoverflow.com/questions/8542591/c11-reverse-range-based-for-loo�Fp
template <typename T> struct reversion_wrapper {
  T &iterable;
};

template <typename T> auto begin(reversion_wrapper<T> w) {
  return std::rbegin(w.iterable);
}

template <typename T> auto end(reversion_wrapper<T> w) {
  return std::rend(w.iterable);
}

template <typename T> reversion_wrapper<T> REV(T &&iterable) {
  return {iterable};
}

template <class T> bool inside(T left, T val, T right) {
  return left <= val and val < right;
}

template <class T> T bitCount(T num) {
  T res = 0;
  while (num > 0) {
    if (num & 1)
      ++res;
    num >>= 1;
  }
  return res;
}

ll MOD = 1e9 + 7;
// ll MOD = 998244353;

void solve();

signed main() {
  SETUP;
#ifdef _DEBUG
  while (true) {
#endif
    solve();
#ifdef _DEBUG
    cout << "-------" << endl;
  }
#endif
#ifdef _DEBUG
  system("pause");
#endif
  return 0;
}

#define int ll

struct mint {
  int64_t value;
  mint() : value(0) {}
  mint(int64_t value_) : value(value_ % MOD) {}
  inline mint &operator+=(const mint &rhs) {
    value += rhs.value;
    if (value >= MOD) {
      value -= MOD;
    }
    return *this;
  }
  inline mint &operator-=(const mint &rhs) {
    value -= rhs.value;
    if (value < 0) {
      value += MOD;
    }
    return *this;
  }
  inline mint &operator*=(const mint &rhs) {
    value *= rhs.value;
    value %= MOD;
    return *this;
  }
  inline mint &operator%=(const mint &rhs) { return *this; }
};

// x^n % mod
// O(log n)
mint mod_pow(mint x, uint64_t n) {
  mint res = 1;
  while (n > 0) {
    if (n & 1)
      res *= x;
    x *= x;
    n >>= 1;
  }
  return res;
}

inline mint operator+(const mint &lhs, const mint &rhs) {
  mint res = lhs;
  res += rhs;
  return res;
}
inline mint operator-(const mint &lhs, const mint &rhs) {
  mint res = lhs;
  res -= rhs;
  return res;
}
inline mint operator*(const mint &lhs, const mint &rhs) {
  mint res = lhs;
  res *= rhs;
  return res;
}
inline mint operator%(const mint &lhs, const uint64_t &rhs) { return lhs; }

ostream &operator<<(ostream &out, mint n) { return out << n.value; }

pii dfs(const vector<pii> &G, int node, vector<bool> memo) {
  if (memo[node]) {
    return {0, 0};
  }
  memo[node] = true;
  pii res = dfs(G, G[node].first, memo);
  res.first += 1;
  res.second += G[node].second;
  return res;
}

pii f(const vector<vector<pii>> &G, const vector<vector<pii>> &loop, int cur,
      int node, int cnt, int depth) {
  if (cnt == 0) {
    return {cur, 0};
  }

  if (depth < 2000) {
    auto res = f(G, loop, G[node][cur].first, node, cnt - 1, depth + 1);
    res.second += G[node][cur].second;
    return res;
  }
  int loopcnt = cnt / loop[node][cur].first;
  int rest = cnt % loopcnt;
  cnt = rest;

  auto res = f(G, loop, cur, node, cnt, 0);

  res.second += loopcnt * loop[node][cur].second;
  return res;
}

void solve() {
  int M;
  cin >> M;
  vector<vector<pii>> G(10, vector<pii>(10));
  vector<pii> dc(M);
  for (auto &a : dc) {
    cin >> a.first >> a.second;
  }

  REP(i, G.size()) {
    REP(j, G.size()) {
      if (i + j >= 10) {
        G[i][j] = {(i + j) - 9, 2};
      } else {
        G[i][j] = {(i + j), 1};
      }
    }
  }

  vector<vector<pii>> loop(10, vector<pii>(10));

  REP(i, G.size()) {
    REP(j, G.size()) {
      vector<bool> memo(10);
      loop[i][j] = dfs(G[i], j, memo);
    }
  }

  int cur = 0;
  int res = 0;
  REP(i, M) {
    int d = dc[i].first;
    int c = dc[i].second;
    auto r = f(G, loop, cur, d, c, 0);
    cur = r.first;
    res += r.second;
  }
  cout << res - 1 << endl;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <ciso646>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define inf 0x3f3f3f3f3f3f3f3f
#define ALL(a) (a).begin(), (a).end()
#define DEBUG(x) // cerr<<#x<<": "<<x<<endl
#define ll long long
#define ull unsigned long long
using pii = pair<ll, ll>;
#define eps 1e-14
#define SETUP                                                                  \
  cin.tie(0), ios::sync_with_stdio(false),                                     \
      cout << setprecision(15) << std::fixed;
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)

template <class T> using vec2 = std::vector<vector<T>>;

//// bit ////
#ifdef _MSC_VER
#ifdef _WIN32
inline unsigned int __builtin_ctz(unsigned int x) {
  unsigned long r;
  _BitScanForward(&r, x);
  return r;
}
inline unsigned int __builtin_clz(unsigned int x) {
  unsigned long r;
  _BitScanReverse(&r, x);
  return 31 - r;
}
inline unsigned int __builtin_ffs(unsigned int x) {
  unsigned long r;
  return _BitScanForward(&r, x) ? r + 1 : 0;
}
// inline unsigned int __builtin_popcount(unsigned int x){ return __popcnt(x); }
#ifdef _WIN64
inline unsigned long long __builtin_ctzll(unsigned long long x) {
  unsigned long r;
  _BitScanForward64(&r, x);
  return r;
}
inline unsigned long long __builtin_clzll(unsigned long long x) {
  unsigned long r;
  _BitScanReverse64(&r, x);
  return 63 - r;
}
inline unsigned long long __builtin_ffsll(unsigned long long x) {
  unsigned long r;
  return _BitScanForward64(&r, x) ? r + 1 : 0;
}
inline unsigned long long __builtin_popcountll(unsigned long long x) {
  return __popcnt64(x);
}
#else
inline unsigned int hidword(unsigned long long x) {
  return static_cast<unsigned int>(x >> 32);
}
inline unsigned int lodword(unsigned long long x) {
  return static_cast<unsigned int>(x & 0xFFFFFFFF);
}
inline unsigned long long __builtin_ctzll(unsigned long long x) {
  return lodword(x) ? __builtin_ctz(lodword(x))
                    : __builtin_ctz(hidword(x)) + 32;
}
inline unsigned long long __builtin_clzll(unsigned long long x) {
  return hidword(x) ? __builtin_clz(hidword(x))
                    : __builtin_clz(lodword(x)) + 32;
}
inline unsigned long long __builtin_ffsll(unsigned long long x) {
  return lodword(x)   ? __builtin_ffs(lodword(x))
         : hidword(x) ? __builtin_ffs(hidword(x)) + 32
                      : 0;
}
// inline unsigned long long __builtin_popcountll(unsigned long long x) { return
// __builtin_popcount(lodword(x)) + __builtin_popcount(hidword(x)); }
#endif // _WIN64
#endif // _WIN32
#endif // _MSC_VER

namespace {
struct input_returnner {
  ll N;
  input_returnner(ll N_ = 0) : N(N_) {}
  template <typename T> operator vector<T>() const {
    vector<T> res(N);
    for (auto &a : res)
      cin >> a;
    return std::move(res);
  }
  template <typename T> operator T() const {
    T res;
    cin >> res;
    return res;
  }
  template <typename T> T operator-(T right) {
    return T(input_returnner()) - right;
  }
  template <typename T> T operator+(T right) {
    return T(input_returnner()) + right;
  }
  template <typename T> T operator*(T right) {
    return T(input_returnner()) * right;
  }
  template <typename T> T operator/(T right) {
    return T(input_returnner()) / right;
  }
  template <typename T> T operator<<(T right) {
    return T(input_returnner()) << right;
  }
  template <typename T> T operator>>(T right) {
    return T(input_returnner()) >> right;
  }
};
template <typename T> input_returnner in() { return in<T>(); }
input_returnner in() { return input_returnner(); }
input_returnner in(ll N) { return std::move(input_returnner(N)); }
} // namespace

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

template <typename T> struct is_vector : std::false_type {};

template <typename T> struct is_vector<std::vector<T>> : std::true_type {};

template <typename T> constexpr bool is_vector_v = is_vector<T>::value;

template <typename T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  if (!v.empty()) {
    for (int i = 0; i < v.size(); ++i) {
      out << v[i] << (i == v.size() - 1 ? "\n" : (is_vector_v<T> ? "" : ", "));
    }
  }
  return out;
}

namespace std {
// ref:
// https://stackoverflow.com/questions/7110301/generic-hash-for-tuples-in-unordered-map-unordered-set
template <class T> inline void hash_combine(std::size_t &seed, T const &v) {
  seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}

// Recursive template code derived from Matthieu M.
template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
struct HashValueImpl {
  static void apply(size_t &seed, Tuple const &tuple) {
    HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
    hash_combine(seed, std::get<Index>(tuple));
  }
};

template <class Tuple> struct HashValueImpl<Tuple, 0> {
  static void apply(size_t &seed, Tuple const &tuple) {
    hash_combine(seed, std::get<0>(tuple));
  }
};
template <typename... TT> struct hash<std::tuple<TT...>> {
  size_t operator()(std::tuple<TT...> const &tt) const {
    size_t seed = 0;
    HashValueImpl<std::tuple<TT...>>::apply(seed, tt);
    return seed;
  }
};

template <class T, class U> class hash<std::pair<T, U>> {
public:
  size_t operator()(const std::pair<T, U> &x) const {
    return hash<std::tuple<T, U>>()(std::tie(x.first, x.second));
  }
};
} // namespace std

// ref: https://stackoverflow.com/questions/6245735/pretty-print-stdtuple
namespace aux {
template <std::size_t...> struct seq {};

template <std::size_t N, std::size_t... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};

template <std::size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {};

template <class Ch, class Tr, class Tuple, std::size_t... Is>
void print_tuple(std::basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) {
  using swallow = int[];
  (void)swallow{0,
                (void(os << (Is == 0 ? "" : ", ") << std::get<Is>(t)), 0)...};
}
} // namespace aux

template <class Ch, class Tr, class... Args>
auto operator<<(std::basic_ostream<Ch, Tr> &os, std::tuple<Args...> const &t)
    -> std::basic_ostream<Ch, Tr> & {
  os << "(";
  aux::print_tuple(os, t, aux::gen_seq<sizeof...(Args)>());
  return os << ")";
}

template <class S, class T>
std::ostream &operator<<(std::ostream &os, const std::pair<S, T> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}

// ref:
// https://stackoverflow.com/questions/8542591/c11-reverse-range-based-for-loo�Fp
template <typename T> struct reversion_wrapper {
  T &iterable;
};

template <typename T> auto begin(reversion_wrapper<T> w) {
  return std::rbegin(w.iterable);
}

template <typename T> auto end(reversion_wrapper<T> w) {
  return std::rend(w.iterable);
}

template <typename T> reversion_wrapper<T> REV(T &&iterable) {
  return {iterable};
}

template <class T> bool inside(T left, T val, T right) {
  return left <= val and val < right;
}

template <class T> T bitCount(T num) {
  T res = 0;
  while (num > 0) {
    if (num & 1)
      ++res;
    num >>= 1;
  }
  return res;
}

ll MOD = 1e9 + 7;
// ll MOD = 998244353;

void solve();

signed main() {
  SETUP;
#ifdef _DEBUG
  while (true) {
#endif
    solve();
#ifdef _DEBUG
    cout << "-------" << endl;
  }
#endif
#ifdef _DEBUG
  system("pause");
#endif
  return 0;
}

#define int ll

struct mint {
  int64_t value;
  mint() : value(0) {}
  mint(int64_t value_) : value(value_ % MOD) {}
  inline mint &operator+=(const mint &rhs) {
    value += rhs.value;
    if (value >= MOD) {
      value -= MOD;
    }
    return *this;
  }
  inline mint &operator-=(const mint &rhs) {
    value -= rhs.value;
    if (value < 0) {
      value += MOD;
    }
    return *this;
  }
  inline mint &operator*=(const mint &rhs) {
    value *= rhs.value;
    value %= MOD;
    return *this;
  }
  inline mint &operator%=(const mint &rhs) { return *this; }
};

// x^n % mod
// O(log n)
mint mod_pow(mint x, uint64_t n) {
  mint res = 1;
  while (n > 0) {
    if (n & 1)
      res *= x;
    x *= x;
    n >>= 1;
  }
  return res;
}

inline mint operator+(const mint &lhs, const mint &rhs) {
  mint res = lhs;
  res += rhs;
  return res;
}
inline mint operator-(const mint &lhs, const mint &rhs) {
  mint res = lhs;
  res -= rhs;
  return res;
}
inline mint operator*(const mint &lhs, const mint &rhs) {
  mint res = lhs;
  res *= rhs;
  return res;
}
inline mint operator%(const mint &lhs, const uint64_t &rhs) { return lhs; }

ostream &operator<<(ostream &out, mint n) { return out << n.value; }

pii dfs(const vector<pii> &G, int node, vector<bool> memo) {
  if (memo[node]) {
    return {0, 0};
  }
  memo[node] = true;
  pii res = dfs(G, G[node].first, memo);
  res.first += 1;
  res.second += G[node].second;
  return res;
}

pii f(const vector<vector<pii>> &G, const vector<vector<pii>> &loop, int cur,
      int node, int cnt, int depth) {
  if (cnt == 0) {
    return {cur, 0};
  }

  if (depth < 20) {
    auto res = f(G, loop, G[node][cur].first, node, cnt - 1, depth + 1);
    res.second += G[node][cur].second;
    return res;
  }
  int loopcnt = cnt / loop[node][cur].first;
  int rest = cnt % loopcnt;
  cnt = rest;

  auto res = f(G, loop, cur, node, cnt, 0);

  res.second += loopcnt * loop[node][cur].second;
  return res;
}

void solve() {
  int M;
  cin >> M;
  vector<vector<pii>> G(10, vector<pii>(10));
  vector<pii> dc(M);
  for (auto &a : dc) {
    cin >> a.first >> a.second;
  }

  REP(i, G.size()) {
    REP(j, G.size()) {
      if (i + j >= 10) {
        G[i][j] = {(i + j) - 9, 2};
      } else {
        G[i][j] = {(i + j), 1};
      }
    }
  }

  vector<vector<pii>> loop(10, vector<pii>(10));

  REP(i, G.size()) {
    REP(j, G.size()) {
      vector<bool> memo(10);
      loop[i][j] = dfs(G[i], j, memo);
    }
  }

  int cur = 0;
  int res = 0;
  REP(i, M) {
    int d = dc[i].first;
    int c = dc[i].second;
    auto r = f(G, loop, cur, d, c, 0);
    cur = r.first;
    res += r.second;
  }
  cout << res - 1 << endl;
}