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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_range(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep_rev(i, s, e) for (int i = (s)-1; i >= (int)(e); i--)
#define sort_vec(v) sort((v).begin(), (v).end())
typedef int64_t lint;
// lint mod = 998244353;
lint mod = 1000000007;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

void app() {
  auto step = [](int i, vector<lint> a) {
    vector<lint> b(16);
    b[0] = a[0];
    rep_range(k, 1, 6) {
      b[k] = a[k];
      if (i % 2 == 0)
        b[k] += b[k - 1];
      b[k] %= mod;
    }
    rep_range(k, 6, 16) {
      b[k] = a[k] + b[k - 1];
      b[k] %= mod;
    }
    return b;
  };

  vector<vector<lint>> s2(16);
  rep(i, 16) {
    vector<lint> a(16, 0);
    a[i] = 1;
    s2[i] = step(2, step(1, a));
  }

  int binmax = 31;
  vector<vector<vector<lint>>> sbin(binmax,
                                    vector<vector<lint>>(16, vector<lint>(16)));
  sbin[1] = s2;
  rep_range(bin, 2, binmax) {
    rep(x, 16) rep(y, 16) {
      lint s = 0;
      rep(k, 16) s += sbin[bin - 1][x][k] * sbin[bin - 1][k][y];
      sbin[bin][x][y] = s % mod;
    }
  }

  int t;
  cin >> t;

  rep(_t, t) {
    int n;
    cin >> n;

    if (n < 5) {
      cout << 0 << endl;
      continue;
    }
    n -= 5;

    vector<lint> now(16, 0);
    now[0] = 1;
    now = step(0, now);
    lint i = 0;
    rep_rev(bin, binmax, 2) {
      if (i + (1 << bin) <= n) {
        vector<lint> tmp(16);
        rep(x, 16) {
          lint s = 0;
          rep(k, 16) s += now[k] * sbin[bin][k][x];
          tmp[x] = s % mod;
        }
        now.swap(tmp);
        i += 1 << bin;
      }
    }

    while (i + 1 <= n) {
      now = step(i + 1, now);
      i++;
      // cout<<i<<endl;
    }

    cout << now[15] << endl;
  }
}

int main() {
  app();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_range(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep_rev(i, s, e) for (int i = (s)-1; i >= (int)(e); i--)
#define sort_vec(v) sort((v).begin(), (v).end())
typedef int64_t lint;
// lint mod = 998244353;
lint mod = 1000000007;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

void app() {
  int t;
  cin >> t;

  rep(_t, t) {
    int n;
    cin >> n;

    if (n < 5) {
      cout << 0 << endl;
      continue;
    }
    n -= 5;

    auto step = [](int i, vector<lint> a) {
      vector<lint> b(16);
      b[0] = a[0];
      rep_range(k, 1, 6) {
        b[k] = a[k];
        if (i % 2 == 0)
          b[k] += b[k - 1];
        b[k] %= mod;
      }
      rep_range(k, 6, 16) {
        b[k] = a[k] + b[k - 1];
        b[k] %= mod;
      }
      return b;
    };

    vector<vector<lint>> s2(16);
    rep(i, 16) {
      vector<lint> a(16, 0);
      a[i] = 1;
      s2[i] = step(2, step(1, a));
    }

    vector<vector<lint>> snsq(16, vector<lint>(16, 0));
    rep(k, 16) snsq[k][k] = 1;
    int sq = 0;
    while (2 * (sq + 1) * (sq + 1) <= n) {
      vector<vector<lint>> tmp(16, vector<lint>(16));
      rep(x, 16) rep(y, 16) {
        lint s = 0;
        rep(k, 16) s += snsq[x][k] * s2[k][y];
        tmp[x][y] = s % mod;
      }
      snsq.swap(tmp);
      sq++;
      // cout<<sq<<endl;
    }

    vector<lint> now(16, 0);
    now[0] = 1;
    now = step(0, now);
    int i = 0;
    while (i + 2 * sq <= n) {
      vector<lint> tmp(16);
      rep(x, 16) {
        lint s = 0;
        rep(k, 16) s += now[k] * snsq[k][x];
        tmp[x] = s % mod;
      }
      now.swap(tmp);
      i += 2 * sq;
      // cout<<i<<endl;
    }

    while (i + 1 <= n) {
      now = step(i + 1, now);
      i++;
      // cout<<i<<endl;
    }

    cout << now[15] << endl;
  }
}

int main() {
  app();
  return 0;
}