Problem p02893 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll) x.size()
#define f first
#define s second
#define MAXN 201000

vi bucket_sizes;
int N;
string S;
ll ans;
ll MOD = 998244353;
ll mem[1010];
ll mod2[MAXN];
string rep, con, opp;

int main() {
  cin >> N >> S;
  for (int i = 3; i <= N; i += 2)
    if (!(N % i)) {
      bucket_sizes.pb(N / i);
    }
  // for (auto i : bucket_sizes)cout<<i<<' ';cout<<'\n';
  reverse(ALL(bucket_sizes));

  ll m = 1;
  ans = 1;
  for (int i = N - 1; i >= 0; --i) {
    if (S[i] == '1') {
      ans += m;
      ans %= MOD;
    }
    mod2[N - 1 - i] = m;
    m = (m * 2) % MOD;
  }
  ans = ans * (2 * N) % MOD;

  for (auto bsiz : bucket_sizes) {
    ll offset = 0;
    for (auto j : bucket_sizes)
      if (bsiz % j == 0) {
        offset += mem[j];
        offset %= MOD;
      }
    // cout<<bsiz<<' '<<offset<<'\n';
    ll s = 0;
    rep = con = opp = "";

    for (int j = 0; j < bsiz; ++j) {
      if (S[bsiz - 1 - j] == '1') {
        s += mod2[j];
        s %= MOD;
      }
      rep += S[j];
      opp += ('0' + '1' - S[j]);
    }

    // cout<<s<<'\n'; // Number of things that work from 0 to id-1
    // Must construct if its equal
    int in = 0;
    while (SZ(con) < N) {
      if (in)
        for (auto i : opp)
          con += (i);
      else
        for (auto i : rep)
          con += (i);
      in = 1 - in;
    }
    if (con <= S)
      ++s;
    s = (s + MOD - offset) % MOD;
    // cout<<"Discount "<<bsiz<<' '<<s<<'\n';
    ans = (ans + MOD - (s * (2 * N - 2 * bsiz)) % MOD) % MOD;
    mem[bsiz] = s;
  }

  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll) x.size()
#define f first
#define s second
#define MAXN 201000

vi bucket_sizes;
int N;
string S;
ll ans;
ll MOD = 998244353;
ll mem[MAXN];
ll mod2[MAXN];
string rep, con, opp;

int main() {
  cin >> N >> S;
  for (int i = 3; i <= N; i += 2)
    if (!(N % i)) {
      bucket_sizes.pb(N / i);
    }
  // for (auto i : bucket_sizes)cout<<i<<' ';cout<<'\n';
  reverse(ALL(bucket_sizes));

  ll m = 1;
  ans = 1;
  for (int i = N - 1; i >= 0; --i) {
    if (S[i] == '1') {
      ans += m;
      ans %= MOD;
    }
    mod2[N - 1 - i] = m;
    m = (m * 2) % MOD;
  }
  ans = ans * (2 * N) % MOD;

  for (auto bsiz : bucket_sizes) {
    ll offset = 0;
    for (auto j : bucket_sizes)
      if (bsiz % j == 0) {
        offset += mem[j];
        offset %= MOD;
      }
    // cout<<bsiz<<' '<<offset<<'\n';
    ll s = 0;
    rep = con = opp = "";

    for (int j = 0; j < bsiz; ++j) {
      if (S[bsiz - 1 - j] == '1') {
        s += mod2[j];
        s %= MOD;
      }
      rep += S[j];
      opp += ('0' + '1' - S[j]);
    }

    // cout<<s<<'\n'; // Number of things that work from 0 to id-1
    // Must construct if its equal
    int in = 0;
    while (SZ(con) < N) {
      if (in)
        for (auto i : opp)
          con += (i);
      else
        for (auto i : rep)
          con += (i);
      in = 1 - in;
    }
    if (con <= S)
      ++s;
    s = (s + MOD - offset) % MOD;
    // cout<<"Discount "<<bsiz<<' '<<s<<'\n';
    ans = (ans + MOD - (s * (2 * N - 2 * bsiz)) % MOD) % MOD;
    mem[bsiz] = s;
  }

  cout << ans;
}