Problem p00906 (C++) — buggy submission, verdict: Memory Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << (DUMP(), __VA_ARGS__).str() << " ["        \\
        << __LINE__ << "]" << endl)
struct DUMP : ostringstream {
  template <class T> DUMP &operator,(const T &t) {
    if (this->tellp())
      *this << ", ";
    *this << t;
    return *this;
  }
};
#else
#define dump(...)
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}

int N, M, A, B, C, T;
int S[55];

#define MCR mat const &
template <typename T> struct mat {
  vector<vector<T>> dat;
  int height, width;
  mat(int height = 0, int width = 0)
      : dat(height, vector<T>(width)), height(height), width(width) {}
  vector<T> const &operator[](int i) const { return dat[i]; };
  vector<T> &operator[](int i) { return dat[i]; };
  mat &operator+=(MCR v) {
    rep(i, height) rep(j, width) dat[i][j] += v[i][j];
    return *this;
  }
  mat &operator-=(MCR v) {
    rep(i, height) rep(j, width) dat[i][j] -= v[i][j];
    return *this;
  }
  mat &operator*=(MCR b) {
    mat res(height, b.width);
    rep(i, height) rep(k, b.height)
        rep(j, b.width)(res[i][j] += dat[i][k] * b[k][j]) %= M;
    return *this = res;
  }
  mat operator+(MCR v) const { return mat(*this) += v; }
  mat operator-(MCR v) const { return mat(*this) -= v; }
  mat operator*(MCR v) const { return mat(*this) *= v; }
  bool operator<(MCR v) const { return dat < v.dat; }
  mat exp(ll t) const {
    int n = height;
    mat e = *this, res(n, n);
    rep(i, n) res[i][i] = 1;
    for (; t; e *= e, t >>= 1)
      if (t & 1)
        res *= e;
    return res;
  }
  mat trans() const {
    mat t(width, height);
    rep(i, width) rep(j, height) t[i][j] = dat[j][i];
    return t;
  }
};

typedef mat<int> matrix;

int main() {
#ifdef LOCAL
  freopen("in", "r", stdin);
#endif
  while (cin >> N >> M >> A >> B >> C >> T && N) {
    matrix a(N, N), x(N, 1);
    rep(i, N) cin >> x[i][0];
    rep(i, N) {
      if (i != 0)
        a[i][i - 1] = A;
      a[i][i] = B;
      if (i != N - 1)
        a[i][i + 1] = C;
    }
    auto p = a.exp(T);
    rep(i, p.dat.size()) { dump(i, p.dat[i]); }
    cout << (p * x).trans()[0] << endl;
  }
}

Error: MLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << (DUMP(), __VA_ARGS__).str() << " ["        \\
        << __LINE__ << "]" << endl)
struct DUMP : ostringstream {
  template <class T> DUMP &operator,(const T &t) {
    if (this->tellp())
      *this << ", ";
    *this << t;
    return *this;
  }
};
#else
#define dump(...)
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}

int N, M, A, B, C, T;
int S[55];

map<size_t, int> hs;

size_t H(int *S) {
  size_t seed = 430985234;
  rep(i, N) { seed ^= S[i] + 0x9e3779b9 + (seed << 6) + (seed >> 2); }
  return seed;
}

void next() {
  static int nS[55];
  rep(j, N) {
    int x = A * (j == 0 ? 0 : S[j - 1]);
    int y = B * S[j];
    int z = C * (j == N - 1 ? 0 : S[j + 1]);
    nS[j] = (x + y + z) % M;
  }
  rep(j, N) S[j] = nS[j];
}

int main() {
  while (cin >> N >> M >> A >> B >> C >> T) {
    hs.clear();
    rep(i, N) cin >> S[i];
    rep(i, T) {
      bool found = false;
      if (!found) {
        next();
        size_t h = H(S);
        if (hs.count(h)) {
          int rem = (T - i - 1) % (i - hs[h]);
          rep(j, rem) next();
          break;
        } else {
          hs[h] = i;
        }
      }
    }
    cout << vi(S, S + N) << endl;
  }
}