Problem p02893 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")

// hloya template v26

// ░░░░░░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░░░░░
// ░░░░░░█░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░█░░░░░
// ░░░░░░█░█░▀░░░░░▀░░▀░░░░█░█░░░░░
// ░░░░░░█░█░░░░░░░░▄▀▀▄░▀░█░█▄▀▀▄░
// █▀▀█▄░█░█░░▀░░░░░█░░░▀▄▄█▄▀░░░█░
// ▀▄▄░▀██░█▄░▀░░░▄▄▀░░░░░░░░░░░░▀▄
// ░░▀█▄▄█░█░░░░▄░░█░░░▄█░░░▄░▄█░░█
// ░░░░░▀█░▀▄▀░░░░░█░██░▄░░▄░░▄░███
// ░░░░░▄█▄░░▀▀▀▀▀▀▀▀▄░░▀▀▀▀▀▀▀░▄▀░
// ░░░░█░░▄█▀█▀▀█▀▀▀▀▀▀█▀▀█▀█▀▀█░░░
// ░░░░▀▀▀▀░░▀▀▀░░░░░░░░▀▀▀░░▀▀░░░░

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

bool dbg = 0;

clock_t start_time = clock();
#define current_time                                                           \
  fixed << setprecision(6) << (ld)(clock() - start_time) / CLOCKS_PER_SEC

#define f first
#define s second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define sz(v) ((int)(v).size())
#define sqr(x) ((x) * (x))

#define ull unsigned long long
#define ll long long
#define ld long double
#define pii pair<int, int>
#define umap unordered_map<int, int>

#define files1 freopen("input.txt", "r", stdin)
#define files2 freopen("output.txt", "w", stdout)
#define files                                                                  \
  files1;                                                                      \
  files2
#define fast_io                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

// #define endl '\n'
#define ln(i, n) " \n"[(i) == (n)-1]

void bad(string mes = "Impossible") {
  cout << mes;
  exit(0);
}
void bad(int mes) {
  cout << mes;
  exit(0);
}

template <typename T> string bin(T x, int st = 2) {
  string ans = "";
  while (x > 0) {
    ans += char('0' + x % st);
    x /= st;
  }
  reverse(ans.begin(), ans.end());
  return ans.empty() ? "0" : ans;
}

mt19937_64 mt_rand(chrono::system_clock::now().time_since_epoch().count());

template <typename T1, typename T2> inline bool upmax(T1 &a, T2 b) {
  return (a < b ? (a = b, true) : false);
}
template <typename T1, typename T2> inline bool upmin(T1 &a, T2 b) {
  return (b < a ? (a = b, true) : false);
}

// inline int popcount(int x){
//     int count = 0;
//     __asm__ volatile("POPCNT %1, %0;":"=r"(count):"r"(x):);
//     return count;
// }

template <typename T> T input() {
  T ans = 0, m = 1;
  char c = ' ';

  while (!((c >= '0' && c <= '9') || c == '-')) {
    c = getchar();
  }

  if (c == '-')
    m = -1, c = getchar();
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + (c - '0'), c = getchar();
  }
  return ans * m;
}

template <typename T> T gcd(T a, T b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}

template <typename T> void read(T &a) { a = input<T>(); }
template <typename T> void read(T &a, T &b) { read(a), read(b); }
template <typename T> void read(T &a, T &b, T &c) { read(a, b), read(c); }
template <typename T> void read(T &a, T &b, T &c, T &d) {
  read(a, b), read(c, d);
}

const int inf = 1e9 + 20;
const short short_inf = 3e4 + 20;
const long double eps = 1e-12;
const int maxn = (int)2e5 + 3, base = 998244353;
const ll llinf = 2e18 + 5;

int binpow(int a, int s) {
  int res = 1;
  while (s) {
    if (s % 2) {
      res = 1ll * res * a % base;
    }
    a = 1ll * a * a % base;
    s /= 2;
  }
  return res;
}

int solve(int n, int k) {
  int need = k;
  for (int iter = 0;; iter++) {
    if (need == k && iter > 0) {
      return iter;
    }

    if (k & 1) {
      k /= 2;
    } else {
      k /= 2;
      k += 1 << (n - 1);
    }
  }
}

int n;
string x;
int dva[maxn];

int eval(int d) {
  int l = n / d;
  int ans = 0;
  for (int i = 0; i < l; i++) {
    if (x[i] == '1') {
      ans += dva[l - i - 1];
      ans %= base;
    }
  }
  bool ok = true;
  for (int i = 0; i < n; i++) {
    int m = i / l;
    int nd = x[i % l] - '0';
    if (m % 2) {
      nd ^= 1;
    }
    int hv = x[i] - '0';
    if (hv < nd) {
      ok = false;
      break;
    }
    if (hv > nd) {
      break;
    }
  }
  return ans + ok;
}

int main() {
  files1;
  fast_io;

  dva[0] = 1;
  for (int i = 1; i < maxn; i++) {
    dva[i] = 1ll * dva[i - 1] * 2 % base;
  }

  cin >> n >> x;

  vector<pii> dp;
  for (int d = 1; d * d <= n; d++) {
    if (n % d == 0) {
      int a = d;
      int b = n / d;

      if (a % 2) {
        dp.pb(mp(a, eval(a)));
      }

      if (a != b) {
        if (b % 2) {
          dp.pb(mp(b, eval(b)));
        }
      }
    }
  }

  sort(all(dp));

  int ans = 0;
  for (int i = dp.size() - 1; i >= 0; i--) {
    for (int j = i + 1; j < dp.size(); j++) {
      if (dp[j].f % dp[i].f == 0) {
        dp[i].s = (dp[i].s - dp[j].s + base) % base;
      }
    }
    ans += 1ll * dp[i].s * (2 * n / dp[i].f) % base;
    ans %= base;
  }
  cout << ans;
  return 0;
}

Fixed submission:
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")

// hloya template v26

// ░░░░░░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░░░░░
// ░░░░░░█░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░█░░░░░
// ░░░░░░█░█░▀░░░░░▀░░▀░░░░█░█░░░░░
// ░░░░░░█░█░░░░░░░░▄▀▀▄░▀░█░█▄▀▀▄░
// █▀▀█▄░█░█░░▀░░░░░█░░░▀▄▄█▄▀░░░█░
// ▀▄▄░▀██░█▄░▀░░░▄▄▀░░░░░░░░░░░░▀▄
// ░░▀█▄▄█░█░░░░▄░░█░░░▄█░░░▄░▄█░░█
// ░░░░░▀█░▀▄▀░░░░░█░██░▄░░▄░░▄░███
// ░░░░░▄█▄░░▀▀▀▀▀▀▀▀▄░░▀▀▀▀▀▀▀░▄▀░
// ░░░░█░░▄█▀█▀▀█▀▀▀▀▀▀█▀▀█▀█▀▀█░░░
// ░░░░▀▀▀▀░░▀▀▀░░░░░░░░▀▀▀░░▀▀░░░░

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

bool dbg = 0;

clock_t start_time = clock();
#define current_time                                                           \
  fixed << setprecision(6) << (ld)(clock() - start_time) / CLOCKS_PER_SEC

#define f first
#define s second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define sz(v) ((int)(v).size())
#define sqr(x) ((x) * (x))

#define ull unsigned long long
#define ll long long
#define ld long double
#define pii pair<int, int>
#define umap unordered_map<int, int>

#define files1 freopen("input.txt", "r", stdin)
#define files2 freopen("output.txt", "w", stdout)
#define files                                                                  \
  files1;                                                                      \
  files2
#define fast_io                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

// #define endl '\n'
#define ln(i, n) " \n"[(i) == (n)-1]

void bad(string mes = "Impossible") {
  cout << mes;
  exit(0);
}
void bad(int mes) {
  cout << mes;
  exit(0);
}

template <typename T> string bin(T x, int st = 2) {
  string ans = "";
  while (x > 0) {
    ans += char('0' + x % st);
    x /= st;
  }
  reverse(ans.begin(), ans.end());
  return ans.empty() ? "0" : ans;
}

mt19937_64 mt_rand(chrono::system_clock::now().time_since_epoch().count());

template <typename T1, typename T2> inline bool upmax(T1 &a, T2 b) {
  return (a < b ? (a = b, true) : false);
}
template <typename T1, typename T2> inline bool upmin(T1 &a, T2 b) {
  return (b < a ? (a = b, true) : false);
}

// inline int popcount(int x){
//     int count = 0;
//     __asm__ volatile("POPCNT %1, %0;":"=r"(count):"r"(x):);
//     return count;
// }

template <typename T> T input() {
  T ans = 0, m = 1;
  char c = ' ';

  while (!((c >= '0' && c <= '9') || c == '-')) {
    c = getchar();
  }

  if (c == '-')
    m = -1, c = getchar();
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + (c - '0'), c = getchar();
  }
  return ans * m;
}

template <typename T> T gcd(T a, T b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}

template <typename T> void read(T &a) { a = input<T>(); }
template <typename T> void read(T &a, T &b) { read(a), read(b); }
template <typename T> void read(T &a, T &b, T &c) { read(a, b), read(c); }
template <typename T> void read(T &a, T &b, T &c, T &d) {
  read(a, b), read(c, d);
}

const int inf = 1e9 + 20;
const short short_inf = 3e4 + 20;
const long double eps = 1e-12;
const int maxn = (int)2e5 + 3, base = 998244353;
const ll llinf = 2e18 + 5;

int binpow(int a, int s) {
  int res = 1;
  while (s) {
    if (s % 2) {
      res = 1ll * res * a % base;
    }
    a = 1ll * a * a % base;
    s /= 2;
  }
  return res;
}

int solve(int n, int k) {
  int need = k;
  for (int iter = 0;; iter++) {
    if (need == k && iter > 0) {
      return iter;
    }

    if (k & 1) {
      k /= 2;
    } else {
      k /= 2;
      k += 1 << (n - 1);
    }
  }
}

int n;
string x;
int dva[maxn];

int eval(int d) {
  int l = n / d;
  int ans = 0;
  for (int i = 0; i < l; i++) {
    if (x[i] == '1') {
      ans += dva[l - i - 1];
      ans %= base;
    }
  }
  bool ok = true;
  for (int i = 0; i < n; i++) {
    int m = i / l;
    int nd = x[i % l] - '0';
    if (m % 2) {
      nd ^= 1;
    }
    int hv = x[i] - '0';
    if (hv < nd) {
      ok = false;
      break;
    }
    if (hv > nd) {
      break;
    }
  }
  return ans + ok;
}

int main() {
  // files1;
  fast_io;

  dva[0] = 1;
  for (int i = 1; i < maxn; i++) {
    dva[i] = 1ll * dva[i - 1] * 2 % base;
  }

  cin >> n >> x;

  vector<pii> dp;
  for (int d = 1; d * d <= n; d++) {
    if (n % d == 0) {
      int a = d;
      int b = n / d;

      if (a % 2) {
        dp.pb(mp(a, eval(a)));
      }

      if (a != b) {
        if (b % 2) {
          dp.pb(mp(b, eval(b)));
        }
      }
    }
  }

  sort(all(dp));

  int ans = 0;
  for (int i = dp.size() - 1; i >= 0; i--) {
    for (int j = i + 1; j < dp.size(); j++) {
      if (dp[j].f % dp[i].f == 0) {
        dp[i].s = (dp[i].s - dp[j].s + base) % base;
      }
    }
    ans += 1ll * dp[i].s * (2 * n / dp[i].f) % base;
    ans %= base;
  }
  cout << ans;
  return 0;
}