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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;

vvi search(int n, int m, int a, int b, int c, int t) {
  vvi r(n);
  for (int i = 0; i < n; i++) {
    r[i].resize(n);
  }
  if (t == 0) {
    for (int i = 0; i < n; i++) {
      r[i][i] = 1;
    }
    return r;
  }
  if (t == 1) {
    for (int i = 0; i < n; i++) {
      if (i - 1 >= 0) {
        r[i - 1][i] = a % m;
      }
      r[i][i] = b % m;
      if (i + 1 < n) {
        r[i + 1][i] = c % m;
      }
    }
    return r;
  }
  vvi ret = search(n, m, a, b, c, t / 2);
  for (int i = 0; i < n; i++) {
    for (int h = 0; h < n; h++) {
      for (int k = 0; k < n; k++) {
        r[k][h] += ret[i][h] * ret[k][i];
        r[k][h] %= m;
      }
    }
  }
  if (t % 2 == 1) {
    for (int i = 0; i < ret.size(); i++) {
      for (int h = 0; h < ret[i].size(); h++) {
        ret[i][h] = 0;
      }
    }
    for (int i = 0; i < n; i++) {
      for (int h = 0; h < n; h++) {
        if (i - 1 >= 0) {
          ret[i - 1][h] += r[i][h] * a % m;
          ret[i - 1][h] %= m;
        }
        ret[i][h] += r[i][h] * b % m;
        ret[i][h] %= m;
        if (i + 1 < n) {
          ret[i + 1][h] += r[i][h] * c;
          ret[i + 1][h] %= m;
        }
      }
    }
    return ret;
  }
  return r;
}

int main() {
  while (1) {

    int n, m, a, b, c, t;
    cin >> n >> m >> a >> b >> c >> t;
    if (n == 0 && m == 0 && a == 0 && b == 0 && c == 0 && t == 0) {
      break;
    }
    swap(a, c);
    vi ans(n);
    vi in(n);
    for (int i = 0; i < n; i++) {
      cin >> in[i];
    }
    vvi vv = search(n, m, a, b, c, t);
    for (int i = 0; i < n; i++) {
      for (int h = 0; h < n; h++) {
        ans[i] += in[h] * vv[i][h] % m;
      }
    }
    for (int i = 0; i < n; i++) {
      ans[i] %= m;
      cout << ans[i];
      if (i != n - 1) {
        cout << " ";
      }
    }
    cout << endl;
  }
  return 0;
}


Error: -6
free(): invalid next size (fast)


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define MK make_pair
#define F first
#define S second

vi kake(int n, vi v) {
  for (int i = 0; i < v.size(); i++) {
    v[i] *= n;
  }
  return v;
}

vi pls(vi a, vi b) {
  for (int i = 0; i < a.size() && i < b.size(); i++) {
    a[i] += b[i];
  }
  return a;
}

vvi search(int n, int m, int a, int b, int c, int t) {
  vvi r(n);
  for (int i = 0; i < n; i++) {
    r[i].resize(n);
  }
  if (t == 0) {
    for (int i = 0; i < n; i++) {
      r[i][i] = 1;
    }
    return r;
  }
  if (t == 1) {
    for (int i = 0; i < n; i++) {
      if (i - 1 >= 0) {
        r[i - 1][i] = a % m;
      }
      r[i][i] = b % m;
      if (i + 1 < n) {
        r[i + 1][i] = c % m;
      }
    }
    return r;
  }
  vvi ret = search(n, m, a, b, c, t / 2);
  for (int i = 0; i < n; i++) {
    for (int h = 0; h < n; h++) {
      for (int k = 0; k < n; k++) {
        r[k][h] += ret[i][h] * ret[k][i];
        r[k][h] %= m;
      }
    }
  }
  if (t % 2 == 1) {
    fill(&ret[0][0], &ret[n - 1][n], 0);
    for (int i = 0; i < n; i++) {
      for (int h = 0; h < n; h++) {
        if (i - 1 >= 0) {
          ret[i - 1][h] += r[i][h] * a % m;
        }
        ret[i][h] += r[i][h] * b % m;
        if (i + 1 < n) {
          ret[i + 1][h] += r[i][h] * c % m;
        }
      }
    }
    for (int i = 0; i < n; i++) {
      for (int h = 0; h < n; h++) {
        ret[i][h] %= m;
      }
    }
    return ret;
  }
  return r;
}

int main() {
  while (1) {

    int n, m, a, b, c, t;
    cin >> n >> m >> a >> b >> c >> t;
    if (n == 0 && m == 0 && a == 0 && b == 0 && c == 0 && t == 0) {
      break;
    }
    swap(a, c);
    vi ans(n);
    vi in(n);
    for (int i = 0; i < n; i++) {
      cin >> in[i];
    }
    vvi vv = search(n, m, a, b, c, t);
    for (int i = 0; i < n; i++) {
      for (int h = 0; h < n; h++) {
        ans[i] += in[h] * vv[i][h] % m;
      }
    }
    for (int i = 0; i < n; i++) {
      ans[i] %= m;
      cout << ans[i];
      if (i != n - 1) {
        cout << " ";
      }
    }
    cout << endl;
  }
  return 0;
}