Problem p02893 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
template <typename T> T inverse(T a, T m) {
  T u = 0, v = 1;
  while (a != 0) {
    T t = m / a;
    m -= t * a;
    swap(a, m);
    u -= t * v;
    swap(u, v);
  }
  assert(m == 1);
  return u;
}
template <typename T> class Modular {
public:
  using Type = typename decay<decltype(T::value)>::type;
  constexpr Modular() : value() {}
  template <typename U> Modular(const U &x) { value = normalize(x); }
  template <typename U> static Type normalize(const U &x) {
    Type v;
    if (-mod() <= x && x < mod())
      v = static_cast<Type>(x);
    else
      v = static_cast<Type>(x % mod());
    if (v < 0)
      v += mod();
    return v;
  }
  const Type &operator()() const { return value; }
  template <typename U> explicit operator U() const {
    return static_cast<U>(value);
  }
  constexpr static Type mod() { return T::value; }
  Modular &operator+=(const Modular &other) {
    if ((value += other.value) >= mod())
      value -= mod();
    return *this;
  }
  Modular &operator-=(const Modular &other) {
    if ((value -= other.value) < 0)
      value += mod();
    return *this;
  }
  template <typename U> Modular &operator+=(const U &other) {
    return *this += Modular(other);
  }
  template <typename U> Modular &operator-=(const U &other) {
    return *this -= Modular(other);
  }
  Modular &operator++() { return *this += 1; }
  Modular &operator--() { return *this -= 1; }
  Modular operator++(int) {
    Modular result(*this);
    *this += 1;
    return result;
  }
  Modular operator--(int) {
    Modular result(*this);
    *this -= 1;
    return result;
  }
  Modular operator-() const { return Modular(-value); }
  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, int>::value,
                     Modular>::type &
  operator*=(const Modular &rhs) {
    value = normalize(static_cast<int64_t>(value) *
                      static_cast<int64_t>(rhs.value));
    return *this;
  }
  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value,
                     Modular>::type &
  operator*=(const Modular &rhs) {
    int64_t q = static_cast<int64_t>(static_cast<long double>(value) *
                                     rhs.value / mod());
    value = normalize(value * rhs.value - q * mod());
    return *this;
  }
  template <typename U = T>
  typename enable_if<!is_integral<typename Modular<U>::Type>::value,
                     Modular>::type &
  operator*=(const Modular &rhs) {
    value = normalize(value * rhs.value);
    return *this;
  }
  Modular &operator/=(const Modular &other) {
    return *this *= Modular(inverse(other.value, mod()));
  }
  template <typename U> friend const Modular<U> &abs(const Modular<U> &v) {
    return v;
  }
  template <typename U>
  friend bool operator==(const Modular<U> &lhs, const Modular<U> &rhs);
  template <typename U>
  friend bool operator<(const Modular<U> &lhs, const Modular<U> &rhs);
  template <typename U>
  friend std::istream &operator>>(std::istream &stream, Modular<U> &number);

private:
  Type value;
};
template <typename T>
bool operator==(const Modular<T> &lhs, const Modular<T> &rhs) {
  return lhs.value == rhs.value;
}
template <typename T, typename U>
bool operator==(const Modular<T> &lhs, U rhs) {
  return lhs == Modular<T>(rhs);
}
template <typename T, typename U>
bool operator==(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) == rhs;
}
template <typename T>
bool operator!=(const Modular<T> &lhs, const Modular<T> &rhs) {
  return !(lhs == rhs);
}
template <typename T, typename U>
bool operator!=(const Modular<T> &lhs, U rhs) {
  return !(lhs == rhs);
}
template <typename T, typename U>
bool operator!=(U lhs, const Modular<T> &rhs) {
  return !(lhs == rhs);
}
template <typename T>
bool operator<(const Modular<T> &lhs, const Modular<T> &rhs) {
  return lhs.value < rhs.value;
}
template <typename T>
Modular<T> operator+(const Modular<T> &lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) += rhs;
}
template <typename T, typename U>
Modular<T> operator+(const Modular<T> &lhs, U rhs) {
  return Modular<T>(lhs) += rhs;
}
template <typename T, typename U>
Modular<T> operator+(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) += rhs;
}
template <typename T>
Modular<T> operator-(const Modular<T> &lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) -= rhs;
}
template <typename T, typename U>
Modular<T> operator-(const Modular<T> &lhs, U rhs) {
  return Modular<T>(lhs) -= rhs;
}
template <typename T, typename U>
Modular<T> operator-(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) -= rhs;
}
template <typename T>
Modular<T> operator*(const Modular<T> &lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) *= rhs;
}
template <typename T, typename U>
Modular<T> operator*(const Modular<T> &lhs, U rhs) {
  return Modular<T>(lhs) *= rhs;
}
template <typename T, typename U>
Modular<T> operator*(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) *= rhs;
}
template <typename T>
Modular<T> operator/(const Modular<T> &lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> operator/(const Modular<T> &lhs, U rhs) {
  return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> operator/(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> power(const Modular<T> &a, const U &b) {
  assert(b >= 0);
  Modular<T> x = a, res = 1;
  U p = b;
  while (p > 0) {
    if (p & 1)
      res *= x;
    x *= x;
    p >>= 1;
  }
  return res;
}
template <typename T> bool IsZero(const Modular<T> &number) {
  return number() == 0;
}
template <typename T> string to_string(const Modular<T> &number) {
  return to_string(number());
}
template <typename T>
std::ostream &operator<<(std::ostream &stream, const Modular<T> &number) {
  return stream << number();
}
template <typename T>
std::istream &operator>>(std::istream &stream, Modular<T> &number) {
  typename common_type<typename Modular<T>::Type, int64_t>::type x;
  stream >> x;
  number.value = Modular<T>::normalize(x);
  return stream;
}
constexpr int md = 998244353;
using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;
template <typename... Args> void read(Args &...args) { (cin >> ... >> args); }
template <typename... Args> void writeln(const Args &...args) {
  ((cout << args << " "), ...), cout << "\n";
}
template <typename... Args> void write(const Args &...args) {
  ((cout << args), ...);
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
typename enable_if<is_same<decltype(declval<T>().size()), size_t>::value,
                   int>::type
operator~(const T &ct) {
  return (int)ct.size();
}
template <typename T, typename... Args> bool ckmax(T &a, const Args &...b) {
  return (a < max({b...})) ? (a = max({b...}), true) : false;
}
template <typename T, typename... Args> bool ckmin(T &a, const Args &...b) {
  return (a > min({b...})) ? (a = min({b...}), true) : false;
}
template <typename T> struct range {
  T u, v;
  bool inclusive;
  range(const T &_u, const T &_v, const bool &_inclusive = false)
      : u(_u), v(_v), inclusive(_inclusive) {}
  bool has(const T &w) { return (u <= w) and (inclusive ? (w <= v) : (w < v)); }
};
using vi = vector<int>;
using vb = vector<bool>;
using pii = pair<int, int>;
using vvi = vector<vector<int>>;
using vpii = vector<pii>;
using uint = unsigned;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
template <typename T> using vv = vector<vector<T>>;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using max_heap = priority_queue<T, vector<T>, less<T>>;
const int N = 200'010;
vi odd_divs(int n) {
  vi res;
  for (int i = 1; i <= n; ++i) {
    if (!(n % i) and i % 2) {
      res.push_back(i);
    }
  }
  return res;
}
Mint bintuMintu(const string &s) {
  Mint res = 0;
  for (int i = 0; i < ~s; ++i) {
    res = 2 * res + (s[i] - '0');
  };
  ;
  return res;
}
Mint compute(int hf, int d, const string &x) {
  int n = ~x;
  string s = x.substr(0, hf), t(n, '0');
  for (int i = 0; i < ~s; ++i) {
    t[i] = s[i] ^ 1;
  };
  ;
  Mint res = bintuMintu(s);
  string st;
  for (int i = 0; i < (d >> 1); ++i) {
    st += s, st += t;
  }
  st += s;
  res += (st <= x);
  return res;
}
auto soln() {
  int n;
  string x;
  cin >> n >> x;
  map<int, Mint> C;
  for (auto d : odd_divs(n)) {
    int hf = n / d, k = 2 * hf;
    C[k] = compute(hf, d, x);
  };
  ;
  for (int i = 1; i <= n; ++i) {
    if (C.find(i) != C.end()) {
      for (int j = 2 * i; j <= 2 * n; j += i) {
        if (C.find(j) != C.end())
          C[j] -= C[i];
      }
    }
  };
  ;
  Mint res;
  for (auto &[k, v] : C) {
    res += k * v;
  }
  cout << res << "\n";
}
signed main() {
  int t = 1;
  { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
  while (t--)
    soln();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <typename T> T inverse(T a, T m) {
  T u = 0, v = 1;
  while (a != 0) {
    T t = m / a;
    m -= t * a;
    swap(a, m);
    u -= t * v;
    swap(u, v);
  }
  assert(m == 1);
  return u;
}
template <typename T> class Modular {
public:
  using Type = typename decay<decltype(T::value)>::type;
  constexpr Modular() : value() {}
  template <typename U> Modular(const U &x) { value = normalize(x); }
  template <typename U> static Type normalize(const U &x) {
    Type v;
    if (-mod() <= x && x < mod())
      v = static_cast<Type>(x);
    else
      v = static_cast<Type>(x % mod());
    if (v < 0)
      v += mod();
    return v;
  }
  const Type &operator()() const { return value; }
  template <typename U> explicit operator U() const {
    return static_cast<U>(value);
  }
  constexpr static Type mod() { return T::value; }
  Modular &operator+=(const Modular &other) {
    if ((value += other.value) >= mod())
      value -= mod();
    return *this;
  }
  Modular &operator-=(const Modular &other) {
    if ((value -= other.value) < 0)
      value += mod();
    return *this;
  }
  template <typename U> Modular &operator+=(const U &other) {
    return *this += Modular(other);
  }
  template <typename U> Modular &operator-=(const U &other) {
    return *this -= Modular(other);
  }
  Modular &operator++() { return *this += 1; }
  Modular &operator--() { return *this -= 1; }
  Modular operator++(int) {
    Modular result(*this);
    *this += 1;
    return result;
  }
  Modular operator--(int) {
    Modular result(*this);
    *this -= 1;
    return result;
  }
  Modular operator-() const { return Modular(-value); }
  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, int>::value,
                     Modular>::type &
  operator*=(const Modular &rhs) {
    value = normalize(static_cast<int64_t>(value) *
                      static_cast<int64_t>(rhs.value));
    return *this;
  }
  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value,
                     Modular>::type &
  operator*=(const Modular &rhs) {
    int64_t q = static_cast<int64_t>(static_cast<long double>(value) *
                                     rhs.value / mod());
    value = normalize(value * rhs.value - q * mod());
    return *this;
  }
  template <typename U = T>
  typename enable_if<!is_integral<typename Modular<U>::Type>::value,
                     Modular>::type &
  operator*=(const Modular &rhs) {
    value = normalize(value * rhs.value);
    return *this;
  }
  Modular &operator/=(const Modular &other) {
    return *this *= Modular(inverse(other.value, mod()));
  }
  template <typename U> friend const Modular<U> &abs(const Modular<U> &v) {
    return v;
  }
  template <typename U>
  friend bool operator==(const Modular<U> &lhs, const Modular<U> &rhs);
  template <typename U>
  friend bool operator<(const Modular<U> &lhs, const Modular<U> &rhs);
  template <typename U>
  friend std::istream &operator>>(std::istream &stream, Modular<U> &number);

private:
  Type value;
};
template <typename T>
bool operator==(const Modular<T> &lhs, const Modular<T> &rhs) {
  return lhs.value == rhs.value;
}
template <typename T, typename U>
bool operator==(const Modular<T> &lhs, U rhs) {
  return lhs == Modular<T>(rhs);
}
template <typename T, typename U>
bool operator==(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) == rhs;
}
template <typename T>
bool operator!=(const Modular<T> &lhs, const Modular<T> &rhs) {
  return !(lhs == rhs);
}
template <typename T, typename U>
bool operator!=(const Modular<T> &lhs, U rhs) {
  return !(lhs == rhs);
}
template <typename T, typename U>
bool operator!=(U lhs, const Modular<T> &rhs) {
  return !(lhs == rhs);
}
template <typename T>
bool operator<(const Modular<T> &lhs, const Modular<T> &rhs) {
  return lhs.value < rhs.value;
}
template <typename T>
Modular<T> operator+(const Modular<T> &lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) += rhs;
}
template <typename T, typename U>
Modular<T> operator+(const Modular<T> &lhs, U rhs) {
  return Modular<T>(lhs) += rhs;
}
template <typename T, typename U>
Modular<T> operator+(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) += rhs;
}
template <typename T>
Modular<T> operator-(const Modular<T> &lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) -= rhs;
}
template <typename T, typename U>
Modular<T> operator-(const Modular<T> &lhs, U rhs) {
  return Modular<T>(lhs) -= rhs;
}
template <typename T, typename U>
Modular<T> operator-(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) -= rhs;
}
template <typename T>
Modular<T> operator*(const Modular<T> &lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) *= rhs;
}
template <typename T, typename U>
Modular<T> operator*(const Modular<T> &lhs, U rhs) {
  return Modular<T>(lhs) *= rhs;
}
template <typename T, typename U>
Modular<T> operator*(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) *= rhs;
}
template <typename T>
Modular<T> operator/(const Modular<T> &lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> operator/(const Modular<T> &lhs, U rhs) {
  return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> operator/(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> power(const Modular<T> &a, const U &b) {
  assert(b >= 0);
  Modular<T> x = a, res = 1;
  U p = b;
  while (p > 0) {
    if (p & 1)
      res *= x;
    x *= x;
    p >>= 1;
  }
  return res;
}
template <typename T> bool IsZero(const Modular<T> &number) {
  return number() == 0;
}
template <typename T> string to_string(const Modular<T> &number) {
  return to_string(number());
}
template <typename T>
std::ostream &operator<<(std::ostream &stream, const Modular<T> &number) {
  return stream << number();
}
template <typename T>
std::istream &operator>>(std::istream &stream, Modular<T> &number) {
  typename common_type<typename Modular<T>::Type, int64_t>::type x;
  stream >> x;
  number.value = Modular<T>::normalize(x);
  return stream;
}
constexpr int md = 998244353;
using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;
template <typename... Args> void read(Args &...args) { (cin >> ... >> args); }
template <typename... Args> void writeln(const Args &...args) {
  ((cout << args << " "), ...), cout << "\n";
}
template <typename... Args> void write(const Args &...args) {
  ((cout << args), ...);
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
typename enable_if<is_same<decltype(declval<T>().size()), size_t>::value,
                   int>::type
operator~(const T &ct) {
  return (int)ct.size();
}
template <typename T, typename... Args> bool ckmax(T &a, const Args &...b) {
  return (a < max({b...})) ? (a = max({b...}), true) : false;
}
template <typename T, typename... Args> bool ckmin(T &a, const Args &...b) {
  return (a > min({b...})) ? (a = min({b...}), true) : false;
}
template <typename T> struct range {
  T u, v;
  bool inclusive;
  range(const T &_u, const T &_v, const bool &_inclusive = false)
      : u(_u), v(_v), inclusive(_inclusive) {}
  bool has(const T &w) { return (u <= w) and (inclusive ? (w <= v) : (w < v)); }
};
using vi = vector<int>;
using vb = vector<bool>;
using pii = pair<int, int>;
using vvi = vector<vector<int>>;
using vpii = vector<pii>;
using uint = unsigned;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
template <typename T> using vv = vector<vector<T>>;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using max_heap = priority_queue<T, vector<T>, less<T>>;
const int N = 200'010;
vi odd_divs(int n) {
  vi res;
  for (int i = 1; i <= n; ++i) {
    if (!(n % i) and i % 2) {
      res.push_back(i);
    }
  }
  return res;
}
Mint bintuMintu(const string &s) {
  Mint res = 0;
  for (int i = 0; i < ~s; ++i) {
    res = 2 * res + (s[i] - '0');
  };
  ;
  return res;
}
Mint compute(int hf, int d, const string &x) {
  int n = ~x;
  string s = x.substr(0, hf), t(hf, '0');
  for (int i = 0; i < ~s; ++i) {
    t[i] = s[i] ^ 1;
  };
  ;
  Mint res = bintuMintu(s);
  string st;
  for (int i = 0; i < (d >> 1); ++i) {
    st += s, st += t;
  }
  st += s;
  res += (st <= x);
  return res;
}
auto soln() {
  int n;
  string x;
  cin >> n >> x;
  map<int, Mint> C;
  for (auto d : odd_divs(n)) {
    int hf = n / d, k = 2 * hf;
    C[k] = compute(hf, d, x);
  };
  ;
  for (int i = 1; i <= n; ++i) {
    if (C.find(i) != C.end()) {
      for (int j = 2 * i; j <= 2 * n; j += i) {
        if (C.find(j) != C.end())
          C[j] -= C[i];
      }
    }
  };
  ;
  Mint res;
  for (auto &[k, v] : C) {
    res += k * v;
  }
  cout << res << "\n";
}
signed main() {
  int t = 1;
  { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
  while (t--)
    soln();
  return 0;
}