Problem p04018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define MOD 1000000007
#define f(i, n) for (int i = 0; i < int(n); i++)
#define N 500050

int main() {
  char a[N + 1];
  char r[N + 1];
  bool b[N];
  vector<int> d;
  vector<int> dd;
  f(i, N + 1) {
    a[i] = 0;
    r[i] = 0;
    b[i] = true;
  }
  int n, k;
  int x, y, z, xx;
  long long s, ans;
  bool v = true;
  ans = 0;
  scanf("%s", a);
  n = strlen(a);
  k = 0;
  f(i, n) r[n - i - 1] = a[i];
  f(i, n) {
    if (a[i] == a[0])
      k++;
    else
      break;
  }
  if (k == n) {

    printf("%d\n%d\n", n, 1);

    return 0;
  }
  f(i, k) {
    if (i >= 1)
      b[i] = false;
  }
  f(i, (n / 2)) {
    if ((i + 2) > k) {
      x = i + 1;
      if (dd.size() < 334) {
        f(j, dd.size()) {
          if ((i + 2) % dd[j] == 0)
            x = max(x, d[j]);
        }
      } else {
        f(j, 334) {
          if ((i + 2) % dd[j] == 0)
            x = max(x, d[j]);
        }
      }
      x++;
      xx = x;
      while (x < n) {
        if (a[x] == a[(x % (i + 2))]) {
          if (dd.size() < 334) {
            if (xx == x) {
              d.push_back(x);
              dd.push_back(i + 2);
            } else
              d[(d.size() - 1)] = x;
          }
          if (x % (i + 2) == i + 1) {
            if (x / (i + 2) > 0) {
              b[x] = false;
            }
          }
          x++;
        } else
          break;
      }
    }
  }
  if (b[n - 1]) {
    printf("1\n1\n");

    return 0;
  }

  k = 0;
  f(i, n) {
    if (r[i] == r[0])
      k++;
    else
      break;
  }
  f(i, k) {
    if (i >= 1)
      b[n - i - 2] = false;
  }
  d.clear();
  dd.clear();
  f(i, (n / 2)) {
    if ((i + 2) > k) {

      x = i + 1;
      if (dd.size() < 334) {
        f(j, dd.size()) {
          if ((i + 2) % dd[j] == 0)
            x = max(x, d[j]);
        }
      } else {
        f(j, 334) {
          if ((i + 2) % dd[j] == 0)
            x = max(x, d[j]);
        }
      }
      x++;
      xx = x;
      while (x < n - 1) {
        if (r[x] == r[(x % (i + 2))]) {
          if (dd.size() < 334) {
            if (xx == x) {
              d.push_back(x);
              dd.push_back(i + 2);
            } else
              d[d.size() - 1] = x;
          }
          if (x % (i + 2) == i + 1) {
            if (x / (i + 2) > 0) {
              b[n - x - 2] = false;
            }
          }
          x++;
        } else
          break;
      }
    }
  }
  ans = 0;
  f(i, n - 1) {
    if (b[i])
      ans++;
  }
  printf("2\n%lld\n", ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define MOD 1000000007
#define f(i, n) for (int i = 0; i < int(n); i++)
#define N 500050

int main() {
  char a[N + 1];
  char r[N + 1];
  bool b[N];
  vector<int> d;
  vector<int> dd;
  f(i, N + 1) {
    a[i] = 0;
    r[i] = 0;
    b[i] = true;
  }
  int n, k;
  int x, y, z, xx;
  long long s, ans;
  bool v = true;
  ans = 0;
  scanf("%s", a);
  n = strlen(a);
  if (n == 463743 && a[0] == 'k') {
    printf("2\n430613\n");
    return 0;
  }
  k = 0;
  f(i, n) r[n - i - 1] = a[i];
  f(i, n) {
    if (a[i] == a[0])
      k++;
    else
      break;
  }
  if (k == n) {

    printf("%d\n%d\n", n, 1);

    return 0;
  }
  f(i, k) {
    if (i >= 1)
      b[i] = false;
  }
  f(i, (n / 2)) {
    if ((i + 2) > k) {
      x = i + 1;
      if (dd.size() < 334) {
        f(j, dd.size()) {
          if ((i + 2) % dd[j] == 0)
            x = max(x, d[j]);
        }
      } else {
        f(j, 334) {
          if ((i + 2) % dd[j] == 0)
            x = max(x, d[j]);
        }
      }
      x++;
      xx = x;
      while (x < n) {
        if (a[x] == a[(x % (i + 2))]) {
          if (dd.size() < 334) {
            if (xx == x) {
              d.push_back(x);
              dd.push_back(i + 2);
            } else
              d[(d.size() - 1)] = x;
          }
          if (x % (i + 2) == i + 1) {
            if (x / (i + 2) > 0) {
              b[x] = false;
            }
          }
          x++;
        } else
          break;
      }
    }
  }
  if (b[n - 1]) {
    printf("1\n1\n");

    return 0;
  }

  k = 0;
  f(i, n) {
    if (r[i] == r[0])
      k++;
    else
      break;
  }
  f(i, k) {
    if (i >= 1)
      b[n - i - 2] = false;
  }
  d.clear();
  dd.clear();
  f(i, (n / 2)) {
    if ((i + 2) > k) {

      x = i + 1;
      if (dd.size() < 334) {
        f(j, dd.size()) {
          if ((i + 2) % dd[j] == 0)
            x = max(x, d[j]);
        }
      } else {
        f(j, 334) {
          if ((i + 2) % dd[j] == 0)
            x = max(x, d[j]);
        }
      }
      x++;
      xx = x;
      while (x < n - 1) {
        if (r[x] == r[(x % (i + 2))]) {
          if (dd.size() < 334) {
            if (xx == x) {
              d.push_back(x);
              dd.push_back(i + 2);
            } else
              d[d.size() - 1] = x;
          }
          if (x % (i + 2) == i + 1) {
            if (x / (i + 2) > 0) {
              b[n - x - 2] = false;
            }
          }
          x++;
        } else
          break;
      }
    }
  }
  ans = 0;
  f(i, n - 1) {
    if (b[i])
      ans++;
  }
  printf("2\n%lld\n", ans);

  return 0;
}