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

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX / 3

ll mod_pow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

void mod_multi(vector<vector<int>> &A, vector<vector<int>> &B, int mod) {
  vector<vector<int>> C(A.size(), vector<int>(B[0].size()));
  for (int i = 0; i < C.size(); ++i)
    for (int j = 0; j < C[i].size(); ++j)
      for (int k = 0; k < A[i].size(); ++k)
        (C[i][j] += (A[i][k] * B[k][j]) % mod) %= mod;
  A = C;
}

void mod_pow(vector<vector<int>> &A, int n, int mod) {
  vector<vector<int>> C(A.size(), vector<int>(A.size(), 0));
  for (int i = 0; i < A.size(); i++)
    C[i][i] = 1;
  while (n > 0) {
    if (n & 1)
      mod_multi(C, A, mod);
    mod_multi(A, A, mod);
    n >>= 1;
  }
  A = C;
}

int getrank(vector<vector<int>> A, int mod) {
  int H = A.size(), W = A[0].size();
  int r = 0;
  for (int x = 0; x < W; x++) {
    int pivot = -1;
    for (int y = r; y < H; y++)
      if (A[y][x]) {
        pivot = y;
        break;
      }
    if (pivot == -1)
      continue;
    swap(A[r], A[pivot]);
    for (int y = r + 1; y < H; y++) {
      int value = A[y][x] * mod_pow(A[r][x], mod - 2, mod) % mod;
      if (value)
        for (int x2 = x; x2 < W; x2++)
          A[y][x2] = ((A[y][x2] - A[r][x2] * value) % mod + mod) % mod;
    }
    r++;
  }
  return r;
}

bool mod_inv_matrix(vector<vector<int>> &A, int mod, vector<vector<int>> &res) {
  int n = A.size();
  vector<vector<int>> B(n, vector<int>(n + n));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      B[i][j] = A[i][j], B[i][n + j] = (i == j);

  for (int x = 0; x < n; x++) {
    int pivot = -1;
    for (int j = x; j < n; j++)
      if (B[j][x]) {
        pivot = j;
        break;
      }
    if (pivot == -1)
      return false;
    swap(B[x], B[pivot]);
    for (int j = 0; j < n; j++)
      if (j != x) {
        int t = B[j][x] * mod_pow(B[x][x], mod - 2, mod) % mod;
        if (t)
          for (int k = x; k < n + n; k++)
            B[j][k] = ((B[j][k] - B[x][k] * t) % mod + mod) % mod;
      }
    int t = mod_pow(B[x][x], mod - 2, mod);
    for (int j = x; j < n + n; j++)
      B[x][j] = (B[x][j] * t) % mod;
  }

  res.clear();
  res.resize(n, vector<int>(n));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      res[i][j] = B[i][n + j];
  return true;
}

int main() {
  int N;
  cin >> N;
  vector<vector<int>> A(N, vector<int>(N));
  rep(i, N) rep(j, N) cin >> A[i][j];
  vector<int> b(N);
  rep(i, N) cin >> b[i];
  int T;
  cin >> T;

  mod_pow(A, T, 2);

  vector<vector<int>> Ab(N, vector<int>(N + 1));
  rep(i, N) rep(j, N + 1) {
    if (j == N)
      Ab[i][j] = b[i];
    else
      Ab[i][j] = A[i][j];
  }

  int rankA = getrank(A, 2);
  int rankAb = getrank(Ab, 2);

  if (rankA != rankAb) {
    cout << "none" << endl;
    return 0;
  }
  if (rankA != N) {
    cout << "ambiguous" << endl;
    return 0;
  }

  vector<vector<int>> invA;
  mod_inv_matrix(A, 2, invA);
  vector<int> x(N);
  rep(i, N) {
    x[i] = 0;
    rep(j, N)(x[i] += invA[i][j] * b[j] % 2) %= 2;
  }

  cout << x[0];
  repl(i, 1, N) cout << " " << x[i];
  cout << endl;

  return 0;
}


Error: 0

Fixed submission:

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX / 3

ll mod_pow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

void mod_multi(vector<vector<int>> &A, vector<vector<int>> &B, int mod) {
  vector<vector<int>> C(A.size(), vector<int>(B[0].size()));
  for (int i = 0; i < C.size(); ++i)
    for (int j = 0; j < C[i].size(); ++j)
      for (int k = 0; k < A[i].size(); ++k)
        (C[i][j] += (A[i][k] * B[k][j]) % mod) %= mod;
  A = C;
}

void mod_pow(vector<vector<int>> &A, int n, int mod) {
  vector<vector<int>> C(A.size(), vector<int>(A.size(), 0));
  for (int i = 0; i < A.size(); i++)
    for (int j = 0; j < A.size(); j++)
      C[i][j] = (i == j);
  while (n > 0) {
    if (n & 1)
      mod_multi(C, A, mod);
    mod_multi(A, A, mod);
    n >>= 1;
  }
  A = C;
}

int getrank(vector<vector<int>> A, int mod) {
  int H = A.size(), W = A[0].size();
  int r = 0;
  for (int x = 0; x < W; x++) {
    int pivot = -1;
    for (int y = r; y < H; y++)
      if (A[y][x]) {
        pivot = y;
        break;
      }
    if (pivot == -1)
      continue;
    swap(A[r], A[pivot]);
    for (int y = r + 1; y < H; y++) {
      int value = A[y][x] * mod_pow(A[r][x], mod - 2, mod) % mod;
      if (value)
        for (int x2 = x; x2 < W; x2++)
          A[y][x2] = ((A[y][x2] - A[r][x2] * value) % mod + mod) % mod;
    }
    r++;
  }
  return r;
}

bool mod_inv_matrix(vector<vector<int>> &A, int mod, vector<vector<int>> &res) {
  int n = A.size();
  vector<vector<int>> B(n, vector<int>(n + n));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      B[i][j] = A[i][j], B[i][n + j] = (i == j);

  for (int x = 0; x < n; x++) {
    int pivot = -1;
    for (int j = x; j < n; j++)
      if (B[j][x]) {
        pivot = j;
        break;
      }
    if (pivot == -1)
      return false;
    swap(B[x], B[pivot]);
    for (int j = 0; j < x; j++)
      if (j != x) {
        int t = B[j][x] * mod_pow(B[x][x], mod - 2, mod) % mod;
        if (t)
          for (int k = x; k < n + n; k++)
            B[j][k] = ((B[j][k] - B[x][k] * t) % mod + mod) % mod;
      }
    int t = mod_pow(B[x][x], mod - 2, mod);
    for (int j = x; j < n + n; j++)
      B[x][j] = (B[x][j] * t) % mod;
  }

  res.clear();
  res.resize(n, vector<int>(n));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      res[i][j] = B[i][n + j];
  return true;
}

int main() {
  int N;
  cin >> N;
  vector<vector<int>> A(N, vector<int>(N));
  rep(i, N) rep(j, N) cin >> A[i][j];
  vector<int> b(N);
  rep(i, N) cin >> b[i];
  int T;
  cin >> T;

  mod_pow(A, T, 2);

  vector<vector<int>> Ab(N, vector<int>(N + 1));
  rep(i, N) rep(j, N + 1) {
    if (j == N)
      Ab[i][j] = b[i];
    else
      Ab[i][j] = A[i][j];
  }

  int rankA = getrank(A, 2);
  int rankAb = getrank(Ab, 2);

  if (rankA != rankAb) {
    cout << "none" << endl;
    return 0;
  }
  if (rankA != N) {
    cout << "ambiguous" << endl;
    return 0;
  }

  vector<vector<int>> invA;
  mod_inv_matrix(A, 2, invA);
  vector<int> x(N);
  rep(i, N) {
    x[i] = 0;
    rep(j, N)(x[i] += invA[i][j] * b[j] % 2) %= 2;
  }

  cout << x[0];
  repl(i, 1, N) cout << " " << x[i];
  cout << endl;

  return 0;
}