Problem p02962 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <random>
#include <stack>
#include <vector>

using namespace std;

#define int long long
#define uint unsigned int
#define dotimes(i, n) for (int i = 0, i##max__ = (n); i < i##max__; i++)
#define whole(x, f, ...)                                                       \
  ([&](decltype((x)) c) { return (f)(begin(c), end(c), ##__VA_ARGS__); })(x)

int rint() {
  int x;
  scanf("%lld", &x);
  return x;
}

void wint(int x) { printf("%lld\n", x); }

template <typename T> int size(T const &c) {
  return static_cast<int>(c.size());
}
template <typename T> bool maxs(T &a, T const &b) {
  return a < b ? a = b, true : false;
}
template <typename T> bool mins(T &a, T const &b) {
  return a > b ? a = b, true : false;
}
inline int lg(int x) {
  return 63 - __builtin_clzll(static_cast<unsigned int>(x));
}

constexpr uint mask30 = (1ull << 30) - 1, mask31 = (1ull << 31) - 1,
               mask61 = (1ull << 61) - 1;

uint add61(uint x, uint y) {
  uint z = x + y, w = (z >> 61) + (z & mask61);
  return w < mask61 ? w : w - mask61;
}

uint sub61(uint x, uint y) { return x >= y ? x - y : mask61 - (y - x); }

uint mul61(uint x, uint y) {
  uint a = x >> 31, b = x & mask31, c = y >> 31, d = y & mask31,
       z = a * d + b * c, e = z >> 30, f = z & mask30;
  return (2 * a * c + e + (f << 31) + b * d) % mask61;
}

uint pow61(uint x, int n) {
  uint r = 1;
  for (; n; n >>= 1, x = mul61(x, x))
    if (n & 1)
      r = mul61(r, x);
  return r;
}

signed main() {
  constexpr int s_max = 5e5;
  char s[s_max + 1], t[s_max + 1];
  scanf("%s\n%s\n", s, t);
  const int m = strlen(s), n = strlen(t);
  const uint b1 = []() {
    std::random_device rd;
    std::mt19937_64 prng((static_cast<uint>(rd()) << 32) | rd());
    std::uniform_int_distribution<uint> uid(256, mask61 - 2);
    return uid(prng);
  }();
  vector<bool> fs(n);
  {
    uint hs = 0, ht = 0, b = 1;
    for (int i = 0;; i++, b = mul61(b1, b)) {
      ht = add61(ht, mul61(b, t[n - i - 1]));
      hs = add61(hs, mul61(b, s[(n - i - 1) % m]));
      if (i == n - 1)
        break;
    }
    fs[0] = ht == hs;
    dotimes(i, m - 1) {
      hs = add61(mul61(b1, sub61(hs, mul61(b, s[i]))), s[(i + n) % m]);
      fs[i + 1] = ht == hs;
    }
  }
  vector<int> rs(m);
  int r = 0;
  dotimes(i, m) {
    stack<int> path;
    for (int j = i;; j = (j + n) % m) {
      if (rs[j] || !fs[j]) {
        if (rs[j] == -1)
          r = -1;
        else {
          for (int c = rs[j] + 1; !path.empty(); c++, path.pop())
            rs[path.top()] = c;
          maxs(r, rs[i]);
        }
        break;
      }
      rs[j] = -1;
      path.emplace(j);
    }
    if (r < 0)
      break;
  }
  wint(r);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <random>
#include <stack>
#include <vector>

using namespace std;

#define int long long
#define uint unsigned int
#define dotimes(i, n) for (int i = 0, i##max__ = (n); i < i##max__; i++)
#define whole(x, f, ...)                                                       \
  ([&](decltype((x)) c) { return (f)(begin(c), end(c), ##__VA_ARGS__); })(x)

int rint() {
  int x;
  scanf("%lld", &x);
  return x;
}

void wint(int x) { printf("%lld\n", x); }

template <typename T> int size(T const &c) {
  return static_cast<int>(c.size());
}
template <typename T> bool maxs(T &a, T const &b) {
  return a < b ? a = b, true : false;
}
template <typename T> bool mins(T &a, T const &b) {
  return a > b ? a = b, true : false;
}
inline int lg(int x) {
  return 63 - __builtin_clzll(static_cast<unsigned int>(x));
}

constexpr uint mask30 = (1ull << 30) - 1, mask31 = (1ull << 31) - 1,
               mask61 = (1ull << 61) - 1;

uint add61(uint x, uint y) {
  uint z = x + y, w = (z >> 61) + (z & mask61);
  return w < mask61 ? w : w - mask61;
}

uint sub61(uint x, uint y) { return x >= y ? x - y : mask61 - (y - x); }

uint mul61(uint x, uint y) {
  uint a = x >> 31, b = x & mask31, c = y >> 31, d = y & mask31,
       z = a * d + b * c, e = z >> 30, f = z & mask30;
  return (2 * a * c + e + (f << 31) + b * d) % mask61;
}

uint pow61(uint x, int n) {
  uint r = 1;
  for (; n; n >>= 1, x = mul61(x, x))
    if (n & 1)
      r = mul61(r, x);
  return r;
}

signed main() {
  constexpr int s_max = 5e5;
  char s[s_max + 1], t[s_max + 1];
  scanf("%s\n%s\n", s, t);
  const int m = strlen(s), n = strlen(t);
  const uint b1 = []() {
    std::random_device rd;
    std::mt19937_64 prng((static_cast<uint>(rd()) << 32) | rd());
    std::uniform_int_distribution<uint> uid(256, mask61 - 2);
    return uid(prng);
  }();
  vector<bool> fs(m);
  {
    uint hs = 0, ht = 0, b = 1;
    for (int i = 0;; i++, b = mul61(b1, b)) {
      ht = add61(ht, mul61(b, t[n - i - 1]));
      hs = add61(hs, mul61(b, s[(n - i - 1) % m]));
      if (i == n - 1)
        break;
    }
    fs[0] = ht == hs;
    dotimes(i, m - 1) {
      hs = add61(mul61(b1, sub61(hs, mul61(b, s[i]))), s[(i + n) % m]);
      fs[i + 1] = ht == hs;
    }
  }
  vector<int> rs(m);
  int r = 0;
  dotimes(i, m) {
    stack<int> path;
    for (int j = i;; j = (j + n) % m) {
      if (rs[j] || !fs[j]) {
        if (rs[j] == -1)
          r = -1;
        else {
          for (int c = rs[j] + 1; !path.empty(); c++, path.pop())
            rs[path.top()] = c;
          maxs(r, rs[i]);
        }
        break;
      }
      rs[j] = -1;
      path.emplace(j);
    }
    if (r < 0)
      break;
  }
  wint(r);
  return 0;
}