Problem p03529 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define X first
#define Y second
#define pb emplace_back
#define FOR(i, a, b) for (int(i) = (a); i < (b); ++(i))
#define EFOR(i, a, b) for (int(i) = (a); i <= (b); ++(i))
#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X))
#define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X))
#define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X))
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
template <class T> using vv = vector<vector<T>>;
template <class T> inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T> inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}
const ll MOD = 1e9 + 7;

struct Val {
  LL num;
  LL dp;
};

int N;
int K;
Val vs[114][21414];
const int MM = 20000;

int main() {
  scanf("%d%d", &N, &K);

  vs[N + 1][0].num = 1;
  vs[N + 1][0].dp = 0;
  rreps(i, 1, N + 1) {
    rep(j, MM + 1) {
      rep(k, K + 1) {
        auto &v = vs[i + 1][j];
        if (k <= i && k + j >= i) {
          int t = (k + j) / i;
          if (j + t > MM)
            continue;
          auto &nv = vs[i][j + t];
          // printf("dp[%d][%d] <- dp[%d][%d]\n", i, j+t, i+1, j);
          // printf("dp : %lld += %lld\n", nv.dp, LL((k+j) % i) * v.num);
          // printf("num: %lld += %lld\n", nv.num, v.num);
          (nv.dp += v.dp) %= MOD;
          (nv.dp += LL((k + j) % i) * v.num) %= MOD;
          (nv.num += v.num) %= MOD;
        } else {
          auto &nv = vs[i][j];
          // printf("dp[%d][%d] <- dp[%d][%d]\n", i, j, i+1, j);
          // printf("dp : %lld += %lld\n", nv.dp, LL((k+j)) * v.num);
          // printf("num: %lld += %lld\n", nv.num, v.num);
          (nv.dp += v.dp) %= MOD;
          (nv.dp += LL(k + j) * v.num) %= MOD;
          (nv.num += v.num) %= MOD;
        }
      }
    }
  }

  /*rreps(i, 1, N+1) {
    printf("dp[%d]:", i);
    rep(j, N*2+1) {
      printf(" (%lld, %lld)", vs[i][j].num, vs[i][j].dp);
    }
    puts("");
  }*/

  LL ans = 0;
  rep(i, MM + 1) {
    ans += vs[1][i].dp;
    ans %= MOD;
  }

  printf("%lld\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
#define X first
#define Y second
#define pb emplace_back
#define FOR(i, a, b) for (int(i) = (a); i < (b); ++(i))
#define EFOR(i, a, b) for (int(i) = (a); i <= (b); ++(i))
#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X))
#define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X))
#define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X))
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
template <class T> using vv = vector<vector<T>>;
template <class T> inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T> inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}
const ll MOD = 1e9 + 7;

struct Val {
  LL num;
  LL dp;
};

int N;
int K;
Val vs[114][21414];
const int MM = 10014;

int main() {
  scanf("%d%d", &N, &K);

  vs[N + 1][0].num = 1;
  vs[N + 1][0].dp = 0;
  rreps(i, 1, N + 1) {
    rep(j, MM + 1) {
      rep(k, K + 1) {
        auto &v = vs[i + 1][j];
        if (k <= i && k + j >= i) {
          int t = (k + j) / i;
          if (j + t > MM)
            continue;
          auto &nv = vs[i][j + t];
          // printf("dp[%d][%d] <- dp[%d][%d]\n", i, j+t, i+1, j);
          // printf("dp : %lld += %lld\n", nv.dp, LL((k+j) % i) * v.num);
          // printf("num: %lld += %lld\n", nv.num, v.num);
          (nv.dp += v.dp) %= MOD;
          (nv.dp += LL((k + j) % i) * v.num) %= MOD;
          (nv.num += v.num) %= MOD;
        } else {
          auto &nv = vs[i][j];
          // printf("dp[%d][%d] <- dp[%d][%d]\n", i, j, i+1, j);
          // printf("dp : %lld += %lld\n", nv.dp, LL((k+j)) * v.num);
          // printf("num: %lld += %lld\n", nv.num, v.num);
          (nv.dp += v.dp) %= MOD;
          (nv.dp += LL(k + j) * v.num) %= MOD;
          (nv.num += v.num) %= MOD;
        }
      }
    }
  }

  /*rreps(i, 1, N+1) {
    printf("dp[%d]:", i);
    rep(j, N*2+1) {
      printf(" (%lld, %lld)", vs[i][j].num, vs[i][j].dp);
    }
    puts("");
  }*/

  LL ans = 0;
  rep(i, MM + 1) {
    ans += vs[1][i].dp;
    ans %= MOD;
  }

  printf("%lld\n", ans);
}