Problem p03332 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define FOR(x, xs) for (auto &x : xs)

using namespace std;
typedef long long ll;
typedef pair<int, int> PI;
typedef pair<ll, ll> PL;
typedef vector<int> VI;
typedef vector<ll> VL;

template <class T, template <class, class> class C, class charT = char>
void vdump(const C<T, allocator<T>> &v, const charT *delimiter = ", ",
           ostream &stream = cout) {
  copy(v.begin(), v.end(), ostream_iterator<T>(stream, delimiter));
  stream << endl;
}

ll ext_gcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll q = a / b;
  ll gcd = ext_gcd(b, a - q * b, x, y);
  ll z = x - q * y;
  x = y;
  y = z;
  return gcd;
}

constexpr ll MOD = 998244353;

int main() {
  ios::sync_with_stdio(false);
  int n;
  ll a, b, k;
  cin >> n >> a >> b >> k;
  VL fact(n + 1);
  fact[0] = 1;
  REP(i, 1, n + 1) { fact[i] = (fact[i - 1] * i) % MOD; }
  VL inv_fact(n + 1);
  ll x, y;
  ext_gcd(fact[n], MOD, x, y);
  inv_fact[n] = (x + MOD) % MOD;
  for (int i = n - 1; i >= 0; i--) {
    inv_fact[i] = (inv_fact[i + 1] * (i + 1)) % MOD;
  }

  ll ans = 0;
  for (ll i = 0; i < n + 1; i++) {
    ll x = a * i;
    if (k - x < 0 || (k - x) % b != 0) {
      continue;
    }
    ll j = (k - x) / b;
    if (j > n)
      continue;
    for (ll p = 0; p <= min(i, j); p++) {
      ll temp = fact[n];
      temp = (temp * inv_fact[i - p]) % MOD;
      temp = (temp * inv_fact[j - p]) % MOD;
      temp = (temp * inv_fact[p]) % MOD;
      ll z = n - (i - p) - (j - p) - p;
      if (z >= 0) {
        temp = (temp * inv_fact[z]) % MOD;
        ans = (ans + temp) % MOD;
      }
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define FOR(x, xs) for (auto &x : xs)

using namespace std;
typedef long long ll;
typedef pair<int, int> PI;
typedef pair<ll, ll> PL;
typedef vector<int> VI;
typedef vector<ll> VL;

template <class T, template <class, class> class C, class charT = char>
void vdump(const C<T, allocator<T>> &v, const charT *delimiter = ", ",
           ostream &stream = cout) {
  copy(v.begin(), v.end(), ostream_iterator<T>(stream, delimiter));
  stream << endl;
}

ll ext_gcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll q = a / b;
  ll gcd = ext_gcd(b, a - q * b, x, y);
  ll z = x - q * y;
  x = y;
  y = z;
  return gcd;
}

constexpr ll MOD = 998244353;

int main() {
  ios::sync_with_stdio(false);
  int n;
  ll a, b, k;
  cin >> n >> a >> b >> k;
  VL fact(n + 1);
  fact[0] = 1;
  REP(i, 1, n + 1) { fact[i] = (fact[i - 1] * i) % MOD; }
  VL inv_fact(n + 1);
  ll x, y;
  ext_gcd(fact[n], MOD, x, y);
  inv_fact[n] = (x + MOD) % MOD;
  for (int i = n - 1; i >= 0; i--) {
    inv_fact[i] = (inv_fact[i + 1] * (i + 1)) % MOD;
  }

  ll ans = 0;
  for (ll i = 0; i < n + 1; i++) {
    ll x = a * i;
    if (k - x < 0 || (k - x) % b != 0) {
      continue;
    }
    ll j = (k - x) / b;
    if (j > n)
      continue;
    ll ca = ((fact[n] * inv_fact[i]) % MOD * inv_fact[n - i]) % MOD;
    ll cb = ((fact[n] * inv_fact[j]) % MOD * inv_fact[n - j]) % MOD;
    ans = (ans + (ca * cb) % MOD) % MOD;
  }
  cout << ans << endl;

  return 0;
}