alexjercan/bugnet huggingface dataset:
Problem p01450 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;

int dp[2][10002][202];

int main() {
  int N, W;
  cin >> N >> W;

  vector<int> w(N);
  for (int i = 0; i < N; i++) {
    cin >> w[i];
  }
  sort(w.begin(), w.end());
  w.push_back(0);
  reverse(w.begin(), w.end());

  memset(dp, 0, sizeof(dp));
  int cur = 0;
  dp[cur][0][0] = 1;
  for (int i = 1; i <= N; i++) {
    int next = cur ^ 1;
    for (int j = 0; j <= W; j++) {
      dp[next][j][i] = 0;
      for (int k = 0; k < i; k++) {
        dp[next][j][i] += dp[cur][j][k];
        dp[next][j][i] %= MOD;
      }
    }
    for (int j = W; j >= 0; j--) {
      for (int k = 0; k < i; k++) {
        if (j - w[i] >= 0)
          dp[next][j][k] = dp[cur][j - w[i]][k];
        else
          dp[next][j][k] = 0;
      }
    }
    cur = next;
  }

  ll ans = 0;
  for (int j = 0; j <= W; j++) {
    ans += dp[cur][j][0];
    for (int k = 1; k <= N; k++) {
      if (W - j < w[k])
        ans += dp[cur][j][k];
    }
  }

  cout << ans % MOD << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;

int dp[2][202][10002];

int main() {
  int N, W;
  cin >> N >> W;

  vector<int> w(N);
  for (int i = 0; i < N; i++) {
    cin >> w[i];
  }
  sort(w.begin(), w.end());
  w.push_back(0);
  reverse(w.begin(), w.end());

  memset(dp, 0, sizeof(dp));
  int cur = 0;
  dp[cur][0][0] = 1;
  for (int i = 1; i <= N; i++) {
    int next = cur ^ 1;
    for (int j = 0; j <= W; j++) {
      dp[next][j][i] = 0;
      for (int k = 0; k < i; k++) {
        dp[next][j][i] += dp[cur][j][k];
      }
    }
    for (int j = W; j >= 0; j--) {
      for (int k = 0; k < i; k++) {
        if (j - w[i] >= 0)
          dp[next][j][k] = dp[cur][j - w[i]][k];
        else
          dp[next][j][k] = 0;
      }
    }
    cur = next;
  }

  ll ans = 0;
  for (int j = 0; j <= W; j++) {
    ans += dp[cur][j][0];
    for (int k = 1; k <= N; k++) {
      if (W - j < w[k])
        ans += dp[cur][j][k];
    }
  }
  cout << ans % MOD << endl;

  return 0;
}
Problem p01450 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#define MOD 1000000007
using namespace std;

int main() {
  int n, W, w[200], sum[201] = {}, res = 0;
  static int dp[2][201][10001] = {};
  dp[0][0][0] = 1;
  cin >> n >> W;
  for (int i = 0; i < n; i++)
    cin >> w[i];
  sort(w, w + n);
  for (int i = 0; i < n; i++)
    sum[i + 1] = sum[i] + w[i];
  if (sum[n] <= W) {
    cout << 1 << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    int now = i % 2, prev = (i + 1) % 2;
    for (int j = 1; j < i; j++) {
      for (int k = 0; k <= W; k++) {
        if (k - w[i - 1] >= 0)
          dp[now][j][k] = (dp[prev][j][k - w[i - 1]] + dp[prev][j][k]) % MOD;
        else
          dp[now][j][k] = dp[prev][j][k];
      }
    }
    for (int j = 0; j <= W; j++) {
      dp[now][i][j] = dp[prev][0][j];
    }
    if (sum[i] <= W)
      dp[now][0][sum[i]] = 1;
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= W; k++)
        dp[prev][j][k] = 0;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = W - w[i - 1] + 1; j <= W; j++) {
      // cout << i << " " << j << " " << dp[n][i][j] << " " << endl;
      res = (res + dp[n % 2][i][j]) % MOD;
    }
  }
  cout << res << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define MOD 1000000007
using namespace std;

int main() {
  int n, W, w[200], sum[201] = {}, res = 0;
  static int dp[201][201][10001] = {};
  dp[0][0][0] = 1;
  cin >> n >> W;
  for (int i = 0; i < n; i++)
    cin >> w[i];
  sort(w, w + n);
  for (int i = 0; i < n; i++)
    sum[i + 1] = sum[i] + w[i];
  if (sum[n] <= W) {
    cout << 1 << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++) {
      for (int k = 0; k <= W; k++) {
        if (k - w[i - 1] >= 0)
          dp[i][j][k] = (dp[i - 1][j][k - w[i - 1]] + dp[i - 1][j][k]) % MOD;
        else
          dp[i][j][k] = dp[i - 1][j][k];
      }
    }
    for (int j = 0; j <= W; j++) {
      dp[i][i][j] = dp[i - 1][0][j];
    }
    if (sum[i] <= W)
      dp[i][0][sum[i]] = 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = W - w[i - 1] + 1; j <= W; j++) {
      // cout << i << " " << j << " " << dp[n][i][j] << " " << endl;
      res = (res + dp[n][i][j]) % MOD;
    }
  }
  cout << res << endl;
  return 0;
}
Problem p01450 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
int n, w, g[200];
ll dp[2][201][10001], mod = 1e9 + 7;
int main() {
  cin >> n >> w;
  rep(i, n) cin >> g[i];
  sort(g, g + n);
  dp[0][0][0] = 1;
  ll ans = 0;
  rep(i, n) {
    rep(j, i + 1) rep(k, w + 1) dp[1 - i % 2][j][k] = 0;
    rep(j, i + 1) {
      rep(k, w + 1) {
        dp[1 - i % 2][j][k] += dp[i % 2][j][k];
        if (k + g[i] <= w) {
          if (i == j)
            dp[1 - i % 2][j + 1][k + g[i]] += dp[i % 2][j][k];
          else
            dp[1 - i % 2][j][k + g[i]] += dp[i % 2][j][k];
        }
      }
    }
    rep(j, i + 1) rep(k, w + 1) if (dp[1 - i % 2][j][k] >= mod)
        dp[1 - i % 2][j][k] %= mod;
  }
  rep(k, w + 1) ans += dp[n % 2][n][k];
  rep(j, n) rep(k, w + 1) if (g[j] > w - k) ans += dp[n % 2][j][k];
  cout << ans % mod << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
int n, w, g[200];
ll dp[2][101][10001], mod = 1e9 + 7;
int main() {
  cin >> n >> w;
  rep(i, n) cin >> g[i];
  sort(g, g + n);
  dp[0][0][0] = 1;
  ll ans = 0;
  rep(i, n) {
    rep(j, i + 1) rep(k, w + 1) dp[1 - i % 2][j][k] = 0;
    rep(j, i + 1) {
      rep(k, w + 1) {
        dp[1 - i % 2][j][k] += dp[i % 2][j][k];
        if (k + g[i] <= w) {
          if (i == j)
            dp[1 - i % 2][j + 1][k + g[i]] += dp[i % 2][j][k];
          else
            dp[1 - i % 2][j][k + g[i]] += dp[i % 2][j][k];
        }
      }
    }
    rep(j, i + 1) rep(k, w + 1) if (dp[1 - i % 2][j][k] >= mod)
        dp[1 - i % 2][j][k] %= mod;
  }
  rep(k, w + 1) ans += dp[n % 2][n][k];
  rep(j, n) rep(k, w + 1) if (g[j] > w - k) ans += dp[n % 2][j][k];
  cout << ans << endl;
}
Problem p01452 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define llint long long
#define mod 1000000007

using namespace std;

llint fact[300005], fact_inv[300005];

llint pow(llint a, llint n) {
  if (n == 0)
    return 1;
  if (n % 2) {
    return ((a % mod) * (pow(a, n - 1) % mod)) % mod;
  } else {
    return pow((a * a) % mod, n / 2) % mod;
  }
}

void make_fact() {
  llint val = 1;
  fact[0] = 1;
  for (int i = 1; i < 300005; i++) {
    val *= i;
    val %= mod;
    fact[i] = val;
  }
  for (int i = 0; i < 300005; i++) {
    fact_inv[i] = pow(fact[i], mod - 2);
  }
}

llint comb(llint n, llint k) {
  llint ret = 1;
  ret *= fact[n];
  ret *= fact_inv[k], ret %= mod;
  ret *= fact_inv[n - k], ret %= mod;
  return ret;
}

llint N, M, K;

int main(void) {
  make_fact();
  cin >> N >> M >> K;

  llint ans = 0;
  for (int i = 0; i <= K; i++) {
    llint tmp = comb(N + M + 2 * K, N + 2 * i);
    tmp *= (comb(N + 2 * i, i) - comb(N + 2 * i, i - 1) + mod) % mod;
    tmp %= mod;
    tmp *= (comb(M + 2 * (K - i), K - i) - comb(M + 2 * (K - i), (K - i) - 1) +
            mod) %
           mod;
    tmp %= mod;
    ans += tmp;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#define llint long long
#define mod 1000000007

using namespace std;

llint fact[200005], fact_inv[200005];

llint pow(llint a, llint n) {
  if (n == 0)
    return 1;
  if (n % 2) {
    return ((a % mod) * (pow(a, n - 1) % mod)) % mod;
  } else {
    return pow((a * a) % mod, n / 2) % mod;
  }
}

void make_fact() {
  llint val = 1;
  fact[0] = 1;
  for (int i = 1; i < 200005; i++) {
    val *= i;
    val %= mod;
    fact[i] = val;
  }
  for (int i = 0; i < 200005; i++) {
    fact_inv[i] = pow(fact[i], mod - 2);
  }
}

llint comb(llint n, llint k) {
  llint ret = 1;
  ret *= fact[n];
  ret *= fact_inv[k], ret %= mod;
  ret *= fact_inv[n - k], ret %= mod;
  return ret;
}

llint N, M, K;

int main(void) {
  make_fact();
  cin >> N >> M >> K;

  llint ans = 0;
  for (int i = 0; i <= K; i++) {
    llint tmp = comb(N + M + 2 * K, N + 2 * i);
    tmp *= (comb(N + 2 * i, i) - comb(N + 2 * i, i - 1) + mod) % mod;
    tmp %= mod;
    tmp *= (comb(M + 2 * (K - i), K - i) - comb(M + 2 * (K - i), (K - i) - 1) +
            mod) %
           mod;
    tmp %= mod;
    ans += tmp;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p01452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define all(a) (a).begin(), (a).end()
#define INIT(a) memset((a), 0, sizeof(a))
#define fs first
#define sc second
#define pb push_back
#define sz size()
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

const static int INF = 1e8;
const static D EPS = 1e-8;

const ll mod = 1e9 + 7;

ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

ll inv[10100];

inline ll mod_inverse(ll a, ll m) {
  ll x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

inline ll cataran(ll n, ll k) {
  if (k == 0)
    return 1;
  ll res = n + 1 - k;
  rep(i, k - 1) {
    (res *= n + i + 2) %= mod;
    (res *= inv[i + 2]) %= mod;
  }
  return res;
}

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  rep(i, k) { inv[i + 1] = mod_inverse(i + 1, mod); }

  ll res = 0, comb = 1;
  rep(i, n) {
    (comb *= n + m + 2 * k - i) %= mod;
    (comb *= mod_inverse(i + 1, mod)) %= mod;
  }

  rep(x, k + 1) {
    ll y = k - x;
    ll tmp = (cataran(n + x, x) * cataran(m + y, y)) % mod;
    (tmp *= comb) %= mod;

    (res += tmp) %= mod;

    (comb *= m + 2 * k - 2 * x) %= mod;
    (comb *= m + 2 * k - 2 * x - 1) %= mod;
    (comb *= mod_inverse(n + 1 + 2 * x, mod)) %= mod;
    (comb *= mod_inverse(n + 2 + 2 * x, mod)) %= mod;
  }
  cout << res << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define all(a) (a).begin(), (a).end()
#define INIT(a) memset((a), 0, sizeof(a))
#define fs first
#define sc second
#define pb push_back
#define sz size()
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

const static int INF = 1e8;
const static D EPS = 1e-8;

const ll mod = 1e9 + 7;

ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

inline ll mod_inverse(ll a, ll m) {
  ll x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

inline ll cataran(ll n, ll k) {
  if (k == 0)
    return 1;
  ll res = n + 1 - k;
  rep(i, k - 1) {
    (res *= n + i + 2) %= mod;
    (res *= mod_inverse(i + 2, mod)) %= mod;
  }
  return res;
}

inline ll comb(ll n, ll k) {
  if (n < 2 * k)
    k = n - k;
  ll res = 1;
  rep(i, k) {
    (res *= n - i) %= mod;
    (res *= mod_inverse(i + 1, mod)) %= mod;
  }
  return res;
}

int main() {
  ll n, m, k;
  cin >> n >> m >> k;

  ll res = 0;
  rep(x, k + 1) {
    ll y = k - x;
    ll tmp = cataran(n + x, x);
    (tmp *= cataran(m + y, y)) %= mod;
    (tmp *= comb(n + m + 2 * k, m + 2 * y)) %= mod;

    (res += tmp) %= mod;
  }
  cout << res << endl;
}
Problem p01452 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>

using namespace std;
typedef long long ll;

const ll mod = 1000000007;

int memo[300000] = {0};

void init() {
  memo[0] = 1;
  ll a = 1;
  for (int i = 1; i < 300000; i++) {
    a *= i;
    a %= mod;
    memo[i] = a;
  }
}

ll mypow(ll a, ll b) {
  if (b == 1)
    return a;
  if (b == 0)
    return 1;
  ll r = mypow(a, b / 2);
  r = (r * r) % mod;
  if (b % 2 == 1)
    r *= a;
  r %= mod;
  return r;
}

ll moddiv(ll a, ll b) { return (a * mypow(b, mod - 2)) % mod; }

ll c(int n, int r) {
  if (r < 0)
    return 0;
  if (n < r)
    return 0;
  if (r == 0)
    return 1;

  int a = memo[n];
  int b = memo[r];
  int c = memo[n - r];
  ll t = moddiv(a, b);
  t = moddiv(t, c);
  return t;
}

/*
int i=0;
ll route(int n,int m){
        i++;
        return c(n+m,min(n,m));
}

ll f(int n,int m,int k){
        if(k==0)return route(n,m);
        ll a=0;
        if(n>0)a += f(n-1,m,k);
        if(m>0)a += f(n,m-1,k);
        a += f(n+1,m,k-1);
        a += f(n,m+1,k-1);
        a%=mod;
        return a;
}*/

int main() {
  init();
  int n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  for (int i = 0; i <= k; i++) {
    int L = i;
    int D = k - i;
    ll x = c(n + L * 2, L) - c(n + L * 2, L - 1);
    ll y = c(m + D * 2, D) - c(m + D * 2, D - 1);
    ll comb = c(n + m + 2 * k, n + 2 * L);
    ll t = (x * y) % mod;
    t = (t * comb) % mod;
    ans = (ans + t) % mod;
  }
  while (ans < 0)
    ans += mod;
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;
typedef long long ll;

const ll mod = 1000000007;

int memo[202002] = {0};

void init() {
  memo[0] = 1;
  ll a = 1;
  for (int i = 1; i < 202002; i++) {
    a *= i;
    a %= mod;
    memo[i] = a;
  }
}

ll mypow(ll a, ll b) {
  if (b == 1)
    return a;
  if (b == 0)
    return 1;
  ll r = mypow(a, b / 2);
  r = (r * r) % mod;
  if (b % 2 == 1)
    r *= a;
  r %= mod;
  return r;
}

ll moddiv(ll a, ll b) { return (a * mypow(b, mod - 2)) % mod; }

ll c(int n, int r) {
  if (r < 0)
    return 0;
  if (n < r)
    return 0;
  if (r == 0)
    return 1;

  int a = memo[n];
  int b = memo[r];
  int c = memo[n - r];
  ll t = moddiv(a, b);
  t = moddiv(t, c);
  return t;
}

/*
int i=0;
ll route(int n,int m){
        i++;
        return c(n+m,min(n,m));
}

ll f(int n,int m,int k){
        if(k==0)return route(n,m);
        ll a=0;
        if(n>0)a += f(n-1,m,k);
        if(m>0)a += f(n,m-1,k);
        a += f(n+1,m,k-1);
        a += f(n,m+1,k-1);
        a%=mod;
        return a;
}*/

int main() {
  init();
  int n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  for (int i = 0; i <= k; i++) {
    int L = i;
    int D = k - i;
    ll x = c(n + L * 2, L) - c(n + L * 2, L - 1);
    ll y = c(m + D * 2, D) - c(m + D * 2, D - 1);
    ll comb = c(n + m + 2 * k, n + 2 * L);
    ll t = (x * y) % mod;
    t = (t * comb) % mod;
    ans = (ans + t) % mod;
  }
  while (ans < 0)
    ans += mod;
  cout << ans << endl;
}
Problem p01452 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
ll MOD = 1000000007LL;
ll mod_pow(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll x = mod_pow(a, b / 2);
    return (x * x) % MOD;
  }
  return (a * mod_pow(a, b - 1)) % MOD;
}
ll fact[300001];
ll inv_fact[300001];
ll comb(ll n, ll k) {
  if (n < k)
    return 0;
  ll ret = fact[n];
  (ret *= inv_fact[k]) %= MOD;
  (ret *= inv_fact[n - k]) %= MOD;
  return ret;
}
int main() {
  fact[0] = inv_fact[0] = 1;
  for (int i = 1; i <= 300000; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
    inv_fact[i] = (inv_fact[i - 1] * mod_pow(i, MOD - 2)) % MOD;
  }
  int N, M, K;
  cin >> N >> M >> K;
  ll ans = 0;
  for (int i = 0; i <= K; i++) {
    ll sum = comb(N + M + 2 * K, N + 2 * i);
    ll X = comb(N + 2 * i, N + i);
    if (i > 0)
      (X += MOD - comb(N + 2 * i, i - 1)) %= MOD;
    ll Y = comb(M + 2 * (K - i), M + (K - i));
    if (K - i > 0)
      (Y += MOD - comb(M + 2 * (K - i), K - i - 1)) %= MOD;
    (sum *= X) %= MOD;
    (sum *= Y) %= MOD;
    (ans += sum) %= MOD;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
ll MOD = 1000000007LL;
ll mod_pow(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll x = mod_pow(a, b / 2);
    return (x * x) % MOD;
  }
  return (a * mod_pow(a, b - 1)) % MOD;
}
ll fact[200001];
ll inv_fact[200001];
ll comb(ll n, ll k) {
  if (n < k)
    return 0;
  ll ret = fact[n];
  (ret *= inv_fact[k]) %= MOD;
  (ret *= inv_fact[n - k]) %= MOD;
  return ret;
}
int main() {
  fact[0] = inv_fact[0] = 1;
  for (int i = 1; i <= 200000; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
    inv_fact[i] = (inv_fact[i - 1] * mod_pow(i, MOD - 2)) % MOD;
  }
  int N, M, K;
  cin >> N >> M >> K;
  ll ans = 0;
  for (int i = 0; i <= K; i++) {
    ll sum = comb(N + M + 2 * K, N + 2 * i);
    ll X = comb(N + 2 * i, N + i);
    if (i > 0)
      (X += MOD - comb(N + 2 * i, i - 1)) %= MOD;
    ll Y = comb(M + 2 * (K - i), M + (K - i));
    if (K - i > 0)
      (Y += MOD - comb(M + 2 * (K - i), K - i - 1)) %= MOD;
    (sum *= X) %= MOD;
    (sum *= Y) %= MOD;
    (ans += sum) %= MOD;
  }
  cout << ans << endl;
}
Problem p01452 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#define MOD 1000000007
long long fact[500000];
long long inv[500000];
long long factinv[500000];
long long ncr(int a, int b) {
  if (b < 0 || a < b)
    return 0;
  return 1LL * fact[a] * factinv[b] % MOD * factinv[a - b] % MOD;
}
long long ktrn(int a, int b) {
  return (ncr(a + b, b) - ncr(a + b, b - 1) + MOD) % MOD;
}
int main() {
  fact[0] = 1;
  inv[1] = 1;
  factinv[0] = 1;
  for (long long i = 1; i < 500000; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= MOD;
  }
  for (long long i = 2; i < 500000; i++) {
    inv[i] = 1LL * inv[MOD % i] * (MOD - MOD / i) % MOD;
  }
  for (long long i = 1; i < 500000; i++) {
    factinv[i] = 1LL * factinv[i - 1] * inv[i] % MOD;
  }
  int n, m, k;
  cin >> n >> m >> k;
  long long sum = 0;
  for (int i = 0; i <= k; i++) {
    long long V = ncr(n + m + k * 2, n + i * 2) * ktrn(n + i, i);
    V %= MOD;
    V *= ktrn(m + k - i, k - i);
    sum += V;
    sum %= MOD;
    // cout<<p[n+m+k*2][n+i*2]<<' '<<dp[n+i][i]<<' '<<dp[m+k-i][k-i]<<endl;
  }
  cout << sum << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define MOD 1000000007
long long power[500000];
long long dp[1600][1600];
long long p[1600][1600];
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  dp[0][0] = 1;
  for (int i = 0; i <= 1500; i++) {
    p[i][0] = 1;
    p[i][i] = 1;
  }
  for (int i = 1; i <= 1500; i++) {
    for (int j = 0; j <= i; j++) {
      dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
      dp[i][j] %= MOD;
      if (j >= 1 && j < i) {
        p[i][j] = p[i - 1][j - 1] + p[i - 1][j];
        p[i][j] %= MOD;
      }
    }
  }
  long long sum = 0;
  for (int i = 0; i <= k; i++) {
    long long V = p[n + m + k * 2][n + i * 2] * dp[n + i][i];
    V %= MOD;
    V *= dp[m + k - i][k - i];
    sum += V;
    sum %= MOD;
    // cout<<p[n+m+k*2][n+i*2]<<' '<<dp[n+i][i]<<' '<<dp[m+k-i][k-i]<<endl;
  }
  cout << sum << endl;
  return 0;
}
Problem p01452 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define MOD 1000000007
#define MAX_N 100000
#define MAX_P 1000005
Int fact[MAX_P], inv[MAX_P], finv[MAX_P];

void init(Int mod) {
  fact[0] = 1;
  for (Int i = 1; i < MAX_P; i++)
    fact[i] = (fact[i - 1] * i) % mod;

  inv[1] = 1;
  for (Int i = 2; i < MAX_P; i++)
    inv[i] = inv[mod % i] * (mod - mod / i) % mod;

  finv[0] = 1;
  for (Int i = 1; i < MAX_P; i++)
    finv[i] = (finv[i - 1] * inv[i]) % mod;
}

Int mod_comb(Int x, Int y) {
  if (y < 0)
    return 0;
  return fact[x] * finv[y] % MOD * finv[x - y] % MOD;
}

// INSERT ABOVE HERE
signed main() {
  init(MOD);
  Int n, m, k;
  cin >> n >> m >> k;
  Int s = n + m + k * 2;
  vector<Int> dpx(s + 1, 0), dpy(s + 1, 0);

  for (Int i = n; i <= s; i += 2)
    dpx[i] =
        (mod_comb(i, (i + n) / 2) + MOD - mod_comb(i, (i - n) / 2 - 1)) % MOD;

  for (Int i = m; i <= s; i += 2)
    dpy[i] =
        (mod_comb(i, (i + m) / 2) + MOD - mod_comb(i, (i - m) / 2 - 1)) % MOD;

  Int ans = 0;
  for (Int i = 0; i <= s; i++)
    (ans += dpx[i] * dpy[s - i] % MOD * mod_comb(s, i) % MOD) %= MOD;

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

#define MOD 1000000007
#define MAX_N 100000
#define MAX_P 1000005
Int fact[MAX_P], inv[MAX_P], finv[MAX_P];

void init(Int mod) {
  fact[0] = 1;
  for (Int i = 1; i < MAX_P; i++)
    fact[i] = (fact[i - 1] * i) % mod;

  inv[1] = 1;
  for (Int i = 2; i < MAX_P; i++)
    inv[i] = inv[mod % i] * (mod - mod / i) % mod;

  finv[0] = 1;
  for (Int i = 1; i < MAX_P; i++)
    finv[i] = (finv[i - 1] * inv[i]) % mod;
}

// INSERT ABOVE HERE
signed main() {
  init(MOD);
  Int n, m, k;
  cin >> n >> m >> k;
  Int s = n + m + k * 2;
  vector<Int> dpx(s + 1, 0), dpy(s + 1, 0);

  {
    vector<Int> dp(s + 1, 0);
    dp[0] = 1;
    for (Int i = 0; i <= s; i++) {
      vector<Int> nx(s + 1, 0);
      // cout<<"idx:"<<i<<endl;
      for (Int j = 0; j <= s; j++) {
        // cout<<dp[j]<<" ";
        if (j - 1 >= 0)
          (nx[j - 1] += dp[j]) %= MOD;
        if (j + 1 <= s)
          (nx[j + 1] += dp[j]) %= MOD;
      }
      // cout<<endl;
      dpx[i] = dp[n];
      dpy[i] = dp[m];
      swap(dp, nx);
    }
  }

  vector<Int> nCk(s + 1, 1);
  for (Int i = 1; i <= s; i++)
    nCk[i] = nCk[i - 1] * (s - i + 1) % MOD * inv[i] % MOD;

  Int ans = 0;
  for (Int i = 0; i <= s; i++)
    (ans += dpx[i] * dpy[s - i] % MOD * nCk[i] % MOD) %= MOD;

  cout << ans << endl;
  return 0;
}
Problem p01452 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const long long int MOD = 1000000007;
const int INF = 1000000000;

ll n, m, k;
ll kai[300000];

ll mod_pow(ll x, ll y) {
  ll ret = 1;
  while (y) {
    if (y & 1)
      ret = ret * x % MOD;
    x = x * x % MOD;
    y /= 2;
  }
  return ret;
}

ll conb(ll x, ll y) {
  ll ret = kai[x + y];
  ret = ret * mod_pow(kai[x], MOD - 2) % MOD;
  ret = ret * mod_pow(kai[y], MOD - 2) % MOD;
  return ret;
}

void init() {
  kai[0] = 1;
  for (ll i = 1; i < 300000; i++) {
    kai[i] = (kai[i - 1] * i) % MOD;
  }
}

ll pre(ll x, ll y) {
  ll ret = 1;
  return ret;
}

int main() {
  cin >> n >> m >> k;
  init();
  ll ans = 0;
  for (ll i = 0; i <= k; i++) {
    ll j = k - i;
    ll add = conb(n + 2 * i, m + 2 * j);
    ll add2 = conb(n + i, i);
    if (i > 0)
      add2 = (add2 - conb(n + i + 1, i - 1) + MOD) % MOD;
    ll add3 = conb(m + j, j);
    if (j > 0)
      add3 = (add3 - conb(m + j + 1, j - 1) + MOD) % MOD;
    add = add * add2 % MOD;
    add = add * add3 % MOD;
    ans = (ans + add) % MOD;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const long long int MOD = 1000000007;
const int INF = 1000000000;

ll n, m, k;
ll kai[300000];

ll mod_pow(ll x, ll y) {
  ll ret = 1;
  while (y) {
    if (y & 1)
      ret = ret * x % MOD;
    x = x * x % MOD;
    y /= 2;
  }
  return ret;
}

ll conb(ll x, ll y) {
  ll ret = kai[x + y];
  ret = ret * mod_pow(kai[x], MOD - 2) % MOD;
  ret = ret * mod_pow(kai[y], MOD - 2) % MOD;
  return ret;
}

void init() {
  kai[0] = 1;
  for (ll i = 1; i <= 300000; i++) {
    kai[i] = (kai[i - 1] * i) % MOD;
  }
}

ll pre(ll x, ll y) {
  ll ret = 1;
  return ret;
}

int main() {
  cin >> n >> m >> k;
  init();
  ll ans = 0;
  for (ll i = 0; i <= k; i++) {
    ll j = k - i;
    ll add = conb(n + 2 * i, m + 2 * j);
    ll add2 = conb(n + i, i);
    if (i > 0)
      add2 = (add2 - conb(n + i + 1, i - 1) + MOD) % MOD;
    ll add3 = conb(m + j, j);
    if (j > 0)
      add3 = (add3 - conb(m + j + 1, j - 1) + MOD) % MOD;
    add = add * add2 % MOD;
    add = add * add3 % MOD;
    ans = (ans + add) % MOD;
  }
  cout << ans << endl;
}