Problem p03332 (C++)

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

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define EPS (1e-8)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

typedef long long ll;
bool LTE(double a, double b) { return equals(a, b) || a < b; }
bool LT(double a, double b) { return !equals(a, b) && a < b; }

const ll mod = 998244353LL;
ll N, A, B, K;

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

long long mod_inv(long long a, long long m) {
  long long x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

#define MAX 300010
ll mfact[MAX];
ll mfactr[MAX];
ll mfactrs[MAX];

void expt() {
  rep(a, N + 1) {
    ll Av = A * a;
    if (Av > K)
      continue;
    if ((K - Av) % B)
      continue;
    ll b = (K - Av) / B;
    ll need = min((ll)a, b) + ((ll)a - min((ll)a, b)) + (b - min((ll)a, b));
    if (need > N)
      continue;
    // if( a + b > N ) continue;
    ll Bv = B * b;
    assert(Av + Bv == K);
    /*
    cout << "Av = " << Av << " ( = " << A << " * " << a << " )" << endl;
    cout << "Bv = " << Bv << " ( = " << B << " * " << b << " )" << endl;
    cout << "N<?> " << need << endl;
    */
    puts("");
  }
}

// [L,R]
ll calc(ll L, ll R) {
  ll ret = mfactrs[R];
  if (L - 1LL >= 0LL)
    ret -= mfactrs[L - 1];
  while (ret < 0LL)
    (ret += mod) %= mod;
  return ret;
}

void expt2() {
  ll sum = 0;
  rep(a, N + 1) {
    ll Av = A * a;
    if (Av > K)
      continue;
    if ((K - Av) % B)
      continue;
    ll b = (K - Av) / B;
    ll Bv = B * b;
    assert(Av + Bv == K);
    ll costa = 0, costb = 0;
    costa = (mfact[N] * mfactr[N - a]) % mod;
    costa = (costa * mfactr[a]) % mod;
    costb = (mfact[N] * mfactr[N - b]) % mod;
    costb = (costb * mfactr[b]) % mod;
    (sum += ((costa * costb) % mod)) %= mod;
  }
  cout << sum << endl;
}

void compute() {
  mfact[0] = 1 % mod;
  REP(i, 1, N + 1) mfact[i] = (mfact[i - 1] * (ll)i) % mod;
  rep(i, N + 1) mfactr[i] = mod_inv(mfact[i], mod);
  mfactrs[0] = mfactr[0];
  REP(i, 1, N + 1) mfactrs[i] = (mfactrs[i - 1] + mfactr[i]) % mod;
  expt2();
}

int main() {
  cin >> N >> A >> B >> K;
  compute();
  return 0;
}


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

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define EPS (1e-8)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

typedef long long ll;
bool LTE(double a, double b) { return equals(a, b) || a < b; }
bool LT(double a, double b) { return !equals(a, b) && a < b; }

const ll mod = 998244353LL;
ll N, A, B, K;

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

long long mod_inv(long long a, long long m) {
  long long x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

#define MAX 300010
ll mfact[MAX];
ll mfactr[MAX];
ll mfactrs[MAX];

void expt() {
  rep(a, N + 1) {
    ll Av = A * a;
    if (Av > K)
      continue;
    if ((K - Av) % B)
      continue;
    ll b = (K - Av) / B;
    ll need = min((ll)a, b) + ((ll)a - min((ll)a, b)) + (b - min((ll)a, b));
    if (need > N)
      continue;
    // if( a + b > N ) continue;
    ll Bv = B * b;
    assert(Av + Bv == K);
    /*
    cout << "Av = " << Av << " ( = " << A << " * " << a << " )" << endl;
    cout << "Bv = " << Bv << " ( = " << B << " * " << b << " )" << endl;
    cout << "N<?> " << need << endl;
    */
    puts("");
  }
}

// [L,R]
ll calc(ll L, ll R) {
  ll ret = mfactrs[R];
  if (L - 1LL >= 0LL)
    ret -= mfactrs[L - 1];
  while (ret < 0LL)
    (ret += mod) %= mod;
  return ret;
}

void expt2() {
  ll sum = 0;
  rep(a, N + 1) {
    ll Av = A * a;
    if (Av > K)
      continue;
    if ((K - Av) % B)
      continue;
    ll b = (K - Av) / B;
    ll Bv = B * b;
    assert(Av + Bv == K);
    if (b > N)
      continue;
    ll costa = 0, costb = 0;
    costa = (mfact[N] * mfactr[N - a]) % mod;
    costa = (costa * mfactr[a]) % mod;
    costb = (mfact[N] * mfactr[N - b]) % mod;
    costb = (costb * mfactr[b]) % mod;
    (sum += ((costa * costb) % mod)) %= mod;
  }
  cout << sum << endl;
}

void compute() {
  mfact[0] = 1 % mod;
  REP(i, 1, N + 1) mfact[i] = (mfact[i - 1] * (ll)i) % mod;
  rep(i, N + 1) mfactr[i] = mod_inv(mfact[i], mod);
  mfactrs[0] = mfactr[0];
  REP(i, 1, N + 1) mfactrs[i] = (mfactrs[i - 1] + mfactr[i]) % mod;
  expt2();
}

int main() {
  cin >> N >> A >> B >> K;
  compute();
  return 0;
}