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

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
// #define mt make_tuple
// typedef tuple<double,int,double> tp;
// typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
const double PI = acos(-1);
const double EPS = 1e-7;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int MOD;
vvi mul(vvi A, vvi B) {
  vvi C(A.size(), vi(B[0].size()));
  rep(i, A.size()) rep(k, B.size())
      rep(j, B[0].size())(C[i][j] += A[i][k] * B[k][j]) %= MOD;
  return C;
}
vvi pow(vvi A, ll n) {
  vvi B(A.size(), vi(A.size()));
  rep(i, A.size()) B[i][i] = 1;
  while (n) {
    if (n & 1)
      B = mul(B, A);
    A = mul(A, A);
    n >>= 1;
  }
  return B;
}
int main() {
  int n, a, b, c, t;
  while (cin >> n >> MOD >> a >> b >> c >> t, n) {
    vi in(n);
    rep(i, n) cin >> in[i];
    vvi A(n, vi(n));
    rep(i, n) {
      if (i)
        A[i][i - 1] = a;
      A[i][i] = b;
      if (i < n - 1)
        A[i][i + 1] = c;
    }
    A = pow(A, t);
    rep(i, n) {
      if (i)
        cout << " ";
      int sum = 0;
      rep(j, n)(sum += A[i][j] * in[j]) %= MOD;
      cout << sum;
    }
    cout << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
// #define mt make_tuple
// typedef tuple<double,int,double> tp;
// typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
const double PI = acos(-1);
const double EPS = 1e-7;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int main() {
  int n, m, a, b, c, t;
  while (cin >> n >> m >> a >> b >> c >> t, n) {
    vi in(n + 2);
    rep(i, n) cin >> in[i + 1];
    map<vi, int> ma;
    ma[in] = 1;
    int co = 1;
    vvi IN;
    int lop = 0;
    while (1) {
      co++;
      IN.pb(in);
      vi nin(n + 2);
      loop(i, 1, n + 1) nin[i] =
          (a * in[i - 1] + b * in[i] + c * in[i + 1]) % m;
      if (ma[nin]) {
        lop = co - ma[nin];
        break;
      }
      ma[nin] = co;
      in = nin;
    }
    t %= lop;
    rep(i, n - 1) cout << IN[t][i + 1] << " ";
    cout << IN[t][n] << endl;
  }
}