Problem p01878 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll mod = 1e9 + 7;

typedef vector<vector<ll>> Matrix;

int N, K, C, T;

vector<int> shuffle(vector<int> &v, int a, int b) {
  vector<int> res;
  for (int i = 0; i < b; ++i) {
    res.push_back(v[a + i]);
  }
  for (int i = 0; i < a; ++i) {
    res.push_back(i);
  }
  for (int i = a + b; i < N; ++i) {
    res.push_back(i);
  }
  return res;
}
vector<int> make_shuffle(int a, int b) {
  vector<int> v(N);
  for (int i = 0; i < N; ++i) {
    v[i] = i;
  }
  v = shuffle(v, a, b);
  vector<int> res(N);
  for (int i = 0; i < N; ++i) {
    res[v[i]] = i;
  }
  return res;
}

vector<vector<ll>> make_E() {
  vector<vector<ll>> res(N * 5, vector<ll>(N * 5, 0));
  for (int i = 0; i < N * 5; ++i) {
    res[i][i] = 1;
  }
  return res;
}
vector<vector<ll>> mul(Matrix A, Matrix B) {
  vector<vector<ll>> res(N * 5, vector<ll>(N * 5, 0));
  for (int i = 0; i < N * 5; ++i) {
    for (int j = 0; j < N * 5; ++j) {
      for (int k = 0; k < N * 5; ++k) {
        res[i][j] = (res[i][j] + A[i][k] * B[k][j] % mod);
        if (res[i][j] >= mod)
          res[i][j] -= mod;
      }
    }
  }
  return res;
}

vector<vector<ll>> power(Matrix A, int n) {
  Matrix res = make_E();
  for (int i = 1; i <= n; i <<= 1) {
    if (n & i) {
      res = mul(res, A);
    }
    A = mul(A, A);
  }
  return res;
}

int main() {
  cin >> N >> K >> C >> T;
  vector<vector<vector<ll>>> pat(5, vector<vector<ll>>(N, vector<ll>(N, 0)));
  for (int i = 0; i < K; ++i) {
    int a, b, t;
    cin >> a >> b >> t;
    --a, --t;
    vector<int> to = make_shuffle(a, b);
    for (int j = 0; j < to.size(); ++j) {
      ++pat[t][j][to[j]];
    }
  }
  vector<vector<ll>> G(N * 5, vector<ll>(N * 5, 0));
  for (int t = 0; t < 5; ++t) {
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        int from = i * 5;
        int to = j * 5 + t;
        G[from][to] += pat[t][i][j];
      }
    }
  }
  for (int i = 0; i < N; ++i) {
    for (int t = 1; t < 5; ++t) {
      G[i * 5 + t][i * 5 + t - 1] = 1;
    }
  }
  vector<vector<ll>> g = power(G, T);
  /*	for (int i = 0; i < N*5; ++i) {
                  for (int j = 0; j < N*5; ++j) {
                          cout << g[i][j] << " ";
                  }
                  cout << endl;
          }
  */ cout << g[(C - 1) * 5][0 * 5] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll mod = 1e9 + 7;

typedef vector<vector<ll>> Matrix;

int N, K, C, T;

vector<int> shuffle(vector<int> &v, int a, int b) {
  vector<int> res;
  for (int i = 0; i < b; ++i) {
    res.push_back(v[a + i]);
  }
  for (int i = 0; i < a; ++i) {
    res.push_back(i);
  }
  for (int i = a + b; i < N; ++i) {
    res.push_back(i);
  }
  return res;
}
vector<int> make_shuffle(int a, int b) {
  vector<int> v(N);
  for (int i = 0; i < N; ++i) {
    v[i] = i;
  }
  v = shuffle(v, a, b);
  vector<int> res(N);
  for (int i = 0; i < N; ++i) {
    res[v[i]] = i;
  }
  return res;
}

vector<vector<ll>> make_E() {
  vector<vector<ll>> res(N * 5, vector<ll>(N * 5, 0));
  for (int i = 0; i < N * 5; ++i) {
    res[i][i] = 1;
  }
  return res;
}
vector<vector<ll>> mul(Matrix A, Matrix B) {
  vector<vector<ll>> res(N * 5, vector<ll>(N * 5, 0));
  for (int i = 0; i < N * 5; ++i) {
    for (int j = 0; j < N * 5; ++j) {
      for (int k = 0; k < N * 5; ++k) {
        res[i][j] = (res[i][j] + A[i][k] * B[k][j] % mod) % mod;
      }
    }
  }
  return res;
}

vector<vector<ll>> power(Matrix A, int n) {
  Matrix res = make_E();
  for (int i = 1; i <= n; i <<= 1) {
    if (n & i) {
      res = mul(res, A);
    }
    A = mul(A, A);
  }
  return res;
}

int main() {
  cin >> N >> K >> C >> T;
  vector<vector<vector<ll>>> pat(5, vector<vector<ll>>(N, vector<ll>(N, 0)));
  for (int i = 0; i < K; ++i) {
    int a, b, t;
    cin >> a >> b >> t;
    --a, --t;
    vector<int> to = make_shuffle(a, b);
    for (int j = 0; j < to.size(); ++j) {
      ++pat[t][j][to[j]];
    }
  }
  vector<vector<ll>> G(N * 5, vector<ll>(N * 5, 0));
  for (int t = 0; t < 5; ++t) {
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        int from = i * 5;
        int to = j * 5 + t;
        G[from][to] += pat[t][i][j];
      }
    }
  }
  for (int i = 0; i < N; ++i) {
    for (int t = 1; t < 5; ++t) {
      G[i * 5 + t][i * 5 + t - 1] = 1;
    }
  }
  vector<vector<ll>> g = power(G, T);
  /*	for (int i = 0; i < N*5; ++i) {
                  for (int j = 0; j < N*5; ++j) {
                          cout << g[i][j] << " ";
                  }
                  cout << endl;
          }
  */ cout << g[(C - 1) * 5][0 * 5] << endl;
}