Problem p01736 (C++) — buggy submission, verdict: Runtime Error

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

const Int MAX = 333;
using BS = bitset<MAX * 2>;
using mat = vector<BS>;
void gauss(mat &v) {
  int n = v.size();
  for (Int i = 0; i < n; i++) {
    for (Int k = i; k < n; k++) {
      if (v[k][i]) {
        swap(v[i], v[k]);
        break;
      }
    }
    for (Int k = 0; k < n; k++)
      if (i != k && v[k][i])
        v[k] ^= v[i];
  }
}

int mrank(mat v, int m) {
  int n = v.size();
  int r = 0, c = 0;
  for (int i = 0; i < n; i++) {
    int s = -1;
    while (c < m) {
      for (int j = i; j < n; j++) {
        if (v[j][c]) {
          s = j;
          break;
        }
      }
      if (~s)
        break;
      c++;
    }
    if (c >= m)
      break;

    swap(v[i], v[s]);
    for (int j = 0; j < n; j++)
      if (i != j && v[j][c])
        v[j] ^= v[i];

    r++;
    c++;
  }
  return r;
}

mat mul(const mat &a, const mat &b) {
  int n = a.size();
  vector<vector<int>> tmp(n, vector<int>(n, 0));
  mat res(n, BS(0));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++)
        tmp[i][j] += (a[i][k] & b[k][j]);

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      res[i][j] = tmp[i][j] & 1;

  return res;
}

mat mat_pow(mat v, int k) {
  int n = v.size();
  mat res(n, BS(0));
  for (int i = 0; i < n; i++)
    res[i][i] = 1;
  while (k) {
    if (k & 1)
      res = mul(res, v);
    v = mul(v, v);
    k >>= 1;
  }
  return res;
}

signed main() {
  int n;
  cin >> n;
  mat v(n, BS(0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int k;
      cin >> k;
      v[i][j] = k;
    }
  }
  BS w;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    w[i] = k;
  }
  int t;
  cin >> t;
  v = mat_pow(v, t);

  int a = mrank(v, n);
  for (int i = 0; i < n; i++)
    v[i][n] = w[i];
  int b = mrank(v, n + 1);

  // cout<<n<<":"<<a<<" "<<b<<endl;

  if (a != b) {
    cout << "none" << endl;
    return 0;
  }

  if (a != n) {
    cout << "ambiguous" << endl;
    return 0;
  }

  auto pv = v;

  gauss(v);
  for (int i = 0; i < n; i++)
    cout << v[i][n] << " \\n"[i == n - 1];

  return 0;
}


Error: -11

Fixed submission:

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

const Int MAX = 333;
using BS = bitset<MAX * 2>;
using mat = vector<BS>;
void gauss(mat &v) {
  int n = v.size();
  for (Int i = 0; i < n; i++) {
    for (Int k = i; k < n; k++) {
      if (v[k][i]) {
        swap(v[i], v[k]);
        break;
      }
    }
    for (Int k = 0; k < n; k++)
      if (i != k && v[k][i])
        v[k] ^= v[i];
  }
}

int mrank(mat v, int m) {
  int n = v.size();
  int r = 0, c = 0;
  for (int i = 0; i < n; i++) {
    int s = -1;
    while (c < m) {
      for (int j = i; j < n; j++) {
        if (v[j][c]) {
          s = j;
          break;
        }
      }
      if (~s)
        break;
      c++;
    }
    if (c >= m)
      break;

    swap(v[i], v[s]);
    for (int j = 0; j < n; j++)
      if (i != j && v[j][c])
        v[j] ^= v[i];

    r++;
    c++;
  }
  return r;
}

mat mul(const mat &a, const mat &b) {
  int n = a.size(), m = b[0].size(), l = a[0].size();
  vector<vector<int>> tmp(n, vector<int>(m, 0));
  mat res(n, BS(0));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      for (int k = 0; k < l; k++)
        tmp[i][j] += (a[i][k] & b[k][j]);

  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      res[i][j] = tmp[i][j] & 1;
  return res;
}

mat mat_pow(mat v, int k) {
  int n = v.size();
  mat res(n, BS(0));
  for (int i = 0; i < n; i++)
    res[i][i] = 1;
  while (k) {
    if (k & 1)
      res = mul(res, v);
    v = mul(v, v);
    k >>= 1;
  }
  return res;
}

signed main() {
  int n;
  cin >> n;
  mat v(n, BS(0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int k;
      cin >> k;
      v[i][j] = k;
    }
  }
  BS w;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    w[i] = k;
  }
  int t;
  cin >> t;
  v = mat_pow(v, t);

  int a = mrank(v, n);
  for (int i = 0; i < n; i++)
    v[i][n] = w[i];
  int b = mrank(v, n + 1);

  // cout<<n<<":"<<a<<" "<<b<<endl;

  if (a != b) {
    cout << "none" << endl;
    return 0;
  }

  if (a != n) {
    cout << "ambiguous" << endl;
    return 0;
  }

  auto pv = v;

  gauss(v);
  for (int i = 0; i < n; i++)
    cout << v[i][n] << " \\n"[i == n - 1];

  return 0;
}