Problem:
M Hokkaido High School
問題文
北海道高校には $M$ 個の科目があり、それぞれ $1, 2, 3$ の $3$ 段階で成績がつけられます。
各生徒の成績は長さ $M$ の文字列で表され、$i$ 文字目が科目 $i$ の成績を表します。
生徒 $X$ が生徒 $Y$ の「上位互換」であるとは
すべての科目 $i$ について ($X$ の科目 $i$ の成績) $\geq$ ($Y$ の科目 $i$ の成績)
ある科目 $i$ が存在して。($X$ の科目 $i$ の成績) $>$ ($Y$ の科目 $i$ の成績)
の両方がみたされることをいいます。
今、教室には誰もいません。これから $Q$ 人の生徒が順に教室に入ってきます。$i$ 番目の生徒の成績は文字列 $S_i$ で表されます。各生徒は、自分が教室に入ったときに教室に自分の上位互換が存在していると悲しくなります。
$Q$ 人それぞれの生徒について、悲しくなるかどうかを判定してください。
入力
入力は以下の形式で標準入力から与えられる。
$Q$ $M$
$S_1$
$S_2$
$\vdots$
$S_Q$
制約
$1 \leq Q \leq 5\times 10^5$
$1 \leq M \leq 15$
$|S_i| = M$
$S_i$ は
1
、
2
、
3
からなる文字列
出力
長さ $Q$ の文字列 $T$ を $1$ 行に出力せよ。
$T$ の $i$ 文字目は生徒 $i$ が悲しくなるなら
1
、そうでないなら
0
とせよ。
入力例1
5 3
123
321
112
221
333
出力例1
00110
生徒 $1$ が生徒 $3$ の上位互換であるため生徒 $3$ は悲しいです。
生徒 $2$ が生徒 $4$ の上位互換であるため生徒 $4$ は悲しいです。
生徒 $5$ は生徒 $1$ の上位互換ですが、生徒 $1$ が教室に入ったときには生徒 $5$ はいないので生徒 $1$ は悲しくなりません。

Submitted solutions:

code_snippet: #define INF 4'000'000'000'000'000'037LL
#include <bits/stdc++.h>
using namespace std;
namespace {
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using pll = pair<ll, ll>;
#define vc vector
template <class T>
using vvc = vc<vc<T>>;
using vl = vc<ll>;
using vpll = vc<pll>;
#ifdef __SIZEOF_INT128__
using i128 = __int128_t;
using u128 = __uint128_t;
#endif
#define cauto const auto
#define overload4(_1, _2, _3, _4, name, ...) name
#define rep1(i, n) for (ll i = 0, nnnnn = ll(n); i < nnnnn; i++)
#define rep(...) overload4(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define repi1(i, n) for (int i = 0, nnnnn = int(n); i < nnnnn; i++)
#define repi2(i, l, r) for (int i = int(l), rrrrr = int(r); i < rrrrr; i++)
#define repi3(i, l, r, d) for (int i = int(l), rrrrr = int(r), ddddd = int(d); ddddd > 0 ? i < rrrrr : i > rrrrr; i += d)
#define repi(...) overload4(__VA_ARGS__, repi3, repi2, repi1)(__VA_ARGS__)
#define fem(...) for (auto &__VA_ARGS__)
template <class T, class U>
inline bool chmin(T &a, U b) { return a > b ? a = b, true : false; }
template <class T = ll, class U, class V>
inline constexpr T divfloor(U a, V b) { return T(a) / T(b) - (T(a) % T(b) && (T(a) ^ T(b)) < 0); }
template <class T = ll, class U, class V>
inline constexpr T safemod(U a, V b) { return T(a) - T(b) * divfloor<T>(a, b); }
template <class T = ll, class U, class V>
constexpr T ipow(U a, V b)
{
  assert(b >= 0);
  if (b == 0)
    return 1;
  if (a == 0 || a == 1)
    return a;
  if (a < 0 && a == -1)
    return b & 1 ? -1 : 1;
  T res = 1, tmp = a;
  while (true)
  {
    if (b & 1)
      res *= tmp;
    b >>= 1;
    if (b == 0)
      break;
    tmp *= tmp;
  }
  return res;
}
template <class T = ll, class A, class B, class M>
T mul_limited(A a, B b, M m)
{
  assert(a >= 0 && b >= 0 && m >= 0);
  if (b == 0)
    return 0;
  return T(a) > T(m) / T(b) ? T(m) : T(a) * T(b);
}
template <class T = ll, class A, class B>
T mul_limited(A a, B b) { return mul_limited<T>(a, b, INF); }
template <class T = ll, class A, class B, class M>
T pow_limited(A a, B b, M m)
{
  assert(a >= 0 && b >= 0 && m >= 0);
  if (a <= 1 || b == 0)
    return min(ipow<T>(a, b), T(m));
  T res = 1, tmp = a;
  while (true)
  {
    if (b & 1)
    {
      if (res > T(m) / tmp)
        return m;
      res *= tmp;
    }
    b >>= 1;
    if (b == 0)
      break;
    if (tmp > T(m) / tmp)
      return m;
    tmp *= tmp;
  }
  return res;
}
template <class T = ll, class A, class B>
T pow_limited(A a, B b) { return pow_limited<T>(a, b, INF); }
template <class T = ll, class U, class V>
vc<T> base_repr(U val, V base)
{
  assert(val >= 0);
  assert(base >= 2);
  if (val == 0)
    return {0};
  vc<T> a;
  while (val > 0)
  {
    a.emplace_back(val % base);
    val /= base;
  }
  reverse(a.begin(), a.end());
  return a;
}
template <class T = ll, class U, class V>
vc<T> base_repr(U val, V base, int n)
{
  assert(val >= 0);
  assert(base >= 2);
  assert(n >= 0);
  vc<T> a(n);
  repi(i, n)
  {
    a[i] = val % base;
    val /= base;
  }
  reverse(a.begin(), a.end());
  return a;
}
#define ALL(a) (a).begin(), (a).end()
template <class T, size_t d, size_t i = 0, class V>
auto dvec(const V (&sz)[d], const T &init)
{
  if constexpr (i < d)
    return vc(sz[i], dvec<T, d, i + 1>(sz, init));
  else
    return init;
}
template <class V>
V reversed(const V &v) { return V(v.rbegin(), v.rend()); }
#if __cplusplus < 202002L
#else
#endif
template <class V>
void unique(V &v) { v.erase(std::unique(ALL(v)), v.end()); }
template <class V, class U>
void rotate(V &v, U k)
{ 
  const U n = v.size();
  k = (k % n + n) % n;
  std::rotate(v.begin(), v.begin() + k, v.end());
}
template <class T, const T infty = INF>
struct MonoidMin
{
  using S = T;
  static constexpr S op(S a, S b) { return min(a, b); }
};
const vpll DRULgrid = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const vpll DRULplane = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
template <class T>
struct is_random_access_iterator
{
  static constexpr bool value = is_same_v<
    typename iterator_traits<T>::iterator_category,
    random_access_iterator_tag
  >;
};
template <class T>
constexpr bool is_random_access_iterator_v = is_random_access_iterator<T>::value;
#if __cplusplus < 202002L
namespace internal
{
};
#else
#endif
#if __cplusplus < 202002L
#else
inline constexpr ll bit_width(ll x) { return std::bit_width((ull)x); }
inline constexpr ll bit_floor(ll x) { return std::bit_floor((ull)x); }
inline constexpr ll bit_ceil(ll x) { return std::bit_ceil((ull)x); }
inline constexpr ll countr_zero(ll x) { assert(x != 0); return std::countr_zero((ull)x); }
inline constexpr ll popcount(ll x) { return std::popcount((ull)x); }
inline constexpr bool has_single_bit(ll x) { return std::has_single_bit((ull)x); }
#endif
#define dump(...)
#define oj(...) __VA_ARGS__
namespace fastio {
static constexpr uint32_t SIZ = 1 << 17;
char ibuf[SIZ];
char obuf[SIZ];
char out[100];
uint32_t pil = 0, pir = 0, por = 0;
struct Pre {
  char num[10000][4];
  constexpr Pre() : num() {
    for (int i = 0; i < 10000; i++) {
      int n = i;
      for (int j = 3; j >= 0; j--) {
        num[i][j] = n % 10 | '0';
        n /= 10;
      }
    }
  }
} constexpr pre;
inline void load() {
  memcpy(ibuf, ibuf + pil, pir - pil);
  pir = pir - pil + fread(ibuf + pir - pil, 1, SIZ - pir + pil, stdin);
  pil = 0;
  if (pir < SIZ) ibuf[pir++] = '\n';
}
inline void flush() {
  fwrite(obuf, 1, por, stdout);
  por = 0;
}
void rd1(string &x) {
  x.clear();
  char c;
  do {
    if (pil + 1 > pir) load();
    c = ibuf[pil++];
  } while (isspace(c));
  do {
    x += c;
    if (pil == pir) load();
    c = ibuf[pil++];
  } while (!isspace(c));
}
template <typename T>
void rd1_integer(T &x) {
  if (pil + 100 > pir) load();
  char c;
  do
    c = ibuf[pil++];
  while (c < '-');
  bool minus = 0;
  if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
    if (c == '-') { minus = 1, c = ibuf[pil++]; }
  }
  x = 0;
  while ('0' <= c) { x = x * 10 + (c & 15), c = ibuf[pil++]; }
  if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
    if (minus) x = -x;
  }
}
void rd1(ll &x) { rd1_integer(x); }
template <class T, class U>
void rd1(pair<T, U> &p) {
  return rd1(p.first), rd1(p.second);
}
template <size_t N = 0, typename T>
void rd1_tuple(T &t) {
  if constexpr (N < std::tuple_size<T>::value) {
    auto &x = std::get<N>(t);
    rd1(x);
    rd1_tuple<N + 1>(t);
  }
}
template <class... T>
void rd1(tuple<T...> &tpl) {
  rd1_tuple(tpl);
}
template <size_t N = 0, typename T>
void rd1(array<T, N> &x) {
  for (auto &d: x) rd1(d);
}
template <class T>
void rd1(vc<T> &x) {
  for (auto &d: x) rd1(d);
}
void read() {}
template <class H, class... T>
void read(H &h, T &... t) {
  rd1(h), read(t...);
}
void wt1(const char c) {
  if (por == SIZ) flush();
  obuf[por++] = c;
}
void wt1(const string s) {
  for (char c: s) wt1(c);
}
template <typename T>
void wt1_integer(T x) {
  if (por > SIZ - 100) flush();
  if (x < 0) { obuf[por++] = '-', x = -x; }
  int outi;
  for (outi = 96; x >= 10000; outi -= 4) {
    memcpy(out + outi, pre.num[x % 10000], 4);
    x /= 10000;
  }
  if (x >= 1000) {
    memcpy(obuf + por, pre.num[x], 4);
    por += 4;
  } else if (x >= 100) {
    memcpy(obuf + por, pre.num[x] + 1, 3);
    por += 3;
  } else if (x >= 10) {
    int q = (x * 103) >> 10;
    obuf[por] = q | '0';
    obuf[por + 1] = (x - q * 10) | '0';
    por += 2;
  } else
    obuf[por++] = x | '0';
  memcpy(obuf + por, out + outi + 4, 96 - outi);
  por += 96 - outi;
}
template <class T, enable_if_t<is_integral_v<T>, int> = 0>
void wt1(T x) { wt1_integer(x); }
template <class T, class U>
void wt1(const pair<T, U> &val) {
  wt1(val.first);
  wt1(' ');
  wt1(val.second);
}
template <size_t N = 0, typename T>
void wt1_tuple(const T &t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) { wt1(' '); }
    const auto x = std::get<N>(t);
    wt1(x);
    wt1_tuple<N + 1>(t);
  }
}
template <class... T>
void wt1(const tuple<T...> &tpl) {
  wt1_tuple(tpl);
}
template <class T, size_t S>
void wt1(const array<T, S> &val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt1(' ');
    wt1(val[i]);
  }
}
template <class T>
void wt1(const vector<T> &val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt1(' ');
    wt1(val[i]);
  }
}
void print() { wt1('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  wt1(head);
  if (sizeof...(Tail)) wt1(' ');
  print(forward<Tail>(tail)...);
}
} // namespace fastio
struct Dummy {
  Dummy() { atexit(fastio::flush); }
} dummy;
namespace internal
{
template <class... Ts>
void READnodump(Ts &...a) { fastio::read(a...); }
template <class T>
void READVECnodump(int n, vc<T> &v)
{
  v.resize(n);
  READnodump(v);
}
template <class T, class... Ts>
void READVECnodump(int n, vc<T> &v, vc<Ts> &...vs)
{ READVECnodump(n, v), READVECnodump(n, vs...); }
template <class T>
void READVEC2nodump(int n, int m, vvc<T> &v)
{
  v.assign(n, vc<T>(m));
  READnodump(v);
}
template <class T, class... Ts>
void READVEC2nodump(int n, int m, vvc<T> &v, vvc<Ts> &...vs)
{ READVEC2nodump(n, m, v), READVEC2nodump(n, m, vs...); }
template <class T>
void READJAGnodump(int n, vvc<T> &v)
{
  v.resize(n);
  repi(i, n)
  {
    int k;
    READnodump(k);
    READVECnodump(k, v[i]);
  }
}
template <class T, class... Ts>
void READJAGnodump(int n, vvc<T> &v, vvc<Ts> &...vs)
{ READJAGnodump(n, v), READJAGnodump(n, vs...); }
}; // namespace internal
#define READ(...) internal::READnodump(__VA_ARGS__); dump(__VA_ARGS__)
#define IN(T, ...) T __VA_ARGS__; READ(__VA_ARGS__)
#define LL(...) IN(ll, __VA_ARGS__)
#define READVEC(...) internal::READVECnodump(__VA_ARGS__); dump(__VA_ARGS__)
#define VEC(T, n, ...) vc<T> __VA_ARGS__; READVEC(n, __VA_ARGS__)
#define PRINT fastio::print
template <class T, class U, class P>
pair<T, U> operator+=(pair<T, U> &a, const P &b)
{
  a.first += b.first;
  a.second += b.second;
  return a;
}
template <class T, class U, class P>
pair<T, U> operator+(pair<T, U> &a, const P &b) { return a += b; }
template <class T, size_t n, class A>
array<T, n> operator+=(array<T, n> &a, const A &b)
{
  for (size_t i = 0; i < n; i++)
    a[i] += b[i];
  return a;
}
template <class T, size_t n, class A>
array<T, n> operator+(array<T, n> &a, const A &b) { return a += b; }
namespace internal
{
template <size_t... I, class A, class B>
auto tuple_add_impl(A &a, const B &b, const index_sequence<I...>)
{
  ((get<I>(a) += get<I>(b)), ...);
  return a;
}
}; // namespace internal
template <class... Ts, class Tp>
tuple<Ts...> operator+=(tuple<Ts...> &a, const Tp &b)
{ return internal::tuple_add_impl(a, b, make_index_sequence<tuple_size_v<tuple<Ts...>>>{}); }
template <class... Ts, class Tp>
tuple<Ts...> operator+(tuple<Ts...> &a, const Tp &b) { return a += b; }
namespace internal
{
};
mt19937_64 mt;
namespace internal
{
constexpr ll powmod32_constexpr(ll x, ll n, int m)
{
  if (m == 1)
    return 0;
  uint _m = (uint)m;
  ull r = 1;
  ull y = safemod(x, m);
  while (n)
  {
    if (n & 1)
      r = (r * y) % _m;
    y = (y * y) % _m;
    n >>= 1;
  }
  return r;
}
constexpr bool isprime32_constexpr(int n)
{
  if (n <= 1)
    return false;
  if (n == 2 || n == 7 || n == 61)
    return true;
  if (n % 2 == 0)
    return false;
  ll d = n - 1;
  while (d % 2 == 0)
    d /= 2;
  constexpr ll bases[3] = {2, 7, 61};
  for (ll a : bases)
  {
    ll t = d;
    ll y = powmod32_constexpr(a, t, n);
    while (t != n - 1 && y != 1 && y != n - 1)
    {
      y = y * y % n;
      t <<= 1;
    }
    if (y != n - 1 && t % 2 == 0)
      return false;
  }
  return true;
}
template <int n>
constexpr bool isprime32 = isprime32_constexpr(n);
struct barrett32
{
  uint m;
  ull im;
  explicit barrett32(uint m) : m(m), im((ull)(-1) / m + 1) {}
  uint umod() const { return m; }
  uint mul(uint a, uint b) const
  {
    ull z = a;
    z *= b;
    ull x = (ull)((u128(z)*im) >> 64);
    ull y = x * m;
    return (uint)(z - y + (z < y ? m : 0));
  }
};
}
namespace internal
{
#define REF static_cast<mint &>(*this)
#define CREF static_cast<const mint &>(*this)
#define VAL *static_cast<const mint *>(this)
template <class mint>
struct modint_base
{
  mint &operator+=(const mint &rhs)
  {
    mint &self = REF;
    self._v += rhs._v;
    if (self._v >= self.umod())
      self._v -= self.umod();
    return self;
  }
  mint &operator-=(const mint &rhs)
  {
    mint &self = REF;
    self._v -= rhs._v;
    if (self._v >= self.umod())
      self._v += self.umod();
    return self;
  }
  mint &operator/=(const mint &rhs)
  {
    mint &self = REF;
    return self = self * rhs.inv();
  }
  mint &operator++()
  {
    mint &self = REF;
    self._v++;
    if (self._v == self.umod())
      self._v = 0;
    return self;
  }
  mint &operator--()
  {
    mint &self = REF;
    if (self._v == 0)
      self._v = self.umod();
    self._v--;
    return self;
  }
  mint operator++(int)
  {
    mint res = VAL;
    ++REF;
    return res;
  }
  mint operator--(int)
  {
    mint res = VAL;
    --REF;
    return res;
  }
  mint operator+() const { return VAL; }
  mint operator-() const { return mint() - VAL; }
  mint pow(ll n) const
  {
    assert(n >= 0);
    mint x = VAL, r = 1;
    while (n)
    {
      if (n & 1)
        r *= x;
      x *= x;
      n >>= 1;
    }
    return r;
  }
  friend mint operator+(const mint &lhs, const mint &rhs)
  { return mint(lhs) += rhs; }
  friend mint operator-(const mint &lhs, const mint &rhs)
  { return mint(lhs) -= rhs; }
  friend mint operator*(const mint &lhs, const mint &rhs)
  { return mint(lhs) *= rhs; }
  friend mint operator/(const mint &lhs, const mint &rhs)
  { return mint(lhs) /= rhs; }
  friend bool operator==(const mint &lhs, const mint &rhs)
  { return mint(lhs).eq(rhs); }
  friend bool operator!=(const mint &lhs, const mint &rhs)
  { return mint(lhs).neq(rhs); }
private:
  bool eq(const mint &rhs) { return REF._v == rhs._v; }
  bool neq(const mint &rhs) { return REF._v != rhs._v; }
};
}
template <typename T, std::enable_if_t<std::is_base_of_v<internal::modint_base<T>, T>, int> = 0>
void rd1(T &x)
{
  ll a;
  fastio::rd1(a);
  x = a;
}
template <typename T, std::enable_if_t<std::is_base_of_v<internal::modint_base<T>, T>, int> = 0>
void wt1(const T &x) { fastio::wt1(x.val()); }
template <class T = ll>
constexpr tuple<T, T, T> extgcd(const T &a, const T &b)
{
  if (a == 0 && b == 0)
    return {0, 0, 0};
  T x1 = 1, y1 = 0, z1 = a;
  T x2 = 0, y2 = 1, z2 = b;
  while (z2 != 0)
  {
    T q = z1 / z2;
    tie(x1, x2) = make_pair(x2, x1 - q * x2);
    tie(y1, y2) = make_pair(y2, y1 - q * y2);
    tie(z1, z2) = make_pair(z2, z1 - q * z2);
  }
  if (z1 < 0)
    x1 = -x1, y1 = -y1, z1 = -z1;
  return {z1, x1, y1};
}
template <int m>
struct static_modint : internal::modint_base<static_modint<m>>
{
  using mint = static_modint;
private:
  friend struct internal::modint_base<static_modint<m>>;
  uint _v;
  static constexpr uint umod() { return m; }
  static constexpr bool prime = internal::isprime32<m>;
public:
  static constexpr int mod() { return m; }
  static mint raw(int v)
  {
    mint x;
    x._v = v;
    return x;
  }
  static_modint() : _v(0) {}
  template <class T>
  static_modint(T v)
  {
    if constexpr (is_signed_v<T>)
    {
      ll x = (ll)(v % (ll)(umod()));
      if (x < 0)
        x += umod();
      _v = (uint)x;
    }
    else if constexpr (is_unsigned_v<T>)
    {
      _v = (uint)(v % umod());
    }
    else
    {
      static_assert(is_signed_v<T> || is_unsigned_v<T>, "Unsupported Type");
    }
  }
  int val() const { return (int)_v; }
  mint& operator*=(const mint &rhs)
  {
    ull z = _v;
    z *= rhs._v;
    _v = (uint)(z % umod());
    return *this;
  }
  mint inv() const
  {
    if (prime)
    {
      assert(_v != 0);
      return CREF.pow(umod() - 2);
    }
    else
    {
      auto [g, x, y] = extgcd<int>(_v, m);
      assert(g == 1);
      return x;
    }
  }
};
template <int id>
struct dynamic_modint : internal::modint_base<dynamic_modint<id>>
{
  using mint = dynamic_modint;
private:
  friend struct internal::modint_base<dynamic_modint<id>>;
  uint _v;
  static internal::barrett32 bt;
  static uint umod() { return bt.umod(); }
public:
  static int mod() { return (int)(bt.umod()); }
  static mint raw(int v)
  {
    mint x;
    x._v = v;
    return x;
  }
  dynamic_modint() : _v(0) {}
  template <class T>
  dynamic_modint(T v)
  {
    if constexpr (is_signed_v<T>)
    {
      ll x = (ll)(v % (ll)(umod()));
      if (x < 0)
        x += umod();
      _v = (uint)x;
    }
    else if constexpr (is_unsigned_v<T>)
    {
      _v = (uint)(v % umod());
    }
    else
    {
      static_assert(is_signed_v<T> || is_unsigned_v<T>, "Unsupported Type");
    }
  }
  int val() const { return (int)_v; }
  mint& operator*=(const mint &rhs)
  {
    _v = bt.mul(_v, rhs._v);
    return *this;
  }
  mint inv() const
  {
    auto [g, x, y] = extgcd<int>(_v, mod());
    assert(g == 1);
    return x;
  }
};
template <int id>
internal::barrett32 dynamic_modint<id>::bt(998244353);
using modint = dynamic_modint<-1>;
template <class T>
struct is_static_modint : false_type {};
template <int m>
struct is_static_modint<static_modint<m>> : true_type {};
template <class T>
inline constexpr bool is_static_modint_v = is_static_modint<T>::value;
template <class T>
struct is_dynamic_modint : false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : true_type {};
template <class T>
inline constexpr bool is_dynamic_modint_v = is_dynamic_modint<T>::value;
template <class T>
struct PowerTable
{
private:
  decltype(T::mod()) mod;
  T base;
  vc<T> pw;
public:
  PowerTable() {}
  PowerTable(T base) : mod(T::mod()), base(base), pw(1, 1) {}
  void reserve(int n)
  {
    if (mod != T::mod())
    {
      mod = T::mod();
      pw = {1};
    }
    int i = pw.size();
    if (n < i)
      return;
    pw.resize(n + 1);
    for (; i <= n; i++)
      pw[i] = pw[i - 1] * base;
  }
  T pow(int n)
  {
    reserve(n);
    return pw[n];
  }
};
template <class T>
struct Binomial
{
private:
  static decltype(T::mod()) mod;
  static vc<T> fac_, finv_, inv_;
public:
  static void reserve(int n)
  {
    if (mod != T::mod())
    {
      mod = T::mod();
      fac_ = {1, 1}, finv_ = {1, 1}, inv_ = {0, 1};
    }
    int i = fac_.size();
    chmin(n, T::mod() - 1);
    if (n < i)
      return;
    fac_.resize(n + 1), finv_.resize(n + 1), inv_.resize(n + 1);
    for (; i <= n; i++)
    {
      fac_[i] = fac_[i - 1] * T::raw(i);
      inv_[i] = -inv_[T::mod() % i] * T::raw(T::mod() / i);
      finv_[i] = finv_[i - 1] * inv_[i];
    }
  }
  static T inv(T n)
  {
    assert(n != 0);
    reserve(n.val());
    return inv_[n.val()];
  }
};
template <class T> decltype(T::mod()) Binomial<T>::mod{};
template <class T> vc<T> Binomial<T>::fac_{};
template <class T> vc<T> Binomial<T>::finv_{};
template <class T> vc<T> Binomial<T>::inv_{};
using mint = modint;
template <int k, class F, class T>
void tensor_power_array_destructive(const F &linear_map, vc<T> &v)
{
  const int len = v.size();
  if (len == 0)
    return;
  {
    int len_ = len;
    while (len_ % k == 0)
      len_ /= k;
    assert(len_ == 1 && "v.size() must be a power of k");
  }
  for (int d = 1; d < len; d *= k)
  {
    repi(iu, 0, len, d * k) repi(i, iu, iu + d)
    {
      array<T, k> tmp;
      repi(j, k) tmp[j] = v[i + j * d];
      tmp = linear_map(tmp);
      repi(j, k) v[i + j * d] = tmp[j];
    }
  }
}
template <class M, int k>
void zeta_supset_general_destructive(vc<typename M::S> &a)
{
  using S = typename M::S;
  auto linear_map = [&](array<S, k> &arr) -> array<S, k>
  {
    repi(i, k - 2, -1, -1) arr[i] = M::op(arr[i], arr[i + 1]);
    return arr;
  };
  tensor_power_array_destructive<k>(linear_map, a);
}
template <class M, int k>
vc<typename M::S> zeta_supset_general(vc<typename M::S> a)
{
  zeta_supset_general_destructive<M, k>(a);
  return a;
}
void init()
{
  oj(mt.seed(random_device()()));
}
void main2()
{
  LL(Q, M);
  VEC(string, Q, S);
  fem(s : S) rep(i, M) s.at(i)--;
  vl A(Q);
  rep(i, Q) A.at(i) = stol(S.at(i), nullptr, 3);
  dump(S, A);
  vl f(ipow(3, M), INF);
  rep(i, Q) chmin(f.at(A.at(i)), i);
  auto g = zeta_supset_general<MonoidMin<ll>, 3>(f);
  dump(g | cp::index());
  string ans(Q, '0');
  rep(i, Q)
  {
    vl s = reversed(base_repr(A.at(i), 3, M));
    rep(j, M)
    {
      if (s.at(j) == 2)
        continue;
      ll na = A.at(i) + ipow(3, j);
      dump(s, A.at(i), na);
      if (g.at(na) < i)
        ans.at(i) = '1';
    }
  }
  PRINT(ans);
}
void test()
{
}
template <auto init, auto main2, auto test>
struct Main
{
  Main()
  {
    cauto CERR = [](string val, string color)
    {
      string s = "\033[" + color + "m" + val + "\033[m";
      /* コードテストで確認する際にコメントアウトを外す
      cerr << val;
      //*/
    };
    CERR("\n[FAST_IO]\n\n", "32");
    cout << fixed << setprecision(20);
    test();
    init();
    CERR("\n[SINGLE_TESTCASE]\n\n", "36");
    main2();
  }
};
Main<init, main2, test> main_dummy;
}
int main() {}
accuracy: 1
time_ms: 460
memory_kb: 248120
final_rank: 16

code_snippet: // AOJ #3184 Hokkaido High School
// 2025.3.6

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

int M;

struct Node {
    int c[3];
    int sub;
    bool end;
};

vector<Node> tr;

int newNode(){
    Node nd;
    for(int i = 0; i < 3; i++) nd.c[i] = -1;
    nd.sub = 0;
    nd.end = false;
    tr.push_back(nd);
    return (int)tr.size() - 1;
}

bool queryDom(int idx, int d, bool str, const int *v){
    if(d == M) return (str && tr[idx].end);
    for(int i = v[d]; i < 3; i++){
        int nxt = tr[idx].c[i];
        if(nxt == -1) continue;
        bool nstr = str || (i > v[d]);
        if(queryDom(nxt, d+1, nstr, v)) return true;
    }
    return false;
}

void insertTrie(const int *v){
    int idx = 0;
    tr[idx].sub++;
    for (int d = 0; d < M; d++){
        int s = v[d];
        if(tr[idx].c[s] == -1){
            int nxt = newNode();
            tr[idx].c[s] = nxt;
        }
        idx = tr[idx].c[s];
        tr[idx].sub++;
    }
    if(!tr[idx].end) tr[idx].end = true;
}

int removeDom(int idx, int d, bool str, const int *v){
    int rem = 0;
    if(d == M){
        if(str && tr[idx].end){
            tr[idx].end = false;
            rem = 1;
        }
        tr[idx].sub = (tr[idx].end ? 1 : 0);
        return rem;
    }
    for(int i = 0; i <= v[d] && i < 3; i++){
        int nxt = tr[idx].c[i];
        if(nxt == -1) continue;
        bool nstr = str || (i < v[d]);
        rem += removeDom(nxt, d+1, nstr, v);
        if(tr[nxt].sub == 0) tr[idx].c[i] = -1;
    }
    int sum = (tr[idx].end ? 1 : 0);
    for(int i = 0; i < 3; i++){
        int nxt = tr[idx].c[i];
        if(nxt != -1) sum += tr[nxt].sub;
    }
    tr[idx].sub = sum;
    return rem;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    int Q;
    cin >> Q >> M;
    tr.reserve(1000000);
    newNode();

    string ans; ans.resize(Q);
    int v[20];

    for (int qi = 0; qi < Q; qi++){
        string s;
        cin >> s;
        for (int i = 0; i < M; i++) v[i] = s[i] - '1';
        bool sad = false;
        if(tr[0].sub > 0 && queryDom(0, 0, false, v)) sad = true;
        ans[qi] = sad ? '1' : '0';

        if(!sad){
            removeDom(0, 0, false, v);
            int idx = 0; bool found = true;
            for (int i = 0; i < M; i++){
                int d = v[i];
                if(tr[idx].c[d] == -1){ found = false; break; }
                idx = tr[idx].c[d];
            }
            if(found && tr[idx].end);
            else insertTrie(v);
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 300
memory_kb: 7624
final_rank: 1

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

bool mark[14348908];
int st[15];

void less_mark(int num) {
    if (mark[num]) return;
    mark[num] = true;
    int c = num;
    int i = 0;
    while (c) {
        if (c % 3) less_mark(num - st[i]);
        i++;
        c /= 3;
    }
}

int main() {
    int q, m;
    cin >> q >> m;
    st[0] = 1;
    for (int i = 1; i < 15; i++) st[i] = st[i - 1] * 3;
    while (q--) {
        string s;
        cin >> s;
        int num = 0;
        for (int i = 0; i < m; i++) {
            num *= 3;
            num += s[i] - '1';
        }
        if (mark[num]) cout << 1;
        else {
            less_mark(num);
            mark[num] = false;
            cout << 0;
        }
    }
    cout << endl;
}
accuracy: 1
time_ms: 1210
memory_kb: 17432
final_rank: 2

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline ll time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

const int N = 14348907; // 3^15
const int inf = (1<<30);
int a[N],dp[N];

int s3toi(string s){
    int res = 0;
    for(char c:s){
        res = res*3 + (c-'1');
    }
    return res;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,m; cin >> n >> m;
    for(int i=0;i<N;i++){
        a[i] = dp[i] = inf;
    }
    vector<string> s(n);
    for(int i=0;i<n;i++){
        cin >> s[i];
        int u = s3toi(s[i]);
        a[u] = min(a[u],i);
    }
    for(int i=N-1;i>=0;i--){
        int base = 1;
        int k = i;
        int p = 0;
        for(int j=0;j<m;j++){
            int bit = k%3;
            if(bit != 2){
                int c = (k+1)*base + p;
                if(c<N)dp[i] = min(dp[i], a[c]);
                if(c<N)dp[i] = min(dp[i], dp[c]);
            }
            p += base*bit;
            base *= 3;
            k /= 3;
        }
    }
    for(int i=0;i<n;i++){
        int u = s3toi(s[i]);
        if(dp[u] < i){
            cout << "1";
        }
        else{
            cout << "0";
        }
    }
    cout << "\n";
}
accuracy: 1
time_ms: 610
memory_kb: 130804
final_rank: 4

code_snippet: #include <bits/stdc++.h>

#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second

using namespace std;

typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;

template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
T div_floor(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T>
T div_ceil(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a > 0 ? (a - 1) / b + 1 : a / b;
}

template <typename T>
struct coord_comp {
    vector<T> v;
    bool sorted = false;

    coord_comp() {}

    int size() { return v.size(); }

    void add(T x) { v.push_back(x); }

    void build() {
        sort(v.begin(), v.end());
        v.erase(unique(v.begin(), v.end()), v.end());
        sorted = true;
    }

    int get_idx(T x) {
        assert(sorted);
        return lower_bound(v.begin(), v.end(), x) - v.begin();
    }
};

constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

int high[15000000], same[15000000], pow3[16];

int get_bit(int S, int i) { return S / pow3[i] % 3; }

int main() {
    pow3[0] = 1;
    For(i, 1, 16) pow3[i] = pow3[i - 1] * 3;

    int q, m;
    scanf("%d%d", &q, &m);
    rep(S, pow3[m]) high[S] = same[S] = mod;
    int s[q];
    rep(i, q) {
        string t;
        cin >> t;
        s[i] = 0;
        rep(j, m) s[i] += pow3[j] * (t[j] - '1');
        chmin(same[s[i]], i);
    }

    rrep(S, pow3[m]) {
        chmin(same[S], high[S]);
        if (same[S] < mod) {
            rep(i, m) if (get_bit(S, i)) {
                int T = S - pow3[i];
                chmin(high[T], same[S]);
            }
        }
    }

    rep(i, q) printf("%d", high[s[i]] < i);
    printf("\n");
}
accuracy: 1
time_ms: 1050
memory_kb: 120168
final_rank: 11

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 9223372036854775807
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 500005

int Q,M;
int dp[14348907];
int POW[16];
int CODE[SIZE];
string input_str[SIZE];


int makeCode(string tmp_str){

	int ret = 0;

	for(int i = 0; i < M; i++){

		ret = 3*ret+(tmp_str[i]-'1');
	}

	return ret;
}


int main(){

	POW[0] = 1;
	for(int i = 1; i <= 15; i++){

		POW[i] = POW[i-1]*3;
	}

	scanf("%d %d",&Q,&M);

	for(int i = 0; i < POW[M]; i++){

		dp[i] = BIG_NUM;
	}

	for(int i = 0; i < Q; i++){

		cin >> input_str[i];
		CODE[i] = makeCode(input_str[i]);

		dp[CODE[i]] = min(dp[CODE[i]],i);
	}

	for(int state = POW[M]-1; state >= 0; state--){
		for(int loop = M-1; loop >= 0; loop--){
			if((state/POW[loop])%3 == 2)continue;

			dp[state] = min(dp[state],dp[state+POW[loop]]);
		}
	}

	for(int i = 0; i < Q; i++){
		int tmp = BIG_NUM;
		for(int loop = M-1; loop >= 0; loop--){
			if((CODE[i]/POW[loop])%3 == 2)continue;

			tmp = min(tmp,dp[CODE[i]+POW[loop]]);
		}
		if(tmp < i){

			printf("1");
		}else{

			printf("0");
		}
	}
	printf("\n");

	return 0;
}
accuracy: 1
time_ms: 1270
memory_kb: 96444
final_rank: 12

code_snippet: // #pragma GCC optimize("unroll-loops", "omit-frame-pointer", "inline")
// #pragma GCC option("arch=native", "tune=native", "no-zero-upper")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("tree-vectorize","openmp","predictive-commoning")
// #pragma GCC option("D_GLIBCXX_PARALLEL","openmp")

// #pragma GCC optimize("O3")
// #pragma GCC target("avx2")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
// #define int long long
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000              // 2e9
#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)
#define fi first
#define sec second
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define dmp(x) cerr << #x << ": " << x << endl;

template <class T>
void chmin(T &a, const T &b) {
  if (a > b) a = b;
}
template <class T>
void chmax(T &a, const T &b) {
  if (a < b) a = b;
}

template <class T>
using MaxHeap = priority_queue<T>;
template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T>
vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << p.fi << ',' << p.sec;
  return os;
}
template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.fi >> p.sec;
  return is;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i];
    if (i + 1 < vec.size()) os << ' ';
  }
  return os;
}
template <class T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) is >> vec[i];
  return is;
}
void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

#define endl "\n"

template <int MOD> // if inv is needed, this shold be prime.
struct ModInt {
  ll val;
  ModInt() : val(0ll) {}
  ModInt(const ll &v) : val(((v % MOD) + MOD) % MOD) {}
  bool operator==(const ModInt &x) const { return val == x.val; }
  bool operator!=(const ModInt &x) const { return !(*this == x); }
  bool operator<(const ModInt &x) const { return val < x.val; }
  bool operator>(const ModInt &x) const { return val > x.val; }
  bool operator>=(const ModInt &x) const { return !(*this < x); }
  bool operator<=(const ModInt &x) const { return !(*this > x); }
  ModInt operator-() const { return ModInt(MOD - val); }
  ModInt inv() const { return this->pow(MOD - 2); }
  ModInt &operator+=(const ModInt &x) {
    if ((val += x.val) >= MOD) val -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt &x) {
    if ((val += MOD - x.val) >= MOD) val -= MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &x) {
    (val *= x.val) %= MOD;
    return *this;
  }
  ModInt &operator/=(const ModInt &x) { return *this *= x.inv(); };
  ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; }
  ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; }
  ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; }
  ModInt operator/(const ModInt &x) const { return ModInt(*this) /= x; }
  friend istream &operator>>(istream &i, ModInt &x) {
    ll v;
    i >> v;
    x = v;
    return i;
  }
  friend ostream &operator<<(ostream &o, const ModInt &x) {
    o << x.val;
    return o;
  }
  ModInt pow(ll x) const {
    auto res = ModInt(1ll);
    auto b = *this;
    while (x) {
      if (x & 1) res *= b;
      x >>= 1;
      b *= b;
    }
    return res;
  }
};

template <int MOD>
ModInt<MOD> pow(ModInt<MOD> a, ll x) {
  ModInt<MOD> res = ModInt<MOD>(1ll);
  while (x) {
    if (x & 1) res *= a;
    x >>= 1;
    a *= a;
  }
  return res;
}

constexpr int MOD = 1e9 + 7;
// constexpr int MOD = 998244353;
using mint = ModInt<MOD>;

void solve() {
  int Q, M;
  cin >> Q >> M;
  vector<int> pow3(M + 1, 1);
  for (int i = 0; i < M; i++) pow3[i + 1] = pow3[i] * 3;
  vector<int> dp(pow3[M], INF);
  auto encode = [](const string &s) -> int {
    int ret = 0;
    for (int i = 0; i < s.size(); i++) { ret = ret * 3 + (s[i] - '1'); }
    return ret;
  };
  vector<int> grade(Q);
  for (int i = 0; i < Q; i++) {
    string s;
    cin >> s;
    int val = encode(s);
    grade[i] = val;
    chmin(dp[val], i);
  }
  // dmp(dp);
  for (int d = 0; d < M; d++) {
    for (int i = pow3[M] - 1; i >= 0; i--) {
      int dig = (i % pow3[d + 1] - i % pow3[d]) / pow3[d];
      if (dig == 2) continue;
      chmin(dp[i], dp[i + pow3[d]]);
    }
  }
  // dmp(dp);
  for (int i = 0; i < Q; i++) {
    int g = grade[i];
    int min_ind = INF;
    for (int d = 0; d < M; d++) {
      int dig = (g % pow3[d + 1] - g % pow3[d]) / pow3[d];
      if (dig == 2) continue;
      chmin(min_ind, dp[g + pow3[d]]);
    }
    if (min_ind < i)
      cout << 1;
    else
      cout << 0;
  }
  cout << endl;
  return;
}

signed main() {
  fastio();
  solve();
  // int t;
  // cin >> t;
  // while (t--) solve();

  // int t; cin >> t;
  // for(int i=1;i<=t;i++){
  //   cout << "Case #" << i << ": ";
  //   solve();
  // }
  return 0;
}
accuracy: 1
time_ms: 1440
memory_kb: 61208
final_rank: 7

code_snippet: #include <bits/stdc++.h>
#ifdef _DEBUG
#include "_DEBUG.hpp"
#endif
#define int long long
using namespace std;
using P = pair<int, int>;
const int inf = 2e18;
const int mod = 1e9 + 7;

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v) is >> in;
  return is;
}

template <class T>
vector<T> make_vec(size_t a) {
  return vector<T>(a);
}

template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

template <class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
  t = v;
}

template <class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
  for (auto &e : t) fill(e, v);
}

int enc(string s) {
  int res = 0;
  for (int i = 0; i < s.size(); i++) {
    res = res * 3 + (s[i] - '1');
  }
  return res;
}

signed main() {
  int q, m;
  cin >> q >> m;
  vector<string> s(q);
  cin >> s;

  vector<bool> dp(pow(3, m), false);

  auto dfs = [&](auto &&dfs, string &s) -> void {
    if (dp[enc(s)]) return;
    dp[enc(s)] = true;
    for (int i = 0; i < m; i++) {
      if (s[i] == '1') continue;
      s[i]--;
      dfs(dfs, s);
      s[i]++;
    }
  };

  for (int i = 0; i < q; i++) {
    if (dp[enc(s[i])]) {
      cout << "1";
    } else {
      cout << "0";
      for (int j = 0; j < m; j++) {
        if (s[i][j] == '1') continue;
        s[i][j]--;
        dfs(dfs, s[i]);
        s[i][j]++;
      }
    }
  }
  cout << endl;

  return 0;
}
accuracy: 1
time_ms: 2530
memory_kb: 21036
final_rank: 15

code_snippet: #include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

// struct fast_io {
	// fast_io(){
		// std::cin.tie(nullptr);
		// std::ios::sync_with_stdio(false);
	// };
// } fio;

constexpr int MAX = 15'000'000; // < 3^15 = 14348907

int Q, M;
vector<int> S;
vector<int> used(MAX);
int t[20];

int to_num(string &s){
	int res = 0;
	
	for(int i = 0; i < s.size(); i++){
		res = res * 3 + s[i] - '1';
	}
	
	return res;
}

void check(string &s, int num){
	// int num = to_num(s);
	
	// cout<<"s = "<<s<<" num =  "<<num<<" <> "<<to_num(s)<<endl;
	
	if(used[num]) return ;
	used[num] = true;
	
	
	for(int i = 0, j = 1; i < s.size(); i++){
		
		s[i]--;
		// cout<<"i "<<i<<" s = "<<s[i]<<endl;
		if(s[i] <= '0') {
			s[i]++;
			continue;
		}
		num -= t[M-i-1];
		check(s, num);
		num += t[M-i-1];
		s[i]++;
	}
}

signed main(){
	cout<<fixed<<setprecision(10);
	
	// string ans;
	vector<int> ans;
	
	t[0] = 1;
	for(int i = 1; i < 17; i++){
		t[i] = t[i-1] * 3;
	}
	
	cin>>Q>>M;
	
	ans.resize(Q);
	
	for(int i = 0; i < Q; i++){
		int num = 0;
		string s;
		
		cin>>s;
		
		num = to_num(s);
		// cout<<"s = "<<s<<" num =  "<<num<<endl;
		if(used[num]) {
			// ans += "1";
			ans[i] = 1;
			continue;
		} else {
			// ans += "0";
		}
		
		check(s, num);
		
		used[num] = false;
	}
	
	// cout<<ans<<endl;
	
	for(int i = 0; i < ans.size(); i++){
		cout<<ans[i];
	}
	cout<<endl;
	return 0;
}
accuracy: 1
time_ms: 1600
memory_kb: 124028
final_rank: 17

code_snippet: //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;

//----------------------- Print Function ----------------------//

inline void print() {
    cout << '\n';
}
template <typename First, typename... Rest>
void print(const First &first, const Rest &... rest) {
    cout << first << ' ';
    print(rest...);
}

template <typename T>
void print(const T &a) {
    for (auto e : a) cout << e << ' ';
    cout << '\n';
}

//------------------------- Libraries -------------------------//

template<class T>
bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
 
template<class T>
bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

//--------------------------- Solve ---------------------------//

int encode(string s) {
    int res = 0;
    for (int i = s.size()-1; i >= 0; i--) {
        res *= 3;
        res += (int)(s[i] - '1');
    }
    return res;
}

void solve() {
    int Q, M; cin >> Q >> M;
    vector<string> S(Q);
    vector<int> state(Q);
    for (int i = 0; i < Q; i++) {
        cin >> S[i];
        state[i] = encode(S[i]);
    }

    vector<int> three(M+1, 1);
    for (int i = 0; i < M; i++) three[i+1] = three[i]*3;

    vector<int> dp(three[M], Q);
    for (int q = 0; q < Q; q++) {
        chmin(dp[state[q]], q);
    }
    for (int bit = three[M]-1; bit >= 0; bit--) {
        for (int i = 0; i < M; i++) {
            if (bit / three[i] % 3 != 2) {
                chmin(dp[bit], dp[bit+three[i]]);
            }
        }
    }
    for (int q = 0; q < Q; q++) {
        int res = Q;
        for (int i = 0; i < M; i++) {
            if (S[q][i] == '3') continue;
            chmin(res, dp[state[q]+three[i]]);
        }
        cout << (res < q ? 1 : 0);
    }
    cout << '\n';
}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 1
time_ms: 1010
memory_kb: 76856
final_rank: 3

code_snippet: // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

int bit_stoi(string s) {
    int res = 0;
    for(auto c : s) {
        assert('0' <= c and c <= '2');
        res = res*3 + (c - '0');
    }
    return res;
}

string bit_tostring(int v, int len) {
    string res = "";
    for(int i=0; i<len; i++) {
        res += (char)('0' + (v % 3));
        v /= 3;
    }
    return string(res.rbegin(), res.rend());
}

int main() {
    int Q, M; scanf("%d%d", &Q, &M);
    int N = 1;
    for(int i=0; i<M; i++) N *= 3;
    
    vector<string> vs(Q);
    vector<int> values(Q), rec(N, INF), mod(N);
    for(int i=0; i<Q; i++) {
        cin >> vs[i];
        for(auto &c : vs[i]) c--;
        int value = bit_stoi(vs[i]);
        values[i] = value;
        chmin(rec[value], i);
    }
    for(int i=0; i<N; i++) mod[i] = i % 3;

    for(int k=0, p=1; k<M; k++, p*=3) {
        for(int i=N-1; i>=0; i--) {
            int d = (i/p) % 3;
            if(d - 1 >= 0) {
                chmin(rec[i-p], rec[i]);
            }
        }
    }
    
    string ans = "";
    for(int i=0; i<Q; i++) {
        bool ng = false;
        int v = values[i];
        for(int k=0, p=1; k<M; k++, p*=3) {
            int d = mod[v/p];
            if(d == 3) d = 0;
            if(d + 1 < 3) {
                int nv = v + p;
                ng |= (rec[nv] < i);
            }
        }
        ans += (char)('0' + ng);
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 840
memory_kb: 133892
final_rank: 10

code_snippet: // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

int bit_stoi(string s) {
    int res = 0;
    for(auto c : s) {
        assert('0' <= c and c <= '2');
        res = res*3 + (c - '0');
    }
    return res;
}

string bit_tostring(int v, int len) {
    string res = "";
    for(int i=0; i<len; i++) {
        res += (char)('0' + (v % 3));
        v /= 3;
    }
    return string(res.rbegin(), res.rend());
}

int main() {
    int Q, M; scanf("%d%d", &Q, &M);
    int N = 1;
    for(int i=0; i<M; i++) N *= 3;
    
    vector<string> vs(Q);
    vector<int> values(Q), rec(N, INF), mod(N);
    for(int i=0; i<Q; i++) {
        cin >> vs[i];
        for(auto &c : vs[i]) c--;
        int value = bit_stoi(vs[i]);
        values[i] = value;
        chmin(rec[value], i);
    }
    for(int i=0; i<N; i++) mod[i] = i % 3;

    vector<bool> checked(N);
    auto dfs = [&](auto &&f, int v) -> void {
        checked[v] = true;
        for(int i=0, p=1; i<M; i++, p*=3) {
            int d = mod[v/p];
            if(d + 1 < 3) {
                int nv = v + p;
                if(!checked[nv]) f(f, nv);
                chmin(rec[v], rec[nv]);
            }
        }
    };
    dfs(dfs, 0);

    string ans = "";
    for(int i=0; i<Q; i++) {
        bool ng = false;
        int v = values[i];
        for(int k=0, p=1; k<M; k++, p*=3) {
            int d = mod[v/p];
            if(d == 3) d = 0;
            if(d + 1 < 3) {
                int nv = v + p;
                ng |= (rec[nv] < i);
            }
        }
        ans += (char)('0' + ng);
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 2180
memory_kb: 135684
final_rank: 20

code_snippet: #include <iostream>
#include <vector>
#include <string>

// 文字列を3進数でエンコード
int enc(const std::string& s) {
    int ret = 0;
    for (char c : s) {
        ret = ret * 3 + (c - '1');
    }
    return ret;
}

void solve() {
    int q, m;
    std::cin >> q >> m;

    // k = 3^m
    int k = 1;
    for (int i = 0; i < m; ++i) k *= 3;

    std::vector<bool> out(k, false);  // 既に上位互換が存在するか

    // 下位互換を全てtrueに更新する
    auto propagate = [&](auto&& f, std::string& s) -> void {
        if (out[enc(s)]) return;  // 枝刈り

        out[enc(s)] = true;
        for (int i = 0; i < m; ++i) {
            if (s[i] == '1') continue;
            --s[i];
            f(f, s);
            ++s[i];
        }
    };

    while (q--) {
        std::string s;
        std::cin >> s;

        if (out[enc(s)]) {
            std::cout << "1";
            continue;
        }

        std::cout << "0";

        // 下位互換を更新
        for (int i = 0; i < m; ++i) {
            if (s[i] == '1') continue;
            --s[i];
            propagate(propagate, s);
            ++s[i];
        }
    }

    std::cout << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 1
time_ms: 1860
memory_kb: 4924
final_rank: 5

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>

int enc(const std::string& s) {
    int ret = 0;
    for (char c : s) {
        ret = ret * 3 + (c - '1');
    }
    return ret;
}

void solve() {
    int n, m;
    std::cin >> n >> m;

    int k = 1;
    for (int i = 0; i < m; ++i) k *= 3;

    std::vector<bool> out(k, false);
    auto propagate = [&](auto&& f, std::string& s) -> void {
        if (out[enc(s)]) return;

        out[enc(s)] = true;
        for (int i = 0; i < m; ++i) {
            if (s[i] == '1') continue;
            --s[i];
            f(f, s);
            ++s[i];
        }
    };

    while (n--) {
        std::string s;
        std::cin >> s;

        if (out[enc(s)]) {
            std::cout << "1";
            continue;
        }

        std::cout << "0";

        for (int i = 0; i < m; ++i) {
            if (s[i] == '1') continue;
            --s[i];
            propagate(propagate, s);
            ++s[i];
        }
    }

    std::cout << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 1
time_ms: 1870
memory_kb: 4924
final_rank: 6

code_snippet: //#define _GLIBCXX_DEBUG

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

#define endl '\n'
#define lfs cout<<fixed<<setprecision(10)
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define spa << " " <<
#define fi first
#define se second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)
using ll = int;
using ld = long double;
const ll MOD1 = 1e9+7;
const ll MOD9 = 998244353;
const ll INF = 1e9;
using P = pair<ll, ll>;
template<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
template<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}  
template<typename T>void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}};
void debug(vector<string>&v,ll h,ll w){for(ll i=0;i<h;i++){for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}};
template<typename T>void debug(vector<T>&v,ll n){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;};
template<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};
template<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}
template<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << " " << p.second;}
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << " ";cout<<"|"; return os;}
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
string convert(ll n,ll bit){
  string ret(bit,'0');
  for(ll i=0;i<bit;i++)if(n&1LL<<bit-1-i)ret[i]='1';
  return ret;
}
string convert(ll n){
  ll bit=0;
  while(n>=1<<bit)bit++;
  return convert(n,max(1,bit));
}
ll convert(string s){
  ll ret=0;
  ll n=s.size();
  for(ll i=0;i<n;i++)if(s[i]=='1')ret|=1LL<<n-i-1;
  return ret;
}
vector<ll> convert_base(ll n,ll m,ll base){
	vector<ll>ret(m,0);
	for(ll i=0;i<m;i++){
		ret[i]=n%base;
		n/=base;
	}
	return ret;
}
ll convert_base(vector<ll>v,ll base){
  ll tmp=1,ret=0;
  for(ll i=0;i<v.size();i++){
    ret+=v[i]*tmp;
    tmp*=base;
  }
  return ret;
}
int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ll res=0,buf=0;
  bool judge = true;
  ll q,m;cin>>q>>m;
  ll sz=1;
  rep(i,0,m)sz*=3;
  vector<bool>dp(sz,false);
  string ret;
  while(q--){
    string s;cin>>s;
    ll pos=0;
    rep(i,0,m){
      pos*=3;
      pos+=(s[i]-'1');
    }
    if(dp[pos])ret+='1';
    else{
      ret+='0';
      queue<ll>que;
      que.push(pos);
      while(!que.empty()){
        auto p=que.front();
        que.pop();
        auto sp=convert_base(p,m,3);
        ll to=p;
        ll add=1;
        rep(i,0,m){
          if(sp[i]==0){
            add*=3;
            continue;
          }
          to-=add;
          if(!dp[to]){
            dp[to]=true;
            que.push(to);
          }
          to+=add;
          add*=3;
        }
      }
    }
    //debug(dp,sz);
  }
  cout<<ret<<endl;
  return 0;
}
accuracy: 1
time_ms: 2270
memory_kb: 12424
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

/*
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s << endl; }
#include <sys/time.h>
double getTime() {
    struct timeval s;
    gettimeofday(&s, NULL);
    return s.tv_sec + s.tv_usec * 1e-6;
}
*/

inline int conv(const string &s) {
    int res = 0;
    for (int i = s.size()-1; i >= 0; --i) res *= 3, res += (int)(s[i] - '1');
    return res;
}

inline string decode(int bit, int M) {
    string res = "";
    while (bit) res += (char)('1' + (bit%3)), bit /= 3;
    while (res.size() < M) res += "1";
    return res;
}

int main() {
    int N, M;
    cin >> N >> M;
    vector<string> S(N);
    vector<int> bitS(N);
    for (int q = 0; q < N; ++q) cin >> S[q], bitS[q] = conv(S[q]);

    vector<int> three(M+1, 1);
    for (int i = 0; i < M; ++i) three[i+1] = three[i] * 3;
    vector<int> dp(three[M], N);
    for (int q = 0; q < N; ++q) chmin(dp[bitS[q]], q);
    for (int bit = three[M]-1; bit >= 0; --bit) {
        for (int i = 0; i < M; ++i) {
            if (bit / three[i] % 3 == 2) continue;
            chmin(dp[bit], dp[bit + three[i]]);
        }
    }
    for (int q = 0; q < N; ++q) {
        int res = N;
        for (int i = 0; i < M; ++i) {
            if (S[q][i] == '3') continue;
            chmin(res, dp[bitS[q] + three[i]]);
        }
        cout << (res < q ? 1 : 0);
    }
    cout << endl;
}
accuracy: 1
time_ms: 1750
memory_kb: 96160
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s << endl; }
#include <sys/time.h>
double getTime() {
    struct timeval s;
    gettimeofday(&s, NULL);
    return s.tv_sec + s.tv_usec * 1e-6;
}


inline int conv(const string &s) {
    int res = 0;
    for (int i = s.size()-1; i >= 0; --i) res *= 3, res += (int)(s[i] - '1');
    return res;
}

inline string decode(int bit, int M) {
    string res = "";
    while (bit) res += (char)('1' + (bit%3)), bit /= 3;
    while (res.size() < M) res += "1";
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); 

    auto start = getTime();

    int N, M;
    cin >> N >> M;
    vector<string> S(N);
    vector<int> bitS(N);
    for (int q = 0; q < N; ++q) cin >> S[q], bitS[q] = conv(S[q]);

    //COUT(getTime() - start);

    vector<int> three(M+1, 1);
    for (int i = 0; i < M; ++i) three[i+1] = three[i] * 3;
    vector<int> dp(three[M], N);
    for (int q = 0; q < N; ++q) chmin(dp[bitS[q]], q);
    for (int bit = three[M]-1; bit >= 0; --bit) {
        for (int i = 0; i < M; ++i) {
            if (bit / three[i] % 3 == 2) continue;
            chmin(dp[bit], dp[bit + three[i]]);
        }
    }

    //COUT(getTime() - start);
    
    for (int q = 0; q < N; ++q) {
        int res = N;
        for (int i = 0; i < M; ++i) {
            if (S[q][i] == '3') continue;
            chmin(res, dp[bitS[q] + three[i]]);
        }
        cout << (res < q ? 1 : 0);
    }
    cout << endl;
}
accuracy: 1
time_ms: 1240
memory_kb: 88240
final_rank: 9

code_snippet: /**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
using namespace std;

//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int q, m;
bitset<14348908> used;
bitset<14348908> sad;

int f(string x) {
    int pw = 1;
    int ret = 0;
    rep(i, m) {
        ret += (x[i] - '1') * pw;
        pw *= 3;
    }
    return ret;
}

void solve() {
	cin >> q >> m;
    vector<int> ans(q, 0);
    queue<int> que;
    rep(_, q) {
        string s; cin >> s;
        int cur = f(s);
        // cerr << cur << endl;
        if(sad[cur]) {
            ans[_] = 1;
        }
        if(!used[cur]) {
            used[cur] = 1;
            que.push(cur);
            while(!que.empty()) {
                int t = que.front(); que.pop();
                int pw = 1;
                rep(i, m) {
                    int x = (t % (3 * pw)) / pw;
                    if(x >= 1) {
                        t -= pw;
                        if(!used[t]) {
                            que.push(t);
                            used[t] = 1;
                            sad[t] = 1;
                        }
                        if(!sad[t]) {
                            sad[t] = 1;
                        }
                        t += pw;
                    }
                    pw *= 3;
                }
            }
        }
    }
    rep(i, q) {
        cout << ans[i];
    }
    cout << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}
accuracy: 1
time_ms: 1860
memory_kb: 16032
final_rank: 8

code_snippet: #include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <ctime>
#include <complex>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 1020000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << "debug: " << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << "debug: " << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;

int sad[20202020];
int cnt[20202020];
int num[505050];

int main(){
	int q,m; cin >> q >> m;
	vs s(q); rep(i,q) cin >> s[i];
	int p = 1;
	rep(i,m) p *= 3;
	p--;
	rep(i,p+1){
		sad[i] = inf;
		cnt[i] = inf;
	}
	rep(i,q){
		int tmp = 1;
		rep(j,m){
			num[i] += (s[i][j] - 1 - '0') * tmp;
			tmp *= 3;
		}
		chmin(cnt[num[i]],i);
	}
	int tmp = 1;
	rep(i,m){
		for(int trit = p; trit > 0; trit--){
			int a = (trit / tmp) % 3;
			for(int j=1; j<=a; j++){
				chmin(sad[trit - tmp * j], min(sad[trit], cnt[trit]));
			}
		}
		tmp *= 3;
	}
	string ans(q,'@');
	rep(i,q){
		ans[i] = (char)('0' + (sad[num[i]] < i));
	}
	cout << ans << "\n";
}
accuracy: 1
time_ms: 1470
memory_kb: 152508
final_rank: 18

code_snippet: /**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

void solve() {
	int q, m; cin >> q >> m;
    int s = 1;
    rep(_, m) s *= 3;
    vector<int> st(s, inf);
    vector<int> pos(q);
    rep(i, q) {
        string s; cin >> s;
        int w = 0;
        rep(_, m) {
            w *= 3;
            w += s[_] - '1';
        }
        chmin(st[w], i);
        pos[i] = w;
    }
    vector<int> dp(s, inf);
    vector<int> buf(m);
    per(i, s) {
        int now = i;
        per(j, m) {
            buf[j] = now % 3;
            now /= 3; 
        }
        int z = 1;
        per(j, m) {
            if(buf[j] < 2) {
                chmin(dp[i], dp[i + z]);
                chmin(dp[i], st[i + z]);
            }
            z *= 3;
        }
    }
    rep(i, q) {
        if(dp[pos[i]] < i) cout << 1;
        else cout << 0;
    }
    cout << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}
accuracy: 1
time_ms: 770
memory_kb: 230988
final_rank: 19