Problem p03562 (C++)

Buggy submission (status: Time Limit Exceeded):
#ifndef __INTMOD_H__0001__
#define __INTMOD_H__0001__

#include <cassert>
#include <iostream>
#include <vector>

template <unsigned int Modulus> class IntMod {
  typedef unsigned long long ULL;

private:
  unsigned int value_m;

  void Copy(const IntMod &other) { value_m = other.value_m; }
  bool Modulus_is_valid() { return Modulus != 0; }

public:
  IntMod() { value_m = 0; }
  IntMod(unsigned int value) { value_m = value % Modulus; }
  IntMod(unsigned long long value) { value_m = value % Modulus; }
  IntMod(int value) {
    if (Modulus >= 0x80000000UL) {
      value_m = value >= 0 ? (unsigned int)value % Modulus
                           : Modulus - (unsigned int)(-value);
    } else {
      int tmp = value % (int)Modulus;
      value_m = tmp >= 0 ? tmp : Modulus - (unsigned int)(-tmp);
    }
  }
  IntMod(const IntMod &other) { Copy(other); }
  IntMod &operator=(const IntMod &other) {
    Copy(other);
    return *this;
  }
  bool operator==(const IntMod &right) const {
    return value_m == right.value_m;
  }
  bool operator!=(const IntMod &right) const {
    return value_m != right.value_m;
  }
  IntMod &operator++() {
    ++value_m;
    value_m %= Modulus;
    return *this;
  }
  IntMod &operator--() {
    if (value_m == 0) {
      value_m = Modulus - 1;
    } else {
      --value_m;
    }
    return *this;
  }
  IntMod operator-() const {
    if (value_m == 0)
      return IntMod(0);
    return IntMod(Modulus - value_m);
  }
  IntMod &operator+=(const IntMod &right) {
    ULL sum = (ULL)value_m + right.value_m;
    if (sum >= (ULL)Modulus) {
      value_m = (unsigned int)(sum - (ULL)Modulus);
    } else {
      value_m = (unsigned int)sum;
    }
    return *this;
  }
  IntMod &operator-=(const IntMod &right) {
    (*this) += (-right);
    return *this;
  }
  IntMod operator+(const IntMod &right) const {
    IntMod ret(*this);
    ret += right;
    return ret;
  }
  IntMod operator-(const IntMod &right) const {
    IntMod ret(*this);
    ret -= right;
    return ret;
  }
  IntMod &operator*=(const IntMod &right) {
    ULL tmp = (ULL)value_m * right.value_m;
    value_m = tmp % (ULL)Modulus;
    return *this;
  }
  IntMod &operator/=(const IntMod &right) {
    (*this) *= (right.Inverse());
    return *this;
  }
  IntMod operator*(const IntMod &right) const {
    IntMod ret(*this);
    ret *= right;
    return ret;
  }
  IntMod operator/(const IntMod &right) const {
    IntMod ret(*this);
    ret /= right;
    return ret;
  }
  // for power
  IntMod operator[](unsigned int exp) const { return Pow(exp); }

  /* 素数判定は自分でしろ */
  IntMod Inverse() const { return (*this).Pow(Modulus - 2); }
  IntMod Pow(unsigned int exp) const {
    unsigned int arr[32]; // TODO
    ULL sum = 1;
    arr[0] = value_m;
    for (int i = 1; i < 32; ++i) {
      arr[i] = ((ULL)arr[i - 1] * arr[i - 1]) % (ULL)Modulus;
    }
    for (int i = 0; i < 32; ++i) {
      if (exp & (0x1 << i)) {
        sum *= arr[i];
        sum %= (ULL)Modulus;
      }
    }
    return (unsigned int)sum;
  }
  IntMod Fact() const {
    static std::vector<IntMod> table;
    int factoriand = Get_value();
    if (table.size() > factoriand) {
      return table[factoriand];
    }

    int old_size = table.size();
    table.resize(factoriand + 1);
    for (int i = old_size; i <= factoriand; i++) {
      table[i] = (i == 0 ? 1 : table[i - 1] * i);
    }
    return table[factoriand];
  }
  IntMod Combination(const IntMod &right) const {
    if (value_m < right.value_m)
      throw "okashii";
    return Fact() / ((*this - right).Fact() * right.Fact());
  }
  unsigned int Get_value() const { return value_m; }
  static std::vector<IntMod> Inverse_list(int size) {
    assert(size < Modulus);
    std::vector<IntMod> ret_arr(size + 1);
    ret_arr[1] = 1;
    for (int i = 2; i <= size; ++i) {
      ret_arr[i] =
          ((ULL)(Modulus - Modulus / i) * ret_arr[Modulus % i].Get_value()) %
          Modulus;
    }
    return ret_arr;
  }
};
typedef IntMod<1000000007> MInt;

// template <unsigned int Modulus>
// IntMod<Modulus> operator*(const int left, const IntMod<Modulus>& right) {
//	IntMod<Modulus> ret(left);
//	ret *= right;
//	return ret;
// }
// template <unsigned int Modulus>
// IntMod<Modulus> operator/(const int left, const IntMod<Modulus>& right) {
//	IntMod<Modulus> ret(left);
//	ret /= right;
//	return ret;
// }

#if 0
MInt operator"" _m(unsigned long long num) { return MInt(num); }
#endif

template <unsigned int Modulus>
std::ostream &operator<<(std::ostream &ost, const IntMod<Modulus> &val) {
  ost << val.Get_value();
  return ost;
}

#endif

#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
typedef unsigned int uint;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PP;
#define REP(i, a, n) for (LL i = (a), i##_max = (n); i < i##_max; ++i)
#define REM(i, a, n) for (LL i = (LL)(n)-1, i##min = (a); i >= i##min; --i)
#define ALL(arr) (arr).begin(), (arr).end()
#define FLOAT fixed << setprecision(16)
#define SPEEDUP                                                                \
  {                                                                            \
    cin.tie(NULL);                                                             \
    ios::sync_with_stdio(false);                                               \
  }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream &operator>>(istream &ist, pair<T, U> &right) {
  return ist >> right.first >> right.second;
}
template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &right) {
  return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible &val) {
  fill(dest, dest + N, val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible &val) {
  for (int i = 0; i < M; ++i)
    Fill(dest[i], val);
}
template <class T> T Compare(T left, T right) {
  return left > right ? 1 : (left < right ? -1 : 0);
}
istream &Ignore(istream &ist) {
  string s;
  ist >> s;
  return ist;
}
bool Inside(int i, int j, int h, int w) {
  return i >= 0 && i < h && j >= 0 && j < w;
}
template <class T> T Next() {
  T buf;
  cin >> buf;
  return buf;
}

#ifdef ONLY_MY_ENVIR
#include "Graph.h"
#include "IntMod.h"
#include "Math.h"
#include "Matrix.h"
#include "MinMax.h"
#include "Range.h"
#include "Tree.h"
#include "Union_Find.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream &operator>>(istream &ist, __int128 &val) {
  LL tmp;
  ist >> tmp;
  val = tmp;
  return ist;
}
ostream &operator<<(ostream &ost, __int128 val) {
  LL tmp = val;
  ost << tmp;
  return ost;
}
#endif

#if 1234567891
#include <array>
#include <random>
#include <unordered_map>
#include <unordered_set>
template <typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T>>;
// template <class T>
// auto Is(const T& value) { return [value](const auto& comparand) -> bool {
// return comparand == value; }; }
#endif

const int M = 4064;
using Bits = bitset<M>;
using Mit = IntMod<998244353>;

int Digit(const Bits &a) {
  REM(i, 0, M) {
    if (a[i]) {
      return i + 1;
    }
  }
}

Bits GCD(Bits a, Bits b) {
  Bits *p = &a;
  Bits *q = &b;
  int as = Digit(a) - 1;
  int bs = Digit(b) - 1;

  while (bs >= 0) {
    while (as >= bs) {
      *p ^= *q << (as - bs);
      while (as >= 0 && (*p)[as] == 0) {
        --as;
      }
    }
    swap(p, q);
    swap(as, bs);
  }
  return *p;
}

bool Less(const Bits &a, const Bits &b) {
  REM(i, 0, M) {
    if (a[i] < b[i])
      return true;
    if (a[i] > b[i])
      return false;
  }
  return false;
}

Bits Inc(const Bits &a) {
  Bits ret(a);

  REP(i, 0, M) {
    if (ret[i]) {
      ret[i] = 0;
    } else {
      ret[i] = 1;
      return ret;
    }
  }
}

int N;
Bits X;
Bits A[6];
Mit P[M];
int main() {
  cin >> N >> X;
  REP(i, 0, N) { cin >> A[i]; }
  X = Inc(X);

  P[0] = 1;
  REP(i, 1, M) { P[i] = P[i - 1] * 2; }

  Bits gcd = 0;
  REP(i, 0, N) { gcd = GCD(gcd, A[i]); }

  int xs = Digit(X);
  int gs = Digit(gcd);

  Mit sum = 0;
  REM(i, gs - 1, xs) {
    if (X[i]) {
      sum += P[i - gs + 1];
    }
  }

  Bits Y = 0;
  REM(i, gs - 1, xs) {
    if (Y[i] != X[i]) {
      Y ^= gcd << (i - gs + 1);
    }
  }

  cout << sum + Less(Y, X) << endl;
  return 0;
}


Fixed submission:
#ifndef __INTMOD_H__0001__
#define __INTMOD_H__0001__

#include <cassert>
#include <iostream>
#include <vector>

template <unsigned int Modulus> class IntMod {
  typedef unsigned long long ULL;

private:
  unsigned int value_m;

  void Copy(const IntMod &other) { value_m = other.value_m; }
  bool Modulus_is_valid() { return Modulus != 0; }

public:
  IntMod() { value_m = 0; }
  IntMod(unsigned int value) { value_m = value % Modulus; }
  IntMod(unsigned long long value) { value_m = value % Modulus; }
  IntMod(int value) {
    if (Modulus >= 0x80000000UL) {
      value_m = value >= 0 ? (unsigned int)value % Modulus
                           : Modulus - (unsigned int)(-value);
    } else {
      int tmp = value % (int)Modulus;
      value_m = tmp >= 0 ? tmp : Modulus - (unsigned int)(-tmp);
    }
  }
  IntMod(const IntMod &other) { Copy(other); }
  IntMod &operator=(const IntMod &other) {
    Copy(other);
    return *this;
  }
  bool operator==(const IntMod &right) const {
    return value_m == right.value_m;
  }
  bool operator!=(const IntMod &right) const {
    return value_m != right.value_m;
  }
  IntMod &operator++() {
    ++value_m;
    value_m %= Modulus;
    return *this;
  }
  IntMod &operator--() {
    if (value_m == 0) {
      value_m = Modulus - 1;
    } else {
      --value_m;
    }
    return *this;
  }
  IntMod operator-() const {
    if (value_m == 0)
      return IntMod(0);
    return IntMod(Modulus - value_m);
  }
  IntMod &operator+=(const IntMod &right) {
    ULL sum = (ULL)value_m + right.value_m;
    if (sum >= (ULL)Modulus) {
      value_m = (unsigned int)(sum - (ULL)Modulus);
    } else {
      value_m = (unsigned int)sum;
    }
    return *this;
  }
  IntMod &operator-=(const IntMod &right) {
    (*this) += (-right);
    return *this;
  }
  IntMod operator+(const IntMod &right) const {
    IntMod ret(*this);
    ret += right;
    return ret;
  }
  IntMod operator-(const IntMod &right) const {
    IntMod ret(*this);
    ret -= right;
    return ret;
  }
  IntMod &operator*=(const IntMod &right) {
    ULL tmp = (ULL)value_m * right.value_m;
    value_m = tmp % (ULL)Modulus;
    return *this;
  }
  IntMod &operator/=(const IntMod &right) {
    (*this) *= (right.Inverse());
    return *this;
  }
  IntMod operator*(const IntMod &right) const {
    IntMod ret(*this);
    ret *= right;
    return ret;
  }
  IntMod operator/(const IntMod &right) const {
    IntMod ret(*this);
    ret /= right;
    return ret;
  }
  // for power
  IntMod operator[](unsigned int exp) const { return Pow(exp); }

  /* 素数判定は自分でしろ */
  IntMod Inverse() const { return (*this).Pow(Modulus - 2); }
  IntMod Pow(unsigned int exp) const {
    unsigned int arr[32]; // TODO
    ULL sum = 1;
    arr[0] = value_m;
    for (int i = 1; i < 32; ++i) {
      arr[i] = ((ULL)arr[i - 1] * arr[i - 1]) % (ULL)Modulus;
    }
    for (int i = 0; i < 32; ++i) {
      if (exp & (0x1 << i)) {
        sum *= arr[i];
        sum %= (ULL)Modulus;
      }
    }
    return (unsigned int)sum;
  }
  IntMod Fact() const {
    static std::vector<IntMod> table;
    int factoriand = Get_value();
    if (table.size() > factoriand) {
      return table[factoriand];
    }

    int old_size = table.size();
    table.resize(factoriand + 1);
    for (int i = old_size; i <= factoriand; i++) {
      table[i] = (i == 0 ? 1 : table[i - 1] * i);
    }
    return table[factoriand];
  }
  IntMod Combination(const IntMod &right) const {
    if (value_m < right.value_m)
      throw "okashii";
    return Fact() / ((*this - right).Fact() * right.Fact());
  }
  unsigned int Get_value() const { return value_m; }
  static std::vector<IntMod> Inverse_list(int size) {
    assert(size < Modulus);
    std::vector<IntMod> ret_arr(size + 1);
    ret_arr[1] = 1;
    for (int i = 2; i <= size; ++i) {
      ret_arr[i] =
          ((ULL)(Modulus - Modulus / i) * ret_arr[Modulus % i].Get_value()) %
          Modulus;
    }
    return ret_arr;
  }
};
typedef IntMod<1000000007> MInt;

// template <unsigned int Modulus>
// IntMod<Modulus> operator*(const int left, const IntMod<Modulus>& right) {
//	IntMod<Modulus> ret(left);
//	ret *= right;
//	return ret;
// }
// template <unsigned int Modulus>
// IntMod<Modulus> operator/(const int left, const IntMod<Modulus>& right) {
//	IntMod<Modulus> ret(left);
//	ret /= right;
//	return ret;
// }

#if 0
MInt operator"" _m(unsigned long long num) { return MInt(num); }
#endif

template <unsigned int Modulus>
std::ostream &operator<<(std::ostream &ost, const IntMod<Modulus> &val) {
  ost << val.Get_value();
  return ost;
}

#endif

#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
typedef unsigned int uint;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PP;
#define REP(i, a, n) for (LL i = (a), i##_max = (n); i < i##_max; ++i)
#define REM(i, a, n) for (LL i = (LL)(n)-1, i##min = (a); i >= i##min; --i)
#define ALL(arr) (arr).begin(), (arr).end()
#define FLOAT fixed << setprecision(16)
#define SPEEDUP                                                                \
  {                                                                            \
    cin.tie(NULL);                                                             \
    ios::sync_with_stdio(false);                                               \
  }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream &operator>>(istream &ist, pair<T, U> &right) {
  return ist >> right.first >> right.second;
}
template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &right) {
  return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible &val) {
  fill(dest, dest + N, val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible &val) {
  for (int i = 0; i < M; ++i)
    Fill(dest[i], val);
}
template <class T> T Compare(T left, T right) {
  return left > right ? 1 : (left < right ? -1 : 0);
}
istream &Ignore(istream &ist) {
  string s;
  ist >> s;
  return ist;
}
bool Inside(int i, int j, int h, int w) {
  return i >= 0 && i < h && j >= 0 && j < w;
}
template <class T> T Next() {
  T buf;
  cin >> buf;
  return buf;
}

#ifdef ONLY_MY_ENVIR
#include "Graph.h"
#include "IntMod.h"
#include "Math.h"
#include "Matrix.h"
#include "MinMax.h"
#include "Range.h"
#include "Tree.h"
#include "Union_Find.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream &operator>>(istream &ist, __int128 &val) {
  LL tmp;
  ist >> tmp;
  val = tmp;
  return ist;
}
ostream &operator<<(ostream &ost, __int128 val) {
  LL tmp = val;
  ost << tmp;
  return ost;
}
#endif

#if 1234567891
#include <array>
#include <random>
#include <unordered_map>
#include <unordered_set>
template <typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T>>;
// template <class T>
// auto Is(const T& value) { return [value](const auto& comparand) -> bool {
// return comparand == value; }; }
#endif

const int M = 4064;
using Bits = bitset<M>;
using Mit = IntMod<998244353>;

int Digit(const Bits &a) {
  REM(i, 0, M) {
    if (a[i]) {
      return i + 1;
    }
  }
  return 0;
}

Bits GCD(Bits a, Bits b) {
  Bits *p = &a;
  Bits *q = &b;
  int as = Digit(a) - 1;
  int bs = Digit(b) - 1;

  while (bs >= 0) {
    while (as >= bs) {
      *p ^= *q << (as - bs);
      while (as >= 0 && (*p)[as] == 0) {
        --as;
      }
    }
    swap(p, q);
    swap(as, bs);
  }
  return *p;
}

bool Less(const Bits &a, const Bits &b) {
  REM(i, 0, M) {
    if (a[i] < b[i])
      return true;
    if (a[i] > b[i])
      return false;
  }
  return false;
}

Bits Inc(const Bits &a) {
  Bits ret(a);

  REP(i, 0, M) {
    if (ret[i]) {
      ret[i] = 0;
    } else {
      ret[i] = 1;
      return ret;
    }
  }
}

int N;
Bits X;
Bits A[6];
Mit P[M];
int main() {
  cin >> N >> X;
  REP(i, 0, N) { cin >> A[i]; }
  X = Inc(X);

  P[0] = 1;
  REP(i, 1, M) { P[i] = P[i - 1] * 2; }

  Bits gcd = 0;
  REP(i, 0, N) { gcd = GCD(gcd, A[i]); }

  int xs = Digit(X);
  int gs = Digit(gcd);

  Mit sum = 0;
  REM(i, gs - 1, xs) {
    if (X[i]) {
      sum += P[i - gs + 1];
    }
  }

  Bits Y = 0;
  REM(i, gs - 1, xs) {
    if (Y[i] != X[i]) {
      Y ^= gcd << (i - gs + 1);
    }
  }

  cout << sum + Less(Y, X) << endl;
  return 0;
}