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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vvd = vector<vd>;

#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, e) for (ll i = s; i < (ll)e; i++)
#define TEN(x) ((ll)1e##x)

// http://topcoder.g.hatena.ne.jp/iwiwi/20130105/1357363348
ll inverse(ll v, ll mod) {
  ll a = v, b = mod, x = 1, y = 0;
  while (b) {
    ll t = a / b;
    swap(a -= t * b, b);
    swap(x -= t * y, y);
  }
  return (x % mod + mod) % mod;
}

int main() {
#ifdef _WIN32
  ifstream cin("sample.in");
  ofstream cout("sample.out");
#endif
  cin.tie(0); // cin??¨cout?????£????????????
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(50);
  ll n, m, k;
  cin >> n >> m >> k;

  ll mod = TEN(9) + 7;
  vector<ll> powmod(3 * TEN(6));
  powmod[0] = 1;
  FOR(i, 1, 3 * TEN(6)) powmod[i] = powmod[i - 1] * i % mod;
  auto nCk = [&](ll n, ll k) {
    ll v = powmod[n] * inverse(powmod[k] * powmod[n - k] % mod, mod) % mod;
    assert(v >= 0);
    return v;
  };

  ll sum = 0;
  REP(i, k + 1) {
    ll v = nCk(n + m + k * 2, n + i * 2);
    {
      ll c1 = (n + 1) * inverse(n + i + 1, mod) % mod;
      (v *= nCk(n + i * 2, i) * c1 % mod) %= mod;

      ll c2 = (m + 1) * inverse(m + (k - i) + 1, mod) % mod;
      (v *= nCk(m + (k - i) * 2, k - i) * c2 % mod) %= mod;
    }
    (sum += v) %= mod;
  }

  cout << sum << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vvd = vector<vd>;

#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, e) for (ll i = s; i < (ll)e; i++)
#define TEN(x) ((ll)1e##x)

// http://topcoder.g.hatena.ne.jp/iwiwi/20130105/1357363348
ll inverse(ll v, ll mod) {
  ll a = v, b = mod, x = 1, y = 0;
  while (b) {
    ll t = a / b;
    swap(a -= t * b, b);
    swap(x -= t * y, y);
  }
  return (x % mod + mod) % mod;
}

int main() {
#ifdef _WIN32
  ifstream cin("sample.in");
  ofstream cout("sample.out");
#endif
  cin.tie(0); // cin??¨cout?????£????????????
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(50);
  ll n, m, k;
  cin >> n >> m >> k;

  ll mod = TEN(9) + 7;
  vector<ll> powmod(TEN(7));
  powmod[0] = 1;
  FOR(i, 1, TEN(7)) powmod[i] = powmod[i - 1] * i % mod;
  auto nCk = [&](ll n, ll k) {
    ll v = powmod[n] * inverse(powmod[k] * powmod[n - k] % mod, mod) % mod;
    assert(v >= 0);
    return v;
  };

  ll sum = 0;
  REP(i, k + 1) {
    ll v = nCk(n + m + k * 2, n + i * 2);
    {
      ll c1 = (n + 1) * inverse(n + i + 1, mod) % mod;
      (v *= nCk(n + i * 2, i) * c1 % mod) %= mod;

      ll c2 = (m + 1) * inverse(m + (k - i) + 1, mod) % mod;
      (v *= nCk(m + (k - i) * 2, k - i) * c2 % mod) %= mod;
    }
    (sum += v) %= mod;
  }

  cout << sum << endl;

  return 0;
}