Problem p04018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define forr(i, p, n) for (ll i = p; i < n; i++)
#define tam 1000010
#define tamlog 12
#define offset 50000
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<string, ll> si;
typedef pair<ll, ii> iii;
const ll INF = 1e11;
string txt;
const ll P1 = 53;
ll P[tam], hsh[tam];
ll n;
bool prefij[tam], sufij[tam];
const ll MOD = 1e9 + 7;
ll gethash(ll pos, ll delta) {
  ll iz, der;
  if (delta < 0) {
    der = pos;
    iz = pos + delta + 1;
  } else {
    der = pos + delta - 1;
    iz = pos;
  }
  // cout<<endl;
  // cout<<iz<<" inquery "<<der<<" "<<hsh[iz]<<" "<<hsh[der+1]<<endl;
  return ((hsh[iz] - hsh[der + 1] * P[abs(delta)]) % MOD + MOD) % MOD;
}
void tle() { tle(); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> txt;
  ll antP = 1, anthsh = 0;
  set<char> s;
  // cout<<txt<<"\n";
  hsh[txt.size()] = 0;
  P[0] = 1;
  for (ll i = txt.size() - 1; i >= 0; i--) {
    P[txt.size() - i] = (antP * P1) % MOD;
    antP = P[txt.size() - i];
    hsh[i] = ((txt[i] - 'a' + 1) * P1 + anthsh * P1) % MOD;
    anthsh = hsh[i];
    //  cout<<hsh[i]<< ' '<<P[txt.size()-i]<<" "<<i<<" "<<txt.size()-i<<endl;
    s.insert(txt[i]);
  }
  // cout<<gethash(1,-1)<<"alfin";
  // cout<<"aca";
  // cout<<gethash(0,-1)<<endl;
  // return 0;

  if (s.size() == 1) {
    cout << txt.size() << "\n1\n";
    return 0;
  }
  // check
  n = txt.size();
  set<ll> s1, s2;
  forr(delta, 1, n) {
    s1.clear(), s2.clear();
    s1.insert(gethash(delta - 1, -delta));
    s2.insert(gethash(n - (delta - 1) - 1, delta));
    for (ll j = delta + delta - 1; j < n; j += delta) {
      s1.insert(gethash(j, -delta));
      s2.insert(gethash(n - j - 1, delta));
      prefij[j] |= (s1.size() == 1);
      sufij[n - j - 1] |= (s2.size() == 1);
      // cout<<j<<" "<<n-j-1<<' '<<delta<<" "<<prefij[j]<<"
      // "<<sufij[n-j-1]<<endl;
    }
  }
  ll c = 0;
  forr(i, 0, n) {
    if (prefij[i] == 0 && sufij[i + 1] == 0) {
      c++;
      // cout<<i+1<<endl;
    }
  }
  if (sufij[0] != 0 && prefij[n - 1] != 0)
    cout << "2\n" << c << "\n";
  else
    cout << "1\n1\n";
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define forr(i, p, n) for (ll i = p; i < n; i++)
#define tam 1000010
#define tamlog 12
#define offset 50000
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<string, ll> si;
typedef pair<ll, ii> iii;
const ll INF = 1e11;
string txt;
const ll P1 = 53;
ll P[tam], hsh[tam];
ll n;
bool prefij[tam], sufij[tam];
const ll MOD = 1e9 + 7;
ll gethash(ll pos, ll delta) {
  ll iz, der;
  if (delta < 0) {
    der = pos;
    iz = pos + delta + 1;
  } else {
    der = pos + delta - 1;
    iz = pos;
  }
  // cout<<endl;
  // cout<<iz<<" inquery "<<der<<" "<<hsh[iz]<<" "<<hsh[der+1]<<endl;
  return ((hsh[iz] - hsh[der + 1] * P[abs(delta)]) % MOD + MOD) % MOD;
}
void tle() { tle(); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> txt;
  ll antP = 1, anthsh = 0;
  set<char> s;
  // cout<<txt<<"\n";
  hsh[txt.size()] = 0;
  P[0] = 1;
  for (ll i = txt.size() - 1; i >= 0; i--) {
    P[txt.size() - i] = (antP * P1) % MOD;
    antP = P[txt.size() - i];
    hsh[i] = ((txt[i] - 'a' + 1) * P1 + anthsh * P1) % MOD;
    anthsh = hsh[i];
    //  cout<<hsh[i]<< ' '<<P[txt.size()-i]<<" "<<i<<" "<<txt.size()-i<<endl;
    s.insert(txt[i]);
  }
  // cout<<gethash(1,-1)<<"alfin";
  // cout<<"aca";
  // cout<<gethash(0,-1)<<endl;
  // return 0;

  if (s.size() == 1) {
    cout << txt.size() << "\n1\n";
    return 0;
  }
  // check
  n = txt.size();
  set<ll> s1, s2;
  forr(delta, 1, n) {
    s1.clear(), s2.clear();
    s1.insert(gethash(delta - 1, -delta));
    s2.insert(gethash(n - (delta - 1) - 1, delta));
    for (ll j = delta + delta - 1; j < n; j += delta) {
      if (s1.size() <= 1)
        s1.insert(gethash(j, -delta));
      if (s2.size() <= 1)
        s2.insert(gethash(n - j - 1, delta));
      prefij[j] |= (s1.size() == 1);
      sufij[n - j - 1] |= (s2.size() == 1);
      // cout<<j<<" "<<n-j-1<<' '<<delta<<" "<<prefij[j]<<"
      // "<<sufij[n-j-1]<<endl;
    }
  }
  ll c = 0;
  forr(i, 0, n) {
    if (prefij[i] == 0 && sufij[i + 1] == 0) {
      c++;
      // cout<<i+1<<endl;
    }
  }
  if (sufij[0] != 0 && prefij[n - 1] != 0)
    cout << "2\n" << c << "\n";
  else
    cout << "1\n1\n";
}