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

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
//---------------------------------------------------------------------------------------------------
typedef long long ll;
int mod = 1000000007;
int add(int x, int y) { return (x += y) >= mod ? x - mod : x; }
template <class... T> int add(int x, T... y) { return add(x, add(y...)); }
int mul(int x, int y) { return 1LL * x * y % mod; }
template <class... T> int mul(int x, T... y) { return mul(x, mul(y...)); }
int sub(int x, int y) { return add(x, mod - y); }
int modpow(int a, long long b) {
  int ret = 1;
  while (b > 0) {
    if (b & 1)
      ret = 1LL * ret * a % mod;
    a = 1LL * a * a % mod;
    b >>= 1;
  }
  return ret;
}
int modinv(int a) { return modpow(a, mod - 2); }
typedef vector<int> Vec;
typedef vector<Vec> Mat;
Vec mulMatVec(Mat a, Vec b) {
  int n = b.size();
  Vec ret(n, 0);
  rep(i, 0, n) rep(j, 0, n) ret[i] = add(ret[i], mul(a[i][j], b[j]));
  return ret;
}
Mat mulMatMat(Mat a, Mat b) {
  int n = a.size();
  Mat ret(n, Vec(n, 0));
  rep(i, 0, n) rep(j, 0, n) rep(k, 0, n) ret[i][j] =
      add(ret[i][j], mul(a[i][k], b[k][j]));
  return ret;
}
Mat mulMatMatForJunkai(Mat a, Mat b) {
  int n = a.size();
  Mat ret(n, Vec(n, 0));
  rep(j, 0, n) rep(k, 0, n) ret[0][j] = add(ret[0][j], mul(a[0][k], b[k][j]));
  rep(i, 1, n) rep(j, 0, n) ret[i][j] = ret[0][(j - i + n) % n];
  return ret;
}
Mat fastpow(Mat x, ll n) {
  Mat ret(x.size(), Vec(x.size(), 0));
  rep(i, 0, x.size()) ret[i][i] = 1;
  while (0 < n) {
    if ((n % 2) == 0) {
      x = mulMatMatForJunkai(x, x);
      n >>= 1;
    } else {
      ret = mulMatMatForJunkai(ret, x);
      --n;
    }
  }
  return ret;
}
void printVec(Vec a) {
  cout << "[\\t";
  rep(i, 0, a.size()) cout << a[i] << "\\t";
  cout << "]" << endl;
}
void printMat(Mat a) { rep(i, 0, a.size()) printVec(a[i]); }
/*---------------------------------------------------------------------------------------------------
????????????????????????????????? ??§?????§
??????????????? ??§?????§ ???????´<_??? ?????? Welcome to My Coding Space!
???????????? ??? ?´_???`??????/??? ???i
?????????????????????????????? ???  |???|
????????? /?????? /??£??£??£??£/??????|
??? ???_(__??????/???    ???/ .| .|????????????
??? ????????????/????????????/??????u??????
---------------------------------------------------------------------------------------------------*/

int N, M, K, P[101010];
//---------------------------------------------------------------------------------------------------
void _main() {
  cin >> N >> M >> K;
  rep(i, 0, M) cin >> P[i];

  Mat m(N, Vec(N, 0));
  Vec v(N, 0);
  v[0] = 1;

  rep(from, 0, N) rep(i, 0, M) {
    int to = (from + P[i]) % N;
    m[to][from]++;
  }
  rep(i, 0, N) m[i][i]++;

  // printMat(m);

  m = fastpow(m, K);
  v = mulMatVec(m, v);
  rep(i, 0, N) printf("%d\\n", v[i]);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
//---------------------------------------------------------------------------------------------------
typedef long long ll;
int mod = 1000000007;
int add(int x, int y) { return (x += y) >= mod ? x - mod : x; }
template <class... T> int add(int x, T... y) { return add(x, add(y...)); }
int mul(int x, int y) { return 1LL * x * y % mod; }
template <class... T> int mul(int x, T... y) { return mul(x, mul(y...)); }
int sub(int x, int y) { return add(x, mod - y); }
int modpow(int a, long long b) {
  int ret = 1;
  while (b > 0) {
    if (b & 1)
      ret = 1LL * ret * a % mod;
    a = 1LL * a * a % mod;
    b >>= 1;
  }
  return ret;
}
int modinv(int a) { return modpow(a, mod - 2); }
typedef vector<int> Vec;
typedef vector<Vec> Mat;
Vec mulMatVec(Mat a, Vec b) {
  int n = b.size();
  Vec ret(n, 0);
  rep(i, 0, n) rep(j, 0, n) ret[i] = add(ret[i], mul(a[i][j], b[j]));
  return ret;
}
Mat mulMatMat(Mat a, Mat b) {
  int n = a.size();
  Mat ret(n, Vec(n, 0));
  rep(i, 0, n) rep(k, 0, n) rep(j, 0, n) ret[i][j] =
      add(ret[i][j], mul(a[i][k], b[k][j]));
  return ret;
}
Mat fastpow(Mat x, ll n) {
  Mat ret(x.size(), Vec(x.size(), 0));
  rep(i, 0, x.size()) ret[i][i] = 1;
  while (0 < n) {
    if ((n % 2) == 0) {
      x = mulMatMat(x, x);
      n >>= 1;
    } else {
      ret = mulMatMat(ret, x);
      --n;
    }
  }
  return ret;
}
void printVec(Vec a) {
  cout << "[\\t";
  rep(i, 0, a.size()) cout << a[i] << "\\t";
  cout << "]" << endl;
}
void printMat(Mat a) { rep(i, 0, a.size()) printVec(a[i]); }
/*---------------------------------------------------------------------------------------------------
????????????????????????????????? ??§?????§
??????????????? ??§?????§ ???????´<_??? ?????? Welcome to My Coding Space!
???????????? ??? ?´_???`??????/??? ???i
?????????????????????????????? ???  |???|
????????? /?????? /??£??£??£??£/??????|
??? ???_(__??????/???    ???/ .| .|????????????
??? ????????????/????????????/??????u??????
---------------------------------------------------------------------------------------------------*/

int N, M, K, P[8010];
//--------------------------------------------------------------------------------------------------
void _main() {
  cin >> N >> M >> K;
  rep(i, 0, M) cin >> P[i];

  Vec v(N, 0);
  v[0] = 1;

  Mat m(N, Vec(N, 0));
  rep(b, 0, N) rep(c, 0, M) {
    int a = (b + P[c]) % N;
    m[a][b]++;
  }
  rep(a, 0, N) m[a][a]++;

  m = fastpow(m, K);
  v = mulMatVec(m, v);

  rep(i, 0, N) printf("%d\\n", v[i]);
}