Problem p02962 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define int long long

template <typename T, T MOD, T B> struct RollingHash {
  vector<T> hash, p;
  RollingHash() {}
  RollingHash(const string &s) {
    int n = s.size();
    hash.assign(n + 1, 0);
    p.assign(n + 1, 1);
    for (int i = 0; i < n; i++) {
      hash[i + 1] = (hash[i] * B + s[i]) % MOD;
      p[i + 1] = p[i] * B % MOD;
    }
  }
  // S[l, r)
  T find(int l, int r) {
    T res = hash[r] + MOD - hash[l] * p[r - l] % MOD;
    return res >= MOD ? res - MOD : res;
  }
};

using ll = long long;
const ll MOD = 1e9 + 7;
const ll B = 1777771;

string s, t;
int n, m;

const int MOD2 = 1e9 + 9;

bool check(int k) {
  int x = 2000000 / n;
  string S = "", T = "";
  for (int i = 0; i < x; i++)
    S += s;
  for (int i = 0; i < k; i++)
    T += t;

  using RH = RollingHash<ll, MOD, B>;
  RH rt(S), rp(T);
  for (int i = 0; i < (int)S.size() - (int)T.size() + 1; i++) {
    if (rt.find(i, i + T.size()) == rp.find(0, T.size()))
      return true;
  }

  return false;
}
bool check2(int k) {
  int x = 2000000 / n;
  string S = "", T = "";
  for (int i = 0; i < x; i++)
    S += s;
  for (int i = 0; i < k; i++)
    T += t;

  using RH = RollingHash<ll, MOD2, B>;
  RH rt(S), rp(T);
  for (int i = 0; i < (int)S.size() - (int)T.size() + 1; i++) {
    if (rt.find(i, i + T.size()) == rp.find(0, T.size()))
      return true;
  }

  return false;
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin >> s >> t;
  n = s.size();
  m = t.size();
  int l = 0, r = 2000000 / m;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (check(mid) && check2(mid))
      l = mid;
    else
      r = mid;
  }

  if (l >= 2000000 / m - 1)
    cout << -1 << endl;
  else
    cout << l << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long

template <typename T, T MOD, T B> struct RollingHash {
  vector<T> hash, p;
  RollingHash() {}
  RollingHash(const string &s) {
    int n = s.size();
    hash.assign(n + 1, 0);
    p.assign(n + 1, 1);
    for (int i = 0; i < n; i++) {
      hash[i + 1] = (hash[i] * B + s[i]) % MOD;
      p[i + 1] = p[i] * B % MOD;
    }
  }
  // S[l, r)
  T find(int l, int r) {
    T res = hash[r] + MOD - hash[l] * p[r - l] % MOD;
    return res >= MOD ? res - MOD : res;
  }
};

using ll = long long;
const ll MOD = 1e9 + 7;
const ll B = 1777771;

string s, t;
int n, m;

const int MOD2 = 1e9 + 9;

bool check(int k) {
  int x = 2000000 / n;
  string S = "", T = "";
  for (int i = 0; i < x; i++)
    S += s;
  for (int i = 0; i < k; i++)
    T += t;

  using RH = RollingHash<ll, MOD, B>;
  RH rt(S), rp(T);
  for (int i = 0; i < (int)S.size() - (int)T.size() + 1; i++) {
    if (rt.find(i, i + T.size()) == rp.find(0, T.size()))
      return true;
  }

  return false;
}
bool check2(int k) {
  int x = 2000000 / n;
  string S = "", T = "";
  for (int i = 0; i < x; i++)
    S += s;
  for (int i = 0; i < k; i++)
    T += t;

  using RH = RollingHash<ll, MOD2, B>;
  RH rt(S), rp(T);
  for (int i = 0; i < (int)S.size() - (int)T.size() + 1; i++) {
    if (rt.find(i, i + T.size()) == rp.find(0, T.size()))
      return true;
  }

  return false;
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin >> s >> t;
  n = s.size();
  m = t.size();
  int l = 0, r = 2000000 / m;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }

  if (l >= 2000000 / m - 1)
    cout << -1 << endl;
  else
    cout << l << endl;

  return 0;
}