Problem p02962 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, l, r) for (ll i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (ll i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; }
inline void err(bool b = true) {
  if (b) {
    cout << -1 << '\n';
    exit(0);
  }
}
template <class T> inline void fin(bool b = true, T e = 0) {
  if (b) {
    cout << e << '\n';
    exit(0);
  }
}
template <class T> T Roundup_div(T x, T y) { return (x + (y - 1)) / y; }
const ll INF = 1e18;
template <typename T> T pow(T a, long long n, T e = 1) {
  T ret = e;
  while (n) {
    if (n & 1)
      ret *= a;
    a *= a;
    n >>= 1;
  }
  return ret;
}

class RollingHash {
  const ll mod = 1e9 + 7;
  const ll base = 1007;
  V<> hash, power;

public:
  RollingHash(const string &s) {
    ll n = SZ(s);
    hash.assign(n + 1, 0);
    power.assign(n + 1, 1);
    REP(i, n) {
      hash[i + 1] = (hash[i] * base + s[i]) % mod;
      power[i + 1] = (power[i] * base) % mod;
    }
  }
  inline ll get(ll l, ll r) const {
    ll res = hash[r] - hash[l] * power[r - l] % mod;
    if (res < 0)
      res += mod;
    return res;
  }
  inline ll s_hash() { return get(0, SZ(hash) - 1); }
  inline ll getLCP(int a, int b) const {
    ll len = min(SZ(hash) - a, SZ(hash) - b);
    ll ok = 0, ng = len;
    while (abs(ok - ng) > 1) {
      ll mid = (ok + ng) / 2;
      if (get(a, a + mid) != get(b, b + mid))
        ng = mid;
      else
        ok = mid;
    }
    return ok;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  string s, t;
  cin >> s >> t;
  string _ = s;
  while (SZ(s) < SZ(t))
    s += _;
  s += s + s;
  RollingHash RH(s);

  auto check = [&](ll mid) {
    string u = "";
    REP(i, mid) u += t;
    RollingHash mew(u);
    ll c = mew.s_hash();
    REP(i, SZ(s) - SZ(t)) {
      if (RH.get(i, i + SZ(u)) == c)
        return true;
    }
    return false;
  };
  ll k = Roundup_div(SZ(s), SZ(t));
  ll ok = 0, ng = k;
  while (abs(ok - ng) > 1) {
    ll m = (ok + ng) / 2;
    if (check(m))
      ok = m;
    else
      ng = m;
  }
  cout << (ok == k - 1 ? -1 : ok) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, l, r) for (ll i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (ll i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; }
inline void err(bool b = true) {
  if (b) {
    cout << -1 << '\n';
    exit(0);
  }
}
template <class T> inline void fin(bool b = true, T e = 0) {
  if (b) {
    cout << e << '\n';
    exit(0);
  }
}
template <class T> T Roundup_div(T x, T y) { return (x + (y - 1)) / y; }
const ll INF = 1e18;
template <typename T> T pow(T a, long long n, T e = 1) {
  T ret = e;
  while (n) {
    if (n & 1)
      ret *= a;
    a *= a;
    n >>= 1;
  }
  return ret;
}

class RollingHash {
  const ll mod = 1e9 + 7;
  const ll base = 1007;
  V<> hash, power;

public:
  RollingHash(const string &s) {
    ll n = SZ(s);
    hash.assign(n + 1, 0);
    power.assign(n + 1, 1);
    REP(i, n) {
      hash[i + 1] = (hash[i] * base + s[i]) % mod;
      power[i + 1] = (power[i] * base) % mod;
    }
  }
  inline ll get(ll l, ll r) const {
    ll res = hash[r] - hash[l] * power[r - l] % mod;
    if (res < 0)
      res += mod;
    return res;
  }
  inline ll s_hash() { return get(0, SZ(hash) - 1); }
  inline ll getLCP(int a, int b) const {
    ll len = min(SZ(hash) - a, SZ(hash) - b);
    ll ok = 0, ng = len;
    while (abs(ok - ng) > 1) {
      ll mid = (ok + ng) / 2;
      if (get(a, a + mid) != get(b, b + mid))
        ng = mid;
      else
        ok = mid;
    }
    return ok;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  string s, t;
  cin >> s >> t;
  string _ = s;
  while (SZ(s) < SZ(t))
    s += _;
  s += s + s;
  RollingHash RH(s);

  auto check = [&](ll mid) {
    string u = "";
    REP(i, mid) u += t;
    RollingHash mew(u);
    ll c = mew.s_hash();
    REP(i, SZ(s) - SZ(u)) {
      if (RH.get(i, i + SZ(u)) == c)
        return true;
    }
    return false;
  };
  ll k = Roundup_div(SZ(s), SZ(t));
  ll ok = 0, ng = k;
  while (abs(ok - ng) > 1) {
    ll m = (ok + ng) / 2;
    if (check(m))
      ok = m;
    else
      ng = m;
  }
  cout << (ok == k - 1 ? -1 : ok) << endl;
}