Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define X first
#define Y second
#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(), (x).end()
#define DEBUG(x) cerr << #x << " = " << x << "\n"
#define UCIN                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<double, double> pt;

const int N = 5e5 + 500;
const int M = 1e6 + 500;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
const int LOG = 18;
const int OFF = (1 << LOG);
const double EPS = 1e-9;
const double PI = 3.1415926535;

ll fak[N], n, a, b, k, inv[N];

ll fastpot(ll x, ll y) {
  ll cur = x, sol = 1;
  for (; y; y /= 2) {
    if (y & 1)
      sol = cur * sol % MOD;
    cur = cur * cur % MOD;
  }
  return sol;
}

void precompute() {
  fak[0] = 1;
  inv[0] = 1;
  for (int i = 1; i < N; i++) {
    fak[i] = i * fak[i - 1] % MOD;
    inv[i] = fastpot(fak[i], MOD - 2);
  }
}

ll choose(int n, int k) { return fak[n] * inv[k] % MOD * inv[n - k] % MOD; }

int main() {
  precompute();
  cin >> n >> a >> b >> k;
  ll sol = 0;
  for (int i = 0; i <= n; i++) {
    ll x = i;
    ll y = (k - a * i) / b;
    if (a * x + b * y != k)
      continue;
    if (y > n)
      continue;
    sol += choose(n, x) * choose(n, y) % MOD;
    sol %= MOD;
  }
  cout << sol << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define X first
#define Y second
#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(), (x).end()
#define DEBUG(x) cerr << #x << " = " << x << "\n"
#define UCIN                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<double, double> pt;

const int N = 5e5 + 500;
const int M = 1e6 + 500;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
const int LOG = 18;
const int OFF = (1 << LOG);
const double EPS = 1e-9;
const double PI = 3.1415926535;

ll fak[N], n, a, b, k, inv[N];

ll fastpot(ll x, ll y) {
  ll cur = x, sol = 1;
  for (; y; y /= 2) {
    if (y & 1)
      sol = cur * sol % MOD;
    cur = cur * cur % MOD;
  }
  return sol;
}

void precompute() {
  fak[0] = 1;
  inv[0] = 1;
  for (int i = 1; i < N; i++) {
    fak[i] = i * fak[i - 1] % MOD;
    inv[i] = fastpot(fak[i], MOD - 2);
  }
}

ll choose(int n, int k) { return fak[n] * inv[k] % MOD * inv[n - k] % MOD; }

int main() {
  precompute();
  cin >> n >> a >> b >> k;
  ll sol = 0;
  for (int i = 0; i <= n; i++) {
    ll x = i;
    ll y = (k - a * i) / b;
    if (a * x + b * y != k)
      continue;
    if (y > n || y < 0)
      continue;
    sol += choose(n, x) * choose(n, y) % MOD;
    sol %= MOD;
  }
  cout << sol << endl;
  return 0;
}