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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

struct UnionFind {
  vector<int> v;
  UnionFind(int n) : v(n) {
    for (int i = 0; i < n; i++)
      v[i] = i;
  }
  int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
  void unite(int x, int y) { v[find(x)] = find(y); }
};

int main() {
  int n, m, a, b, c, t;
  while (cin >> n >> m >> a >> b >> c >> t, n) {
    vi num(n);
    vector<vvi> mat(32, vvi(n, vi(n, 0)));
    REP(i, n)
    REP(j, n) {
      if (i == j + 1)
        mat[0][i][j] = a;
      if (i == j)
        mat[0][i][j] = b;
      if (i == j - 1)
        mat[0][i][j] = c;
    }
    FOR(i, 1, 32)
    REP(j, n)
    REP(k, n)
    REP(l, n)
    mat[i][j][k] = (mat[i][j][k] + mat[i - 1][l][k] * mat[i - 1][j][l]) % m;
    REP(i, n)
    cin >> num[i];
    REP(i, 32) {
      if (t & (1 << i)) {
        vi num2(n, 0);
        REP(j, n) {
          REP(k, n) { num2[j] = (num2[j] + mat[i][j][k] * num[k]) % m; }
        }
        num = num2;
      }
    }
    REP(i, n) {
      if (i != 0)
        cout << ' ';
      cout << num[i];
    }
    cout << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int main() {
  int n, m, a, b, c, t;
  while (cin >> n >> m >> a >> b >> c >> t, n) {
    vector<short> v(n);
    REP(i, n) { cin >> v[i]; }
    map<vector<short>, int> dp;
    dp[v] = 0;
    int loop = t;
    REP(i, loop) {
      vector<short> v2(n);
      REP(j, n) {
        int sum = 0;
        if (j != 0)
          sum += a * v[j - 1];
        sum += b * v[j];
        if (j != n - 1)
          sum += c * v[j + 1];
        v2[j] = sum % m;
      }
      if (dp.find(v2) == dp.end()) {
        dp[v2] = i + 1;
      } else {
        loop = i + 1 + (t - i - 1) % ((i + 1) - dp[v2]);
      }
      v = v2;
      // s		cout << (i + 1) << ": ";
      /*printf("%2d: ",i+1);
      REP(j, n)
      cout << v[j] << " ";
      cout << endl;*/
    }
    REP(j, n)
    cout << v[j] << " ";
    cout << endl;
  }
}