Problem p02962 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = 1'000'000'007;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> vector<T> gen_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto gen_v(size_t a, Ts... ts) {
  return vector<decltype(gen_v<T>(ts...))>(a, gen_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

template <long long MOD> struct RollingHash {
  int n;
  long long base;
  vector<long long> pow, hash;
  RollingHash() {}
  RollingHash(const string &s, long long base_ = 10007) { init(s, base_); }
  void init(const string &s, long long base_ = 10007) {
    n = s.size();
    base = base_;
    calc(s.c_str());
  }
  long long get(int x) const { return hash[x]; }
  long long get(int l, int r) const {
    return (get(r) - get(l) * pow[r - l] % MOD + MOD) % MOD;
  }
  void calc(const char *s) {
    pow.resize(n + 1);
    pow[0] = 1;
    hash.resize(n + 1);
    hash[0] = 0;
    for (int i = 0; i < n; i++) {
      pow[i + 1] = pow[i] * base % MOD;
      hash[i + 1] = (s[i] + hash[i] * base) % MOD;
    }
  }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s, t;
  cin >> s >> t;

  while (s.size() <= t.size()) {
    s += s;
  }
  s += s; // s.size() * 2 > t.size()
  int n = t.size();
  while (s.size() >= t.size()) {
    t += t;
  }
  // s.size() < t.size()
  const int MOD2 = 1000000009;
  RollingHash<MOD> S(s), T(t);
  RollingHash<MOD2> S2(s, 10009), T2(t, 10009);

  auto f = [&](int x) {
    int hash = T.get(0, n * x);
    int hash2 = T2.get(0, n * x);
    rep(i, 0, s.size()) {
      if (i + n * x >= s.size())
        break;
      if (S.get(i, i + n * x) == hash && S2.get(i, i + n * x) == hash2)
        return false;
    }
    return true;
  };
  auto binarySearch = [&](int ng, int ok) {
    if (f(ng))
      return ng;
    while (ng + 1 < ok) {
      int m = (ng + ok) / 2;
      if (f(m))
        ok = m;
      else
        ng = m;
    }
    return ok;
  };
  dump(s, t);
  int ans = binarySearch(1, t.size() / n + 1) - 1;
  s += s;
  t += t;
  S = RollingHash<MOD>(s);
  T = RollingHash<MOD>(t);
  S2 = RollingHash<MOD2>(s, 10009);
  T2 = RollingHash<MOD2>(t, 10009);
  int ans2 = binarySearch(1, t.size() / n + 1) - 1;
  int cnt = 0;
  while (ans != ans2 && cnt < 2) {
    ans = ans2;
    s += s;
    t += t;
    S = RollingHash<MOD>(s);
    T = RollingHash<MOD>(t);
    S2 = RollingHash<MOD2>(s, 10009);
    T2 = RollingHash<MOD2>(t, 10009);
    ans2 = binarySearch(1, t.size() / n + 1) - 1;
    cnt++;
  }
  if (ans != ans2) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = 1'000'000'007;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> vector<T> gen_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto gen_v(size_t a, Ts... ts) {
  return vector<decltype(gen_v<T>(ts...))>(a, gen_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

template <long long MOD> struct RollingHash {
  int n;
  long long base;
  vector<long long> pow, hash;
  RollingHash() {}
  RollingHash(const string &s, long long base_ = 10007) { init(s, base_); }
  void init(const string &s, long long base_ = 10007) {
    n = s.size();
    base = base_;
    calc(s.c_str());
  }
  long long get(int x) const { return hash[x]; }
  long long get(int l, int r) const {
    return (get(r) - get(l) * pow[r - l] % MOD + MOD) % MOD;
  }
  void calc(const char *s) {
    pow.resize(n + 1);
    pow[0] = 1;
    hash.resize(n + 1);
    hash[0] = 0;
    for (int i = 0; i < n; i++) {
      pow[i + 1] = pow[i] * base % MOD;
      hash[i + 1] = (s[i] + hash[i] * base) % MOD;
    }
  }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s, t;
  cin >> s >> t;

  while (s.size() <= t.size()) {
    s += s;
  }
  s += s; // s.size() * 2 > t.size()
  int n = t.size();
  while (s.size() >= t.size()) {
    t += t;
  }
  // s.size() < t.size()
  const int MOD2 = 1000000009;
  RollingHash<MOD> S(s), T(t);
  RollingHash<MOD2> S2(s, 10009), T2(t, 10009);

  auto f = [&](int x) {
    int hash = T.get(0, n * x);
    int hash2 = T2.get(0, n * x);
    rep(i, 0, s.size()) {
      if (i + n * x >= s.size())
        break;
      if (S.get(i, i + n * x) == hash && S2.get(i, i + n * x) == hash2)
        return false;
    }
    return true;
  };
  auto binarySearch = [&](int ng, int ok) {
    if (f(ng))
      return ng;
    while (ng + 1 < ok) {
      int m = (ng + ok) / 2;
      if (f(m))
        ok = m;
      else
        ng = m;
    }
    return ok;
  };
  dump(s, t);
  int ans = binarySearch(1, t.size() / n + 1) - 1;
  s += s;
  t += t;
  S = RollingHash<MOD>(s);
  T = RollingHash<MOD>(t);
  S2 = RollingHash<MOD2>(s, 10009);
  T2 = RollingHash<MOD2>(t, 10009);
  int ans2 = binarySearch(1, t.size() / n + 1) - 1;
  int cnt = 0;
  while (ans != ans2 && cnt < 1) {
    ans = ans2;
    s += s;
    t += t;
    S = RollingHash<MOD>(s);
    T = RollingHash<MOD>(t);
    S2 = RollingHash<MOD2>(s, 10009);
    T2 = RollingHash<MOD2>(t, 10009);
    ans2 = binarySearch(1, t.size() / n + 1) - 1;
    cnt++;
  }
  if (ans != ans2) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}