Problem p02893 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second

/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
template <class T> inline void chmin(T &a, const T& b) { if (b < a) a = b; }
template <class T> inline void chmax(T &a, const T& b) { if (b > a) a = b; }
/* clang-format on */

template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;

public:
  u64 a;

  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(const modint rhs) noexcept {
    return *this *= ~rhs;
  }
  constexpr modint power(u64 exp) const noexcept {
    modint v = 1, x = *this;
    while (exp) {
      if (exp & 1) {
        v *= x;
      }
      x *= x;
      exp >>= 1;
    }
    return v;
  }
  constexpr modint operator~() const noexcept { return power(Modulus - 2); }
};

vector<int> divisors(int N) {
  vector<int> res;
  for (int i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      res.push_back(i);
      if (N / i != i)
        res.push_back(N / i);
    }
  }
  sort(res.begin(), res.end());
  return res;
}

using mint = modint<998244353>;

struct edge_t {
  int to;
  int w;
};

mint pw2[200000 + 10];

mint solve(int N, const vector<int> &X, int L) {
  vec<edge_t, 2> G(N);
  for (int i = 0; i < N; i++) {
    int j = (i + L) % N;
    int w = ((i + L) / N) % 2;
    G[i].push_back({j, w});
  }
  vector<int> id(N, -1), color(N, -1);
  vec<int, 2> group;
  for (int i = 0; i < N; i++) {
    if (id[i] != -1)
      continue;
    int v = i, c = 0, k = group.size();
    group.push_back({});
    while (id[v] == -1) {
      id[v] = k;
      color[v] = c;
      group[k].push_back(v);
      assert(G[v].size() == 1);
      int nv = G[v][0].to;
      int w = G[v][0].w;
      v = nv;
      c ^= w;
    }
    assert(v == i);
    if (c)
      return 0;
  }
  mint res = 0;
  int rem = group.size();
  vector<int> bit(N, -1);
  for (int i = N - 1; i >= 0; i--) {
    if (bit[i] != -1) {
      if (bit[i] > X[i]) {
        return res;
      }
      if (bit[i] < X[i]) {
        res += pw2[rem];
        return res;
      }
      continue;
    }
    int k = id[i];
    --rem;
    if (X[i] == 0) {
      for (int v : group[k])
        bit[v] = (0 ^ color[i] ^ color[v]);
    } else {
      res += pw2[rem];
      for (int v : group[k])
        bit[v] = (1 ^ color[i] ^ color[v]);
    }
  }
  res += 1;
  return res;
}

int main() {
#ifdef DEBUG
  ifstream ifs("in.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
  int N;
  string S;
  while (cin >> N >> S) {
    pw2[0] = 1;
    for (int i = 1; i <= N; i++)
      pw2[i] = pw2[i - 1] * 2;
    reverse(S.begin(), S.end());
    vector<int> X(N);
    for (int i = 0; i < N; i++)
      X[i] = S[i] - '0';
    auto divs = divisors(2 * N);
    vector<mint> ways(divs.size());
    for (int i = 0; i < divs.size(); i++)
      ways[i] = solve(N, X, divs[i]);
    for (int i = 0; i < divs.size(); i++) {
      for (int j = 0; j < i; j++) {
        if (divs[i] % divs[j] == 0)
          ways[i] -= ways[j];
      }
    }
    mint res = 0;
    for (int i = 0; i < divs.size(); i++) {
      res += ways[i] * divs[i];
    }
    cout << res.value() << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second

/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
template <class T> inline void chmin(T &a, const T& b) { if (b < a) a = b; }
template <class T> inline void chmax(T &a, const T& b) { if (b > a) a = b; }
/* clang-format on */

template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;

public:
  u64 a;

  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(const modint rhs) noexcept {
    return *this *= ~rhs;
  }
  constexpr modint power(u64 exp) const noexcept {
    modint v = 1, x = *this;
    while (exp) {
      if (exp & 1) {
        v *= x;
      }
      x *= x;
      exp >>= 1;
    }
    return v;
  }
  constexpr modint operator~() const noexcept { return power(Modulus - 2); }
};

vector<int> divisors(int N) {
  vector<int> res;
  for (int i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      res.push_back(i);
      if (N / i != i)
        res.push_back(N / i);
    }
  }
  sort(res.begin(), res.end());
  return res;
}

using mint = modint<998244353>;

struct edge_t {
  int to;
  int w;
};

mint pw2[200000 + 10];

mint solve(int N, const vector<int> &X, int L) {
  if (N % L == 0)
    return 0;
  vec<edge_t, 2> G(N);
  for (int i = 0; i < N; i++) {
    int j = (i + L) % N;
    int w = ((i + L) / N) % 2;
    G[i].push_back({j, w});
  }
  vector<int> id(N, -1), color(N, -1);
  vec<int, 2> group;
  for (int i = 0; i < N; i++) {
    if (id[i] != -1)
      continue;
    int v = i, c = 0, k = group.size();
    group.push_back({});
    while (id[v] == -1) {
      id[v] = k;
      color[v] = c;
      group[k].push_back(v);
      assert(G[v].size() == 1);
      int nv = G[v][0].to;
      int w = G[v][0].w;
      v = nv;
      c ^= w;
    }
    assert(v == i);
    if (c)
      return 0;
  }
  mint res = 0;
  int rem = group.size();
  vector<int> bit(N, -1);
  for (int i = N - 1; i >= 0; i--) {
    if (bit[i] != -1) {
      if (bit[i] > X[i]) {
        return res;
      }
      if (bit[i] < X[i]) {
        res += pw2[rem];
        return res;
      }
      continue;
    }
    int k = id[i];
    --rem;
    if (X[i] == 0) {
      for (int v : group[k])
        bit[v] = (0 ^ color[i] ^ color[v]);
    } else {
      res += pw2[rem];
      for (int v : group[k])
        bit[v] = (1 ^ color[i] ^ color[v]);
    }
  }
  res += 1;
  return res;
}

int main() {
#ifdef DEBUG
  ifstream ifs("in.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
  int N;
  string S;
  while (cin >> N >> S) {
    pw2[0] = 1;
    for (int i = 1; i <= N; i++)
      pw2[i] = pw2[i - 1] * 2;
    reverse(S.begin(), S.end());
    vector<int> X(N);
    for (int i = 0; i < N; i++)
      X[i] = S[i] - '0';
    auto divs = divisors(2 * N);
    vector<mint> ways(divs.size());
    for (int i = 0; i < divs.size(); i++)
      ways[i] = solve(N, X, divs[i]);
    for (int i = 0; i < divs.size(); i++) {
      for (int j = 0; j < i; j++) {
        if (divs[i] % divs[j] == 0)
          ways[i] -= ways[j];
      }
    }
    mint res = 0;
    for (int i = 0; i < divs.size(); i++) {
      res += ways[i] * divs[i];
    }
    cout << res.value() << endl;
  }
  return 0;
}