Problem p02893 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ri register int
#define pb push_back
#define fi first
#define se second
using namespace std;
const int rlen = 1 << 18 | 1;
char buf[rlen], *ib = buf, *ob = buf;
#define gc()                                                                   \
  (((ib == ob) && (ob = (ib = buf) + fread(buf, 1, rlen, stdin)), ib == ob)    \
       ? -1                                                                    \
       : *ib++)
typedef long long ll;
typedef pair<int, int> pii;
inline int read() {
  int ans = 0;
  char ch = gc();
  while (!isdigit(ch))
    ch = gc();
  while (isdigit(ch))
    ans = ((ans << 2) + ans << 1) + (ch ^ 48), ch = gc();
  return ans;
}
inline int Read(char *s) {
  int top = 0;
  char ch = gc();
  while (!isdigit(ch))
    ch = gc();
  while (isdigit(ch))
    s[++top] = ch, ch = gc();
  return top;
}
const int mod = 998244353;
inline int add(int a, int b) { return (a += b) < mod ? a : a - mod; }
inline void Add(int &a, int b) { a = add(a, b); }
inline int dec(int a, int b) { return (a -= b) < 0 ? a + mod : a; }
inline void Dec(int &a, int b) { a = dec(a, b); }
inline int mul(int a, int b) { return (ll)a * b % mod; }
inline void Mul(int &a, int b) { a = (ll)a * b % mod; }
inline int ksm(int a, int p) {
  int ret = 1;
  for (; p; p >>= 1, Mul(a, a))
    (p & 1) && (Mul(ret, a), 1);
  return ret;
}
const int N = 2e5 + 5;
char s[N], t[N];
int n;
int pre[N], f[N];
inline int solve(int len) {
  int res = pre[len];
  for (ri i = 1; i <= len; ++i)
    t[i] = s[i];
  for (ri l = len + 1, r = len << 1; l <= n; l += len, r += len) {
    for (ri i = l; i <= r; ++i) {
      t[i] = t[i - len] ^ 1;
      if (s[i] > t[i])
        return res + 1;
      if (s[i] < t[i])
        return res;
    }
  }
  return res + 1;
}
int pri[N], tot = 0;
bool vs[N];
inline void init() {
  for (ri i = 2; i <= n; ++i) {
    if (!vs[i])
      pri[++tot] = i;
    for (ri j = 1, up = n / i; j <= tot && pri[j] <= up; ++j) {
      vs[i * pri[j]] = 1;
      if (i == i / pri[j] * pri[j])
        break;
    }
  }
}
int a[N], b[N], top;
vector<int> divv;
void dfs(int ps, int mt) {
  divv.pb(mt);
  for (ri mlt = a[ps], i = ps; i <= top; ++i, mlt = a[i])
    for (ri j = 1; j <= b[ps]; ++j, mlt *= a[i])
      dfs(i + 1, mt * mlt);
}
inline int calc(int lm) {
  int x = lm;
  top = 0;
  for (ri i = 1, up = sqrt(lm); i <= tot && pri[i] <= up && lm != 1; ++i) {
    if (lm != lm / pri[i] * pri[i])
      continue;
    ++top, a[top] = pri[i], b[top] = 0;
    while (lm == lm / pri[i] * pri[i])
      lm /= pri[i], ++b[top];
  }
  if (lm ^ 1)
    a[++top] = lm, b[top] = 1;
  divv.clear();
  dfs(1, 1);
  int res = 0;
  for (ri i = divv.size() - 1; ~i; --i)
    if (divv[i] != 1)
      if ((n / (x / divv[i])) & 1)
        Add(res, f[x / divv[i]]);
  return res;
}
int main() {
#ifdef ldxcaicai
  freopen("lx.in", "r", stdin);
#endif
  n = read(), Read(s);
  init();
  int res = 0;
  for (ri i = 1; i <= n; ++i)
    pre[i] = add(add(pre[i - 1], pre[i - 1]), s[i] - '0');
  res = add(pre[n], 1);
  Mul(res, n * 2);
  for (ri tp, i = 1; i * 3 <= n; ++i) {
    if (n % i)
      continue;
    if (!((n / i) & 1))
      continue;
    f[i] = dec(solve(i), calc(i));
    Dec(res, mul(2 * (n - i), f[i]));
  }
  cout << res;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ri register int
#define pb push_back
#define fi first
#define se second
using namespace std;
const int rlen = 1 << 18 | 1;
char buf[rlen], *ib = buf, *ob = buf;
#define gc()                                                                   \
  (((ib == ob) && (ob = (ib = buf) + fread(buf, 1, rlen, stdin)), ib == ob)    \
       ? -1                                                                    \
       : *ib++)
typedef long long ll;
typedef pair<int, int> pii;
inline int read() {
  int ans = 0;
  char ch = gc();
  while (!isdigit(ch))
    ch = gc();
  while (isdigit(ch))
    ans = ((ans << 2) + ans << 1) + (ch ^ 48), ch = gc();
  return ans;
}
inline int Read(char *s) {
  int top = 0;
  char ch = gc();
  while (!isdigit(ch))
    ch = gc();
  while (isdigit(ch))
    s[++top] = ch, ch = gc();
  return top;
}
const int mod = 998244353;
inline int add(int a, int b) { return (a += b) < mod ? a : a - mod; }
inline void Add(int &a, int b) { a = add(a, b); }
inline int dec(int a, int b) { return (a -= b) < 0 ? a + mod : a; }
inline void Dec(int &a, int b) { a = dec(a, b); }
inline int mul(int a, int b) { return (ll)a * b % mod; }
inline void Mul(int &a, int b) { a = (ll)a * b % mod; }
inline int ksm(int a, int p) {
  int ret = 1;
  for (; p; p >>= 1, Mul(a, a))
    (p & 1) && (Mul(ret, a), 1);
  return ret;
}
const int N = 2e5 + 5;
char s[N], t[N];
int n;
int pre[N], f[N];
inline int solve(int len) {
  int res = pre[len];
  for (ri i = 1; i <= len; ++i)
    t[i] = s[i];
  for (ri l = len + 1, r = len << 1; l <= n; l += len, r += len) {
    for (ri i = l; i <= r; ++i) {
      t[i] = t[i - len] ^ 1;
      if (s[i] > t[i])
        return res + 1;
      if (s[i] < t[i])
        return res;
    }
  }
  return res + 1;
}
int pri[N], tot = 0;
bool vs[N];
inline void init() {
  for (ri i = 2; i <= n; ++i) {
    if (!vs[i])
      pri[++tot] = i;
    for (ri j = 1, up = n / i; j <= tot && pri[j] <= up; ++j) {
      vs[i * pri[j]] = 1;
      if (i == i / pri[j] * pri[j])
        break;
    }
  }
}
int a[N], b[N], top;
vector<int> divv;
void dfs(int ps, int mt) {
  divv.pb(mt);
  for (ri mlt = a[ps], i = ps; i <= top; ++i, mlt = a[i])
    for (ri j = 1; j <= b[i]; ++j, mlt *= a[i])
      dfs(i + 1, mt * mlt);
}
inline int calc(int lm) {
  int x = lm;
  top = 0;
  for (ri i = 1, up = sqrt(lm); i <= tot && pri[i] <= up && lm != 1; ++i) {
    if (lm != lm / pri[i] * pri[i])
      continue;
    ++top, a[top] = pri[i], b[top] = 0;
    while (lm == lm / pri[i] * pri[i])
      lm /= pri[i], ++b[top];
  }
  if (lm ^ 1)
    a[++top] = lm, b[top] = 1;
  divv.clear();
  dfs(1, 1);
  int res = 0;
  for (ri i = divv.size() - 1; ~i; --i)
    if (divv[i] != 1)
      if ((n / (x / divv[i])) & 1)
        Add(res, f[x / divv[i]]);
  return res;
}
int main() {
#ifdef ldxcaicai
  freopen("lx.in", "r", stdin);
#endif
  n = read(), Read(s);
  init();
  int res = 0;
  for (ri i = 1; i <= n; ++i)
    pre[i] = add(add(pre[i - 1], pre[i - 1]), s[i] - '0');
  res = add(pre[n], 1);
  Mul(res, n * 2);
  for (ri tp, i = 1; i * 3 <= n; ++i) {
    if (n % i)
      continue;
    if (!((n / i) & 1))
      continue;
    f[i] = dec(solve(i), calc(i));
    Dec(res, mul(2 * (n - i), f[i]));
  }
  cout << res;
  return 0;
}