Problem p03332 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORd(i, a, b) for (int i = (a); i >= (b); --i)
#define sz(a) int((a).size())
#define pconent(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed << setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef long double ld;
const ll MOD = 998244353;
const ll LINF = (ll)1e18;
const ld PI = acos((ld)-1);
const ld EPS = 1e-9;
inline ll gcd(ll a, ll b) {
  ll r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
inline ll fpow(ll n, ll k, int p = MOD) {
  ll r = 1;
  for (; k; k >>= 1) {
    if (k & 1)
      r = r * n % p;
    n = n * n % p;
  }
  return r;
}
inline ll isqrt(ll k) {
  ll r = sqrt(k) + 1;
  while (r * r > k)
    r--;
  return r;
}
inline ll icbrt(ll k) {
  ll r = cbrt(k) + 1;
  while (r * r * r > k)
    r--;
  return r;
}
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(ld x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(ld x, ld y) { return sign(x - y); }
#define db(v) (cerr << #v << " ** " << v << " ** " << endl)
template <class T> inline void dbv(T a) {
  for (auto x : a) {
    cout << x << " ** ";
  }
  cout << endl;
}
vector<ll> fact;
vector<ll> bin;
ll n;
void pre() {
  fact.resize(n + 5, 1);
  bin.resize(n + 5, 1);
  FOR(i, 1, n + 5) { fact[i] = (i * fact[i - 1]) % MOD; }
  FOR(i, 0, n + 1) {
    bin[i] = (fact[n] * inv((fact[n - i] * fact[i]) % MOD)) % MOD;
  }
}
void output(ll tx) {
  ll a, b, k;
  cin >> n >> a >> b >> k;
  pre();
  ll ans = 0;
  FOR(i, 0, n + 1) {
    ll ca = i;
    ll cb;
    if ((k - ca * a) >= 0 && (k - ca * a) % b == 0) {
      // db(ca);
      cb = (k - ca * a) / b;
    } else {
      continue;
    }
    if (max(ca, cb) > n) {
      continue;
    }
    ll x = ca;
    ll y = cb;
    FOR(j, max((ll)0, ca + cb - n), min(x, y) + 1) {
      // db(x-y);
      // db(x);
      // db(y);
      // db(j);
      ll fx = x - j;
      ll fy = y - j;
      ll fj = j;
      ll temp = fact[n];
      temp = (temp * inv(fact[n - fx - fy - j])) % MOD;
      temp = (temp * inv(fact[j])) % MOD;
      temp = (temp * inv(fact[fx])) % MOD;
      temp = (temp * inv(fact[fy])) % MOD;
      // db(temp);
      ans = (ans + temp) % MOD;
    }
    // db(ca);
    // db(cb);
    // db(ans);
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;
  FOR(i, 0, t) { output(i + 1); }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORd(i, a, b) for (int i = (a); i >= (b); --i)
#define sz(a) int((a).size())
#define pconent(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed << setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef long double ld;
const ll MOD = 998244353;
const ll LINF = (ll)1e18;
const ld PI = acos((ld)-1);
const ld EPS = 1e-9;
inline ll gcd(ll a, ll b) {
  ll r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
inline ll fpow(ll n, ll k, int p = MOD) {
  ll r = 1;
  for (; k; k >>= 1) {
    if (k & 1)
      r = r * n % p;
    n = n * n % p;
  }
  return r;
}
inline ll isqrt(ll k) {
  ll r = sqrt(k) + 1;
  while (r * r > k)
    r--;
  return r;
}
inline ll icbrt(ll k) {
  ll r = cbrt(k) + 1;
  while (r * r * r > k)
    r--;
  return r;
}
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(ld x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(ld x, ld y) { return sign(x - y); }
#define db(v) (cerr << #v << " ** " << v << " ** " << endl)
template <class T> inline void dbv(T a) {
  for (auto x : a) {
    cout << x << " ** ";
  }
  cout << endl;
}
vector<ll> fact;
vector<ll> bin;
ll n;
void pre() {
  fact.resize(n + 5, 1);
  bin.resize(n + 5, 1);
  FOR(i, 1, n + 5) { fact[i] = (i * fact[i - 1]) % MOD; }
  FOR(i, 0, n + 1) {
    bin[i] = (fact[n] * inv((fact[n - i] * fact[i]) % MOD)) % MOD;
  }
}
void output(ll tx) {
  ll a, b, k;
  cin >> n >> a >> b >> k;
  pre();
  ll ans = 0;
  FOR(i, 0, n + 1) {
    ll ca = i;
    ll cb;
    if ((k - ca * a) >= 0 && (k - ca * a) % b == 0) {
      // db(ca);
      cb = (k - ca * a) / b;
    } else {
      continue;
    }
    if (max(ca, cb) > n) {
      continue;
    }
    ll x = ca;
    ll y = cb;
    ll temp = (bin[ca] * bin[cb]) % MOD;
    ans = (ans + temp) % MOD;
    // FOR(j,max((ll)0,ca+cb-n),min(x,y)+1)
    // {
    //     ll fx = x-j;
    //     ll fy = y-j;
    //     ll fj = j;
    //     ll temp = fact[n];
    //     temp = (temp*inv(fact[n-fx-fy-j]))%MOD;
    //     temp = (temp*inv(fact[j]))%MOD;
    //     temp = (temp*inv(fact[fx]))%MOD;
    //     temp = (temp*inv(fact[fy]))%MOD;
    //     // db(temp);
    //     ans = (ans+temp)%MOD;
    // }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;
  FOR(i, 0, t) { output(i + 1); }
  return 0;
}