Problem p02555 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

vs64 f(2010), finv(2010);

s64 pow_mod(s64 x, s64 n, s64 mod) {
  s64 ret = 1;
  while (n) {
    if (n & 1)
      ret = ret * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return ret;
}

void init(int n) {
  f[0] = 1;
  rep(i, n) { f[i + 1] = f[i] * (i + 1) % MOD; }
  finv[n] = pow_mod(f[n], MOD - 2, MOD);
  for (int i = n - 1; i >= 0; --i) {
    finv[i] = finv[i + 1] * (i + 1) % MOD;
  }
}

s64 comb(int n, int k) {
  if (n < k || n < 0 || k < 0)
    return 0;
  return f[n] * finv[n - k] % MOD * finv[k] % MOD;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int s;
  cin >> s;

  init(s);

  s64 ans = 0;
  for (int l = 1; l <= s / 3; ++l) {
    ans += comb(s - 2 * l - 1, s - 3 * l);
    ans %= MOD;
  }

  cout << ans << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

vs64 f(2010), finv(2010);

s64 pow_mod(s64 x, s64 n, s64 mod) {
  s64 ret = 1;
  while (n) {
    if (n & 1)
      ret = ret * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return ret;
}

s64 init(int n) {
  assert(n <= 2000);
  f[0] = 1;
  rep(i, n) { f[i + 1] = f[i] * (i + 1) % MOD; }
  finv[n] = pow_mod(f[n], MOD - 2, MOD);
  for (int i = n - 1; i >= 0; --i) {
    finv[i] = finv[i + 1] * (i + 1) % MOD;
  }
}

s64 comb(int n, int k) {
  if (n < k || n < 0 || k < 0)
    return 0;
  return f[n] * finv[n - k] % MOD * finv[k] % MOD;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int s;
  cin >> s;

  init(s);

  s64 ans = 0;
  for (int l = 1; l <= s / 3; ++l) {
    ans += comb(s - 2 * l - 1, s - 3 * l);
    ans %= MOD;
  }

  cout << ans << "\\n";
  return 0;
}