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

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 1e9 + 7;

struct Combination {
  int mod;
  vector<int64_t> mfact, rfact;

  Combination(int sz, int mod) : mfact(sz + 1), rfact(sz + 1), mod(mod) {
    mfact[0] = 1;
    for (int i = 1; i < mfact.size(); i++) {
      mfact[i] = mfact[i - 1] * i % mod;
    }
    rfact[sz] = inv(mfact[sz]);
    for (int i = sz - 1; i >= 0; i--) {
      rfact[i] = rfact[i + 1] * (i + 1) % mod;
    }
  }

  int64_t fact(int k) const { return (mfact[k]); }

  int64_t pow(int64_t x, int64_t n) const {
    int64_t ret = 1;
    while (n > 0) {
      if (n & 1)
        (ret *= x) %= mod;
      (x *= x) %= mod;
      n >>= 1;
    }
    return (ret);
  }

  int64_t inv(int64_t x) const { return (pow(x, mod - 2)); }

  int64_t P(int n, int r) const {
    if (r < 0 || n < r)
      return (0);
    return (mfact[n] * rfact[n - r] % mod);
  }

  int64_t C(int p, int q) const {
    if (q < 0 || p < q)
      return (0);
    return (mfact[p] * rfact[q] % mod * rfact[p - q] % mod);
  }

  int64_t H(int n, int r) const {
    if (n < 0 || r < 0)
      return (0);
    return (r == 0 ? 1 : C(n + r - 1, r));
  }
};

int main() {
  Combination beet(500001, mod);
  int N, M, K;
  cin >> N >> M >> K;

  vector<int> latte(K + 1), malta(K + 1);

  vector<int> dp1(N + 2 * K + 1);
  dp1[0] = 1;
  int base = 0;
  for (int i = 0; i <= K; i++) {
    for (int j = 1 + base; j <= N + K + base; j++) {
      dp1[j] += dp1[j - 1];
      if (dp1[j] >= mod)
        dp1[j] -= mod;
    }
    latte[i] = dp1[base + N]; // 1 個削る
    ++base;
  }

  dp1.assign(M + 2 * K + 1, 0);
  dp1[0] = 1;
  base = 0;
  for (int i = 0; i <= K; i++) {
    for (int j = 1 + base; j <= M + K + base; j++) {
      dp1[j] += dp1[j - 1];
      if (dp1[j] >= mod)
        dp1[j] -= mod;
    }
    malta[i] = dp1[base + M];
    ++base;
  }

  int64 ret = 0;
  for (int i = 0; i <= K; i++) {
    (ret += 1LL * latte[i] * malta[K - i] % mod *
            beet.C(N + M + 2 * K, N + 2 * i) % mod) %= mod;
  }

  cout << ret << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 1e9 + 7;

struct Combination {
  int mod;
  vector<int64_t> mfact, rfact;

  Combination(int sz, int mod) : mfact(sz + 1), rfact(sz + 1), mod(mod) {
    mfact[0] = 1;
    for (int i = 1; i < mfact.size(); i++) {
      mfact[i] = mfact[i - 1] * i % mod;
    }
    rfact[sz] = inv(mfact[sz]);
    for (int i = sz - 1; i >= 0; i--) {
      rfact[i] = rfact[i + 1] * (i + 1) % mod;
    }
  }

  int64_t fact(int k) const { return (mfact[k]); }

  int64_t pow(int64_t x, int64_t n) const {
    int64_t ret = 1;
    while (n > 0) {
      if (n & 1)
        (ret *= x) %= mod;
      (x *= x) %= mod;
      n >>= 1;
    }
    return (ret);
  }

  int64_t inv(int64_t x) const { return (pow(x, mod - 2)); }

  int64_t P(int n, int r) const {
    if (r < 0 || n < r)
      return (0);
    return (mfact[n] * rfact[n - r] % mod);
  }

  int64_t C(int p, int q) const {
    if (q < 0 || p < q)
      return (0);
    return (mfact[p] * rfact[q] % mod * rfact[p - q] % mod);
  }

  int64_t H(int n, int r) const {
    if (n < 0 || r < 0)
      return (0);
    return (r == 0 ? 1 : C(n + r - 1, r));
  }
};

int main() {
  Combination beet(500001, mod);
  int N, M, K;
  cin >> N >> M >> K;

  vector<int> latte(K + 1), malta(K + 1);

  vector<int> dp1(N + 2 * K + 1);
  dp1[0] = 1;
  int base = 0;
  for (int i = 0; i <= K; i++) {
    for (int j = 1; j <= N + K; j++) {
      dp1[base + j] += dp1[base + j - 1];
      if (dp1[base + j] >= mod)
        dp1[base + j] -= mod;
    }
    latte[i] = dp1[base + N]; // 1 個削る
    ++base;
  }

  dp1.assign(M + 2 * K + 1, 0);
  dp1[0] = 1;
  base = 0;
  for (int i = 0; i <= K; i++) {
    for (int j = 1; j <= M + K; j++) {
      dp1[base + j] += dp1[base + j - 1];
      if (dp1[base + j] >= mod)
        dp1[base + j] -= mod;
    }
    malta[i] = dp1[base + M];
    ++base;
  }

  int64 ret = 0;
  for (int i = 0; i <= K; i++) {
    (ret += 1LL * latte[i] * malta[K - i] % mod *
            beet.C(N + M + 2 * K, N + 2 * i) % mod) %= mod;
  }

  cout << ret << endl;
}