Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using std::pair;
using std::string;
using std::vector;

typedef long long ll;
typedef pair<int, int> pii;

#define fst first
#define snd second
#define pb(a) push_back(a)
#define mp(a, b) std::make_pair(a, b)
#define debug(...) fprintf(stderr, __VA_ARGS__)

template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }

const int oo = 0x3f3f3f3f;

string procStatus() {
  std::ifstream t("/proc/self/status");
  return string(std::istreambuf_iterator<char>(t),
                std::istreambuf_iterator<char>());
}

template <typename T> T read(T &x) {
  int f = 1;
  x = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - 48;
  return x *= f;
}

const int N = 300000;
const int mo = 998244353;

int fpm(int x, int y) {
  int res = 1;
  for (; y > 0; y >>= 1) {
    if (y & 1)
      res = (ll)res * x % mo;
    x = (ll)x * x % mo;
  }
  return res;
}

int fac[N + 5], inv[N + 5];
int binom(int n, int k) { return (ll)fac[n] * inv[k] % mo * inv[n - k] % mo; }

ll n, a, b, k;

int main() {
#ifdef Wearry
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif

  read(n), read(a), read(b), read(k);

  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = (ll)fac[i - 1] * i % mo;
  inv[n] = fpm(fac[n], mo - 2);
  for (int i = n; i >= 1; --i)
    inv[i - 1] = (ll)inv[i] * i % mo;

  ll ans = n * (a + b), res = 0;
  for (int i = 0; i <= n; ++i)
    if (ans - a * i >= k && (ans - a * i - k) % b == 0) {
      int j = (ans - a * i - k) / b;
      res = (res + (ll)binom(n, i) * binom(n, j)) % mo;
    }
  printf("%lld\n", res);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using std::pair;
using std::string;
using std::vector;

typedef long long ll;
typedef pair<int, int> pii;

#define fst first
#define snd second
#define pb(a) push_back(a)
#define mp(a, b) std::make_pair(a, b)
#define debug(...) fprintf(stderr, __VA_ARGS__)

template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }

const int oo = 0x3f3f3f3f;

string procStatus() {
  std::ifstream t("/proc/self/status");
  return string(std::istreambuf_iterator<char>(t),
                std::istreambuf_iterator<char>());
}

template <typename T> T read(T &x) {
  int f = 1;
  x = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - 48;
  return x *= f;
}

const int N = 300000;
const int mo = 998244353;

int fpm(int x, int y) {
  int res = 1;
  for (; y > 0; y >>= 1) {
    if (y & 1)
      res = (ll)res * x % mo;
    x = (ll)x * x % mo;
  }
  return res;
}

int fac[N + 5], inv[N + 5];
int binom(int n, int k) { return (ll)fac[n] * inv[k] % mo * inv[n - k] % mo; }

ll n, a, b, k;

int main() {
#ifdef Wearry
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif

  read(n), read(a), read(b), read(k);

  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = (ll)fac[i - 1] * i % mo;
  inv[n] = fpm(fac[n], mo - 2);
  for (int i = n; i >= 1; --i)
    inv[i - 1] = (ll)inv[i] * i % mo;

  ll ans = n * (a + b), res = 0;
  for (int i = 0; i <= n; ++i)
    if (ans - a * i >= k && (ans - a * i - k) % b == 0) {
      int j = (ans - a * i - k) / b;
      if (j <= n)
        res = (res + (ll)binom(n, i) * binom(n, j)) % mo;
    }
  printf("%lld\n", res);

  return 0;
}