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

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
// INSERT ABOVE HERE
signed main() {
  Int n, k, l, r;
  cin >> n >> k >> l >> r;
  vector<Int> a(n);
  for (Int i = 0; i < n; i++)
    cin >> a[i];

  Int h = 20;
  if (n <= h) {
    Int ans = 0;
    for (Int b = 0; b < (1 << n); b++) {
      Int c = 0, s = 0;
      for (Int i = 0; i < n; i++)
        if ((b >> i) & 1)
          c++, s += a[i];
      if (c == k && l <= s && s <= r)
        ans++;
    }
    cout << ans << endl;
    return 0;
  }

  vector<vector<Int>> v(h + 1);
  for (Int b = 0; b < (1 << h); b++) {
    Int c = 0, s = 0;
    for (Int i = 0; i < n; i++)
      if ((b >> i) & 1)
        c++, s += a[i];
    v[c].emplace_back(s);
  }
  for (Int i = 0; i <= h; i++)
    sort(v[i].begin(), v[i].end());
  // cout<<"OK"<<endl;

  Int ans = 0;
  for (Int b = 0; b < (1 << (n - h)); b++) {
    Int c = 0, s = 0;
    for (Int i = 0; i < (n - h); i++)
      if ((b >> i) & 1)
        c++, s += a[h + i];
    if (c > k || k - c > h)
      continue;
    // cout<<c<<" "<<s<<":"<<k<<endl;
    auto latte = upper_bound(v[k - c].begin(), v[k - c].end(), r - s);
    auto malta = lower_bound(v[k - c].begin(), v[k - c].end(), l - s);
    ans += latte - malta;
  }

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
// INSERT ABOVE HERE
signed main() {
  Int n, k, l, r;
  cin >> n >> k >> l >> r;
  vector<Int> a(n);
  for (Int i = 0; i < n; i++)
    cin >> a[i];

  Int h = 20;
  if (n <= h) {
    Int ans = 0;
    for (Int b = 0; b < (1 << n); b++) {
      Int c = 0, s = 0;
      for (Int i = 0; i < n; i++)
        if ((b >> i) & 1)
          c++, s += a[i];
      if (c == k && l <= s && s <= r)
        ans++;
    }
    cout << ans << endl;
    return 0;
  }

  vector<vector<Int>> v(h + 1);
  for (Int b = 0; b < (1 << h); b++) {
    Int c = 0, s = 0;
    for (Int i = 0; i < n; i++)
      if ((b >> i) & 1)
        c++, s += a[i];
    v[c].emplace_back(s);
  }
  for (Int i = 0; i <= h; i++)
    sort(v[i].begin(), v[i].end());

  Int ans = 0;
  for (Int b = 0; b < (1 << (n - h)); b++) {
    Int c = 0, s = 0;
    for (Int i = 0; i < (n - h); i++)
      if ((b >> i) & 1)
        c++, s += a[h + i];
    if (c > k)
      continue;
    ans += upper_bound(v[k - c].begin(), v[k - c].end(), r - s) -
           lower_bound(v[k - c].begin(), v[k - c].end(), l - s);
  }

  cout << ans << endl;
  return 0;
}
Problem p02330 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
#define N 40
using namespace std;

vector<int> A[21];
int coin[N];

int calc(int bit, int arr[], int n) {
  int res = 0;
  for (int i = 0; i < n; i++)
    if (bit >> i & 1)
      res += arr[i];
  return res;
}

signed main() {

  int n, K, L, R;
  cin >> n >> K >> L >> R;
  for (int i = 0; i < n; i++)
    cin >> coin[i];

  int a = n / 2;
  int b = n - a;
  for (int i = 0; i < (1 << a); i++) {
    int cnt = __builtin_popcount(i);
    A[cnt].push_back(calc(i, coin, a));
  }

  for (int i = 0; i <= 20; i++)
    sort(A[i].begin(), A[i].end());

  int ans = 0;
  for (int i = 0; i < (1 << b); i++) {
    int cnt = __builtin_popcount(i);
    int br = calc(i, coin + a, b);
    if (cnt > K)
      continue;
    int l = lower_bound(A[K - cnt].begin(), A[K - cnt].end(), L - br) -
            A[K - cnt].begin();
    int r = upper_bound(A[K - cnt].begin(), A[K - cnt].end(), R - br) -
            A[K - cnt].begin();
    assert(l <= r);
    ans += r - l;
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define N 40
using namespace std;

vector<int> A[11];
int coin[N];

int calc(int bit, int arr[], int n) {
  int res = 0;
  for (int i = 0; i < n; i++)
    if (bit >> i & 1)
      res += arr[i];
  return res;
}

signed main() {

  int n, K, L, R;
  cin >> n >> K >> L >> R;
  for (int i = 0; i < n; i++)
    cin >> coin[i];

  int a = n / 2;
  int b = n - a;
  for (int i = 0; i < (1 << a); i++) {
    int cnt = __builtin_popcount(i);
    A[cnt].push_back(calc(i, coin, a));
  }

  int ans = 0;
  for (int i = 0; i < (1 << b); i++) {
    int cnt = __builtin_popcount(i);
    int br = calc(i, coin + a, b);
    if (cnt > K)
      continue;
    for (int ar : A[K - cnt])
      if (L <= ar + br && ar + br <= R)
        ans++;
  }
  cout << ans << endl;

  return 0;
}
Problem p02330 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

int N, K;
ll L, R;
ll a[41];

ll bitsum(int b, int offset) {
  ll s = 0;
  for (int i = 1; b > 0; b >>= 1, i += 1) {
    if (b & 1) {
      s += a[offset + i];
    }
  }
  return s;
}

ll solve() {
  int M = N / 2;
  vector<vector<ll>> h(M + 1);
  for (int i = 0; i < (1 << M); ++i) {
    int bc = __builtin_popcount(i);
    h[bc].push_back(bitsum(i, 0));
  }
  for (int i = 1; i <= M; ++i) {
    sort(h[i].begin(), h[i].end());
  }
  ll ans = 0;
  for (int i = 0; i < (1 << (N - M)); ++i) {
    ll s = bitsum(i, M);
    int bc = __builtin_popcount(i);
    if (K < bc || K - bc > M) {
      continue;
    }
    ans += upper_bound(h[K - bc].begin(), h[K - bc].end(), R - s) -
           lower_bound(h[K - bc].begin(), h[K - bc].end(), L - s);
  }
  return ans;
}

int main() {
  cin >> N >> K >> L >> R;
  for (int i = 0; i < N; ++i) {
    cin >> a[i + 1];
  }
  cout << solve() << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

int N, K;
ll L, R;
ll a[41];

ll bitsum(int b, int offset) {
  ll s = 0;
  for (int i = 1; b > 0; b >>= 1, i += 1) {
    if (b & 1) {
      s += a[offset + i];
    }
  }
  return s;
}

ll solve() {
  int M = N / 2;
  vector<vector<ll>> h(M + 1);
  for (int i = 0; i < (1 << M); ++i) {
    int bc = __builtin_popcount(i);
    h[bc].push_back(bitsum(i, 0));
  }
  for (int i = 1; i <= M; ++i) {
    sort(h[i].begin(), h[i].end());
  }
  ll ans = 0;
  for (int i = 0; i < (1 << (N - M)); ++i) {
    ll s = bitsum(i, M);
    int bc = __builtin_popcount(i);
    if (bc > K) {
      continue;
    }
    ans += upper_bound(h[K - bc].begin(), h[K - bc].end(), R - s) -
           lower_bound(h[K - bc].begin(), h[K - bc].end(), L - s);
  }
  return ans;
}

int main() {
  cin >> N >> K >> L >> R;
  for (int i = 0; i < N; ++i) {
    cin >> a[i + 1];
  }
  cout << solve() << endl;
  return 0;
}
Problem p02330 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define MAX 40

int N, K;
long long L, R;
long long a[MAX];

int main() {
  cin >> N >> K >> L >> R;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  vector<long long> v[K + 1];
  int n1 = N / 2;
  int n2 = N - n1;
  for (int i = 0; i < (1 << n1); i++) {
    long long sum = 0;
    int cnt = 0;
    for (int j = 0; j < n1; j++) {
      if (i & (1 << j)) {
        cnt++;
        sum += a[j];
      }
    }
    if (cnt > K) {
      continue;
    }
    v[cnt].push_back(sum);
  }
  for (int i = 0; i < K + 1; i++) {
    sort(v[i].begin(), v[i].end());
  }
  long long ans = 0;
  for (int i = 0; i < (1 << n2); i++) {
    long long sum = 0;
    int cnt = 0;
    for (int j = 0, k = n1; j < n2; j++, k++) {
      if (i & (1 << j)) {
        cnt++;
        sum += a[k];
      }
    }
    if (cnt > K) {
      continue;
    }
    vector<long long>::iterator r =
        upper_bound(v[K - cnt].begin(), v[K - cnt].end(), R - sum);
    vector<long long>::iterator l =
        lower_bound(v[K - cnt].begin(), v[K - cnt].end(), L - sum);
    ans += r - l;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define MAX 40

int N, K;
long long L, R;
long long a[MAX];

long long ans;
void dfs(int k, long long bit, long long sum) {
  if (k >= K) {
    if (L <= sum && sum <= R) {
      ans++;
    }
    return;
  }
  for (int j = 0; j < N; j++) {
    if (bit & (1LL << j)) {
      continue;
    }
    dfs(k + 1, bit | (1LL << j), sum + a[j]);
  }
}

int main() {
  cin >> N >> K >> L >> R;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  dfs(0, 0, 0);
  while (K > 1) {
    ans /= K;
    K--;
  }

  cout << ans << endl;

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

#include <algorithm>
#include <cstdio>
#include <set>
#include <vector>
using namespace std;

int main() {
  int N, K;
  long long L, R;
  scanf("%d%d%lld%lld", &N, &K, &L, &R);
  int n = N / 2;
  vector<long long> v(N);
  for (int i = 0; i < N; i++)
    scanf("%lld", &v[i]);
  multiset<pair<int, long long>> se1;
  vector<pair<int, long long>> se2;
  for (int i = 0; i < 1 << n; i++) {
    int m = 0;
    long long V = 0;
    for (int j = 0; j < n; j++)
      if (i & (1 << j))
        m++, V += v[j];
    se1.emplace(m, V);
  }
  for (int i = 0; i < 1 << (N - n); i++) {
    int m = 0;
    long long V = 0;
    for (int j = 0; j < (N - n); j++)
      if (i & (1 << j))
        m++, V += v[j + n];
    se2.emplace_back(m, V);
  }
  sort(se2.begin(), se2.end());
  long long r = 0;
  for (auto &e1 : se1) {
    //?????°=K-e1.first
    //????¨?=L-e1.second??\\???R-e1.second??\\???
    auto it1 = lower_bound(se2.begin(), se2.end(),
                           make_pair(K - e1.first, L - e1.second));
    auto it2 = upper_bound(se2.begin(), se2.end(),
                           make_pair(K - e1.first, R - e1.second));
    r += distance(it1, it2);
  }
  printf("%lld\\n", r);
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <set>
#include <vector>
using namespace std;

int main() {
  int N, K;
  long long L, R;
  scanf("%d%d%lld%lld", &N, &K, &L, &R);
  int n = N / 2;
  vector<long long> v(N);
  for (int i = 0; i < N; i++)
    scanf("%lld", &v[i]);
  multiset<pair<int, long long>> se1, se2;
  for (int i = 0; i < 1 << n; i++) {
    int m = 0;
    long long V = 0;
    for (int j = 0; j < n; j++)
      if (i & (1 << j))
        m++, V += v[j];
    se1.emplace(m, V);
  }
  for (int i = 0; i < 1 << (N - n); i++) {
    int m = 0;
    long long V = 0;
    for (int j = 0; j < (N - n); j++)
      if (i & (1 << j))
        m++, V += v[j + n];
    se2.emplace(m, V);
  }
  long long r = 0;
  for (auto &e1 : se1) {
    //?????°=K-e1.first
    //????¨?=L-e1.second??\\???R-e1.second??\\???
    auto it1 = se2.lower_bound({K - e1.first, L - e1.second});
    auto it2 = se2.upper_bound({K - e1.first, R - e1.second});
    r += distance(it1, it2);
  }
  printf("%lld\\n", r);
}
Problem p02333 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;
static const ll MOD = 1000000007;

ll mod_pow(ll n, ll x) {
  if (n == 0)
    return 1;
  ll ret = mod_pow(n / 2, x * x % MOD) % MOD;
  if (n % 2)
    return ret * x % MOD;
  return ret;
}

ll comb(int n, int k, vector<ll> memo) {
  if (k < 0 || k > n)
    return 0;
  return ((memo[n] * mod_pow(MOD - 2, memo[k])) % MOD) *
         mod_pow(MOD - 2, memo[n - k]) % MOD;
}

int main() {
  int n, k;
  cin >> n >> k;
  if (n < k) {
    cout << 0 << endl;
    return 0;
  }
  vector<ll> memo(k + 1, 0);
  memo[1] = 1;
  vector<ll> memo2(n + 1);
  memo2[0] = 1;
  memo2[1] = 1;
  for (int i = 2; i <= n; i++) {
    memo2[i] = memo2[i - 1] * (ll)i;
    memo2[i] %= MOD;
  }
  for (int i = 2; i <= k; i++) {
    memo[i] = mod_pow(n, i);
    for (int j = 1; j < i; j++) {
      memo[i] -= (comb(i, j, memo2) * memo[j]) % MOD;
      while (memo[i] < 0)
        memo[i] += MOD;
    }
  }
  cout << memo[k] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;
static const ll MOD = 1000000007;

ll mod_pow(ll n, ll x) {
  if (n == 0)
    return 1;
  ll ret = mod_pow(n / 2, x * x % MOD) % MOD;
  if (n % 2)
    return ret * x % MOD;
  return ret;
}

ll comb(int n, int k) {
  if (k < 0 || k > n)
    return 0;
  vector<ll> memo(n + 1);
  memo[0] = 1;
  memo[1] = 1;
  for (int i = 2; i <= n; i++) {
    memo[i] = memo[i - 1] * (ll)i;
    memo[i] %= MOD;
  }
  return ((memo[n] * mod_pow(MOD - 2, memo[k])) % MOD) *
         mod_pow(MOD - 2, memo[n - k]) % MOD;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> memo(k + 1, 0);
  memo[1] = 1;
  for (int i = 2; i <= k; i++) {
    memo[i] = mod_pow(n, i);
    for (int j = 1; j < i; j++) {
      memo[i] -= (comb(i, j) * memo[j]) % MOD;
      while (memo[i] < 0)
        memo[i] += MOD;
    }
  }
  cout << memo[k] << endl;
  return 0;
}
Problem p02334 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
ll m = 1e9 + 7;
ll dp[2002][1002];

ll dfs(ll a, ll b) {
  if (a == b)
    return 1;
  if (dp[a][b])
    return dp[a][b];
  ll res = 0;
  if (a)
    res += dfs(a - 1, b);
  if (a && b)
    res += dfs(a - 1, b - 1);
  res %= m;
  return dp[a][b] = res;
}

int main() {
  ll n, k;
  cin >> n >> k;
  dp[0][0] = 1;
  cout << dfs(n + k - 1, k - 1) << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
ll m = 1e9 + 7;
ll dp[2002][1002];

ll dfs(ll a, ll b) {
  dp[0][0] = 1;
  if (dp[a][b])
    return dp[a][b];
  ll res = 0;
  if (a)
    res += dfs(a - 1, b);
  if (a && b)
    res += dfs(a - 1, b - 1);
  res %= m;
  return dp[a][b] = res;
}

int main() {
  ll n, k;
  cin >> n >> k;
  cout << dfs(n + k - 1, k - 1) << endl;
  return 0;
}
Problem p02334 (C++) — buggy submission, verdict: Runtime Error

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

// a quote from http://www.igaris.com/math/c.pdf

#define MOD (1000000000 + 7)

long long C[1001 * 2][1001 * 2];

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

  // Pascal's triangle for combination
  C[0][0] = 1;
  for (int i = 1; i < 1001 * 2; i++) {
    for (int j = 0; j <= i; j++) {
      if (j == 0 || j == i) {
        C[i][j] = C[i - 1][0];
      } else {
        C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;
      }
    }
  }
  long long ans = C[n + k - 1][n];

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

// a quote from http://www.igaris.com/math/c.pdf

#define MOD (1000000000 + 7)

long long C[1001][1001];

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

  // Pascal's triangle for combination
  C[0][0] = 1;
  for (int i = 1; i < 1001; i++) {
    for (int j = 0; j <= i; j++) {
      if (j == 0 || j == i) {
        C[i][j] = C[i - 1][0];
      } else {
        C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;
      }
    }
  }
  long long ans = C[n + k - 1][n];

  cout << ans << endl;

  return 0;
}
Problem p02335 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stack>
using namespace std;
typedef long long ll;

ll mod = 1e9 + 7; // 1e9+9

ll add(ll x, ll y) { return (x + y) % mod; }
ll mul(ll x, ll y) { return (x * y) % mod; }

ll mpow(ll x, ll y) {
  ll v = 1;
  for (; y; x = mul(x, x), y >>= 1)
    if (y & 1)
      v = mul(v, x);
  return v;
}

ll ncr[5000][5000];

ll nCr(ll n, ll r) {
  if (n < r)
    return 0;
  if (n == r)
    return 1;
  if (r == 0)
    return 1;
  if (ncr[n][r])
    return ncr[n][r];
  ncr[n][r] = (nCr(n - 1, r - 1) + nCr(n - 1, r)) % mod;
  return ncr[n][r];
}

ll nHr(ll n, ll r) { return nCr(n + r - 1, n); }

int main() {
  ll n, k;
  cin >> n >> k;
  cout << nCr(k, n) << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
#include <stack>
using namespace std;
typedef long long ll;

ll mod = 1e9 + 7; // 1e9+9

ll add(ll x, ll y) { return (x + y) % mod; }
ll mul(ll x, ll y) { return (x * y) % mod; }

ll mpow(ll x, ll y) {
  ll v = 1;
  for (; y; x = mul(x, x), y >>= 1)
    if (y & 1)
      v = mul(v, x);
  return v;
}

ll ncr[5000][5000];

ll nCr(ll n, ll r) {
  if (n == r)
    return 1;
  if (r == 0)
    return 1;
  if (ncr[n][r])
    return ncr[n][r];
  ncr[n][r] = (nCr(n - 1, r - 1) + nCr(n - 1, r)) % mod;
  return ncr[n][r];
}

ll nHr(ll n, ll r) { return nCr(n + r - 1, n); }

int main() {
  ll n, k;
  cin >> n >> k;
  cout << nCr(k, n) << endl;
  return 0;
}
Problem p02336 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
const ll E = 1e18 + 7;
#define F first
#define S second
#define MK make_pair
const ll MOD = 1000000007;

vector<vector<ll>> c;

void mk_c() {
  c.resize(3100);
  for (int i = 1; i < 2100; i++) {
    c[i].resize(3100);
    c[i][0] = 1;
    c[i][i] = 1;
    for (int t = 1; t < i; t++) {
      c[i][t] = (c[i - 1][t - 1] + c[i - 1][t]) % MOD;
    }
  }
}

int main() {
  ll n, k;
  cin >> n >> k;
  n -= k;
  if (n < 0) {
    cout << 0 << endl;
    return 0;
  }
  if (n == 0) {
    cout << 1 << endl;
    return 0;
  }
  mk_c();
  cout << c[n + k - 1][k - 1] << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
const ll E = 1e18 + 7;
#define F first
#define S second
#define MK make_pair
const ll MOD = 1000000007;

vector<vector<ll>> c;

void mk_c() {
  c.resize(3100);
  for (int i = 1; i < 2100; i++) {
    c[i].resize(3100);
    c[i][0] = 1;
    c[i][i] = 1;
    for (int t = 1; t < i; t++) {
      c[i][t] = (c[i - 1][t - 1] + c[i - 1][t]) % MOD;
    }
  }
}

int main() {
  ll n, k;
  cin >> n >> k;
  n -= k;
  if (n < 0) {
    cout << 0 << endl;
    return 0;
  }
  mk_c();
  cout << c[n + k - 1][k - 1] << endl;

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

#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<double, ll> pdl;
#define F first
#define S second
#define MK make_pair
const ll E = 1e18 + 7;
const ll MOD = 1000000007;

vector<vector<ll>> dp(2000, vector<ll>(2000, 0));

void DP() {
  dp[0][0] = 1;
  for (int i = 0; i < 2000; i++) {
    for (int t = 1; t < 2000; t++) {
      if (i - t >= 0) {
        dp[i][t] = dp[i][t - 1] + dp[i - t][t];
        dp[i][t] %= MOD;
      } else {
        dp[i][t] = dp[i][t - 1];
      }
    }
  }
}

int main() {
  ll n, k;
  cin >> n >> k;
  DP();
  cout << dp[n][k] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
#define F first
#define S second
#define MK make_pair
const ll E = 1e17 + 7;
const ll MOD = 1000000007;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<vector<ll>> dp(n + 100, vector<ll>(k + 100, 0));
  dp[0][0] = 1;
  for (int mx = 1; mx <= n; mx++) {
    for (int i = 0; i < k; i++) {
      for (int t = 0; t + mx <= n; t++) {
        dp[t + mx][i + 1] += dp[t][i];
        dp[t + mx][i + 1] %= MOD;
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= k; i++) {
    ans += dp[n][i];
    ans %= MOD;
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll, ll>
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
#define FORR(I, A, B) for (ll I = (B - 1); I >= (A); --I)
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;

ll dp[1001][1001];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  FOR(i, 0, k + 1) {
    dp[i][0] = 1;
    FOR(j, 1, n + 1) {
      if ((j - i) >= 0)
        dp[i][j] += dp[i][j - i];
      if (i >= 1)
        dp[i][j] += dp[i - 1][j];
      dp[i][j] %= MOD;
    }
  }
  cout << dp[k][n] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll, ll>
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
#define FORR(I, A, B) for (ll I = (B - 1); I >= (A); --I)
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;

map<pair<P, ll>, ll> ma;
ll memo[1001][1001];
// p -> max in the box
ll dfs(ll a, ll b, ll p) {
  ll ans = 0;
  if (a <= p) {
    if (memo[a][b] != 0)
      return memo[a][b];
    else {
      if (a <= 1 || b <= 1)
        return 1;
      FOR(i, 0, a + 1) ans = (ans + dfs(i, b - 1, a - i)) % MOD;
      return memo[a][b] = ans;
    }
  } else {
    ll q = a - p * b;
    if (q > 0)
      return 0;
    if (ma[{{a, b}, p}] != 0)
      return ma[{{a, b}, p}];
    FOR(i, 1, p + 1) ans = (ans + dfs(a - i, b - 1, i)) % MOD;
    return ma[{{a, b}, p}] = ans;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k;
  // cout<<dfs(5,3,3)<<endl;return 0;
  cin >> n >> k;
  cout << dfs(n, k, n) << endl;
}
Problem p02342 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
ll m = 1e9 + 7;
ll dp[1002][1002];

int main() {
  ll n, k;
  cin >> n >> k;
  dp[0][0] = 1;
  n -= k;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= k; j++) {
      if (j)
        dp[i][j] += dp[i][j - 1];
      if ((i || j) && i - j >= 0)
        dp[i][j] += dp[i - j][j];
      dp[i][j] %= m;
    }
  if (n < 0)
    cout << 0 << endl;
  else
    cout << dp[n][k] << endl;
  return 0;
}


Error: 0

Fixed submission:

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

typedef long long ll;
ll m = 1e9 + 7;
ll dp[1002][1002];

int main() {
  ll n, k;
  cin >> n >> k;
  n -= k;
  dp[0][0] = 1;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= k; j++) {
      if (j)
        dp[i][j] += dp[i][j - 1];
      if ((i || j) && i - j >= 0)
        dp[i][j] += dp[i - j][j];
      dp[i][j] %= m;
    }

  cout << dp[n][k] << endl;
  return 0;
}