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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

const long long M = 1e9 + 7;
typedef vector<long long> vec;
typedef vector<vec> mat;
struct Matrix {
  int N;
  mat a;
  Matrix(int n) {
    N = n;
    a = mat(N, vec(N));
    for (int i = 0; i < N; i++)
      a[i][i] = 1;
  }
  Matrix pow(long long b) const {
    Matrix res(N);
    Matrix d(N);
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        d[i][j] = a[i][j];
    for (; b > 0; b >>= 1, d = d * d)
      if (b & 1)
        res = d * res;
    return res;
  }
  Matrix operator+(const Matrix &r) const {
    Matrix s(N);
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        s[i][j] = (a[i][j] + r.a[i][j]) % M;
      }
    }
    return s;
  }
  Matrix operator*(const Matrix &r) const {
    Matrix s(N);
    for (int i = 0; i < N; i++)
      s[i][i] = 0;
    const long long modl = 8 * M * M;
    // for(int i=0;i<N;i++){
    for (int k = 0; k < N; k++) {
      for (int j = 0; j < N; j++) {
        s[0][j] += a[0][k] * r.a[k][j];
        if (s[0][j] >= modl)
          s[0][j] -= modl;
      }
    }
    for (int j = 0; j < N; j++)
      s[0][j] %= M;
    //}
    for (int i = 0; i < N - 1; i++) {
      for (int j = 0; j < N; j++) {
        s[i + 1][(j + 1) % N] = s[i][j];
      }
    }
    return s;
  }

  vector<long long> &operator[](int i) { return a[i]; }
};

ll n, m, k;
ll cnt[888];

int main() {
  cin >> n >> m >> k;
  rep(i, m) {
    ll p;
    cin >> p;
    cnt[p % n]++;
  }
  Matrix mat(n);
  rep(i, n) rep(j, n) { mat[i][(i + j) % n] += cnt[j]; }
  Matrix res = mat.pow(k);

  rep(i, n) { cout << res[0][i] << endl; }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

const long long M = 1e9 + 7;
typedef vector<long long> vec;
typedef vector<vec> mat;
struct Matrix {
  int N;
  mat a;
  Matrix(int n) {
    N = n;
    a = mat(N, vec(N));
    for (int i = 0; i < N; i++)
      a[i][i] = 1;
  }
  Matrix pow(long long b) const {
    Matrix res(N);
    Matrix d(N);
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        d[i][j] = a[i][j];
    for (; b > 0; b >>= 1, d = d * d)
      if (b & 1)
        res = d * res;
    return res;
  }
  Matrix operator+(const Matrix &r) const {
    Matrix s(N);
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        s[i][j] = (a[i][j] + r.a[i][j]) % M;
      }
    }
    return s;
  }
  Matrix operator*(const Matrix &r) const {
    Matrix s(N);
    for (int i = 0; i < N; i++)
      s[i][i] = 0;
    const long long modl = 8 * M * M;
    for (int i = 0; i < N; i++) {
      for (int k = 0; k < N; k++) {
        for (int j = 0; j < N; j++) {
          s[i][j] += a[i][k] * r.a[k][j];
          if (s[i][j] >= modl)
            s[i][j] -= modl;
        }
      }
      for (int j = 0; j < N; j++)
        s[i][j] %= M;
    }
    return s;
  }

  vector<long long> &operator[](int i) { return a[i]; }
};

ll n, m, k;
ll cnt[888];

int main() {
  cin >> n >> m >> k;
  rep(i, m) {
    ll p;
    cin >> p;
    cnt[p % n]++;
  }
  Matrix mat(n);
  rep(i, n) rep(j, n) { mat[i][(i + j) % n] += cnt[j]; }
  Matrix res = mat.pow(k);

  rep(i, n) { cout << res[0][i] << endl; }
  return 0;
}