prithivMLmods/Coder-Stat huggingface dataset:
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define PB push_back
#define FT first
#define SD second
#define MP make_pair
#define INF 0x7fffffff
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
typedef vector<int> VI;
typedef map<int, int> MII;
const int N = 3e5 + 10, MOD = 998244353;
LL C[N];
LL pw(LL x, int k) {
  LL res = 1LL;
  while (k) {
    if (k & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    k >>= 1;
  }
  return res;
}
void init(int n) {
  C[0] = 1;
  for (int i = 0; i < n; i++) {
    C[i + 1] = C[i] * (n - i) % MOD * pw(i + 1, MOD - 2) % MOD;
  }
}
int main() {
#ifdef YUUKILP
  freopen("in.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, a, b;
  LL k;
  cin >> n >> a >> b >> k;
  init(n);
  LL ans = 0;
  for (int i = 0; i <= n; i++) {
    if ((k - 1ll * i * a) % b == 0) {
      int j = (k - 1ll * i * a) / b;
      if (j <= n)
        ans = (ans + C[i] * C[j] % MOD) % MOD;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define PB push_back
#define FT first
#define SD second
#define MP make_pair
#define INF 0x7fffffff
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
typedef vector<int> VI;
typedef map<int, int> MII;
const int N = 3e5 + 10, MOD = 998244353;
LL C[N];
LL pw(LL x, int k) {
  LL res = 1LL;
  while (k) {
    if (k & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    k >>= 1;
  }
  return res;
}
void init(int n) {
  C[0] = 1;
  for (int i = 0; i < n; i++) {
    C[i + 1] = C[i] * (n - i) % MOD * pw(i + 1, MOD - 2) % MOD;
  }
}
int main() {
#ifdef YUUKILP
  freopen("in.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, a, b;
  LL k;
  cin >> n >> a >> b >> k;
  init(n);
  LL ans = 0;
  for (int i = 0; i <= n; i++) {
    if ((k - 1ll * i * a) % b == 0) {
      int j = (k - 1ll * i * a) / b;
      if (j >= 0 && j <= n)
        ans = (ans + C[i] * C[j] % MOD) % MOD;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
typedef long long ll;
const int mod = 998244353;
int mul(int a, int b) { return a * (ll)b % mod; }
int ad(int a, int b) { return (a + b) % mod; }
int pow(int a, int b) {
  int s = 1;
  while (b) {
    if (b & 1)
      s = mul(s, a);
    a = mul(a, a);
    b >>= 1;
  }
  return s;
}
int fac[300010], rfac[300010];
int C(int n, int k) { return mul(fac[n], mul(rfac[k], rfac[n - k])); }
int main() {
  int n, i, ans;
  ll a, b, k;
  scanf("%d%lld%lld%lld", &n, &a, &b, &k);
  fac[0] = 1;
  for (i = 1; i <= n; i++)
    fac[i] = mul(fac[i - 1], i);
  rfac[n] = pow(fac[n], mod - 2);
  for (i = n; i > 0; i--)
    rfac[i - 1] = mul(rfac[i], i);
  ans = 0;
  for (i = 0; i <= n; i++) {
    if ((k - i * a) % b == 0 && (k - i * a) / b <= n)
      ans = ad(ans, mul(C(n, i), C(n, (k - i * a) / b)));
  }
  printf("%d", ans);
}

Fixed submission:
#include <stdio.h>
typedef long long ll;
const int mod = 998244353;
int mul(int a, int b) { return a * (ll)b % mod; }
int ad(int a, int b) { return (a + b) % mod; }
int pow(int a, int b) {
  int s = 1;
  while (b) {
    if (b & 1)
      s = mul(s, a);
    a = mul(a, a);
    b >>= 1;
  }
  return s;
}
int fac[300010], rfac[300010];
int C(int n, int k) { return mul(fac[n], mul(rfac[k], rfac[n - k])); }
int main() {
  int n, i, ans;
  ll a, b, k;
  scanf("%d%lld%lld%lld", &n, &a, &b, &k);
  fac[0] = 1;
  for (i = 1; i <= n; i++)
    fac[i] = mul(fac[i - 1], i);
  rfac[n] = pow(fac[n], mod - 2);
  for (i = n; i > 0; i--)
    rfac[i - 1] = mul(rfac[i], i);
  ans = 0;
  for (i = 0; i <= n && i * a <= k; i++) {
    if ((k - i * a) % b == 0 && (k - i * a) / b <= (ll)n)
      ans = ad(ans, mul(C(n, i), C(n, (k - i * a) / b)));
  }
  printf("%d", ans);
}
Problem p03332 (C++)

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

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef double db;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pct __builtin_popcount

#define P 998244353
#define M 400000
int n, A, B;
ll m;
int F[M], IF[M];

void gcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return;
  }
  gcd(b, a % b, y, x), y -= a / b * x;
}
int inv(int n) {
  int x, y;
  gcd(n, P, x, y);
  if (x < 0)
    x += P;
  return x;
}

int C(int n, int k) { return (ll)F[n] * IF[k] % P * IF[n - k] % P; }

int main() {
  F[0] = 1;
  for (int i = 1; i < M; i++)
    F[i] = (ll)F[i - 1] * i % P;
  IF[M - 1] = inv(F[M - 1]);
  for (int i = M - 1; i; i--)
    IF[i - 1] = (ll)IF[i] * i % P;
  cin >> n >> A >> B >> m;
  int S = 0;
  for (int i = 0; i <= n; i++) {
    ll r = m - (ll)i * A;
    if (r % B != 0)
      continue;
    r /= B;
    if (r > n)
      continue;
    S = (S + (ll)C(n, i) * C(n, r) % P) % P;
  }
  cout << S << endl;
  return 0;
}


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

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef double db;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pct __builtin_popcount

#define P 998244353
#define M 400000
int n, A, B;
ll m;
int F[M], IF[M];

void gcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return;
  }
  gcd(b, a % b, y, x), y -= a / b * x;
}
int inv(int n) {
  int x, y;
  gcd(n, P, x, y);
  if (x < 0)
    x += P;
  return x;
}

int C(int n, int k) { return (ll)F[n] * IF[k] % P * IF[n - k] % P; }

int main() {
  F[0] = 1;
  for (int i = 1; i < M; i++)
    F[i] = (ll)F[i - 1] * i % P;
  IF[M - 1] = inv(F[M - 1]);
  for (int i = M - 1; i; i--)
    IF[i - 1] = (ll)IF[i] * i % P;
  cin >> n >> A >> B >> m;
  int S = 0;
  for (int i = 0; i <= n; i++) {
    ll r = m - (ll)i * A;
    if (r < 0)
      continue;
    if (r % B != 0)
      continue;
    r /= B;
    if (r > n)
      continue;
    S = (S + (ll)C(n, i) * C(n, r) % P) % P;
  }
  cout << S << endl;
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
using namespace std;
#define MOD 998244353
long long fac[300006], ifac[300006];
long long fastpow(register int a, register int n) {
  register long long t, p;
  for (t = 1, p = a; n; n >>= 1, p = p * p % MOD)
    if (n & 1)
      t = p * t % MOD;
  return t;
}
int main() {
  register int n, i, a, b;
  register long long k, ans, p;
  scanf("%d %d %d %lld", &n, &a, &b, &k);
  ifac[0] = fac[0] = fac[1] = 1;
  for (i = 2; i <= n; ++i)
    fac[i] = fac[i - 1] * i % MOD;
  ifac[n] = fastpow(fac[n], MOD - 2);
  for (i = n - 1; i; --i)
    ifac[i] = ifac[i + 1] * (i + 1) % MOD;
  ans = 0;
  for (i = 0; i <= n; ++i) {
    p = k - 1ll * i * a;
    if (p % b == 0) {
      p /= b;
      if (p <= n) {
        ans = (ans + 1ll * fac[n] * ifac[i] % MOD * ifac[n - i] % MOD * fac[n] %
                         MOD * ifac[p] % MOD * ifac[n - p]) %
              MOD;
      }
    }
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
using namespace std;
#define MOD 998244353
long long fac[300006], ifac[300006];
long long fastpow(register int a, register int n) {
  register long long t, p;
  for (t = 1, p = a; n; n >>= 1, p = p * p % MOD)
    if (n & 1)
      t = p * t % MOD;
  return t;
}
int main() {
  register int n, i, a, b;
  register long long k, ans, p;
  scanf("%d %d %d %lld", &n, &a, &b, &k);
  ifac[0] = fac[0] = fac[1] = 1;
  for (i = 2; i <= n; ++i)
    fac[i] = fac[i - 1] * i % MOD;
  ifac[n] = fastpow(fac[n], MOD - 2);
  for (i = n - 1; i; --i)
    ifac[i] = ifac[i + 1] * (i + 1) % MOD;
  ans = 0;
  for (i = 0; i <= n; ++i) {
    p = k - 1ll * i * a;
    if (p % b == 0) {
      p /= b;
      if (0 <= p && p <= n) {
        ans = (ans + 1ll * fac[n] * ifac[i] % MOD * ifac[n - i] % MOD * fac[n] %
                         MOD * ifac[p] % MOD * ifac[n - p]) %
              MOD;
      }
    }
  }
  printf("%lld", ans);
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define maxn 1000005
#define inf 1e9
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

ll fac[maxn], inv[maxn], n, k, a, b, c, ans;
const ll mod = 998244353;

inline ll pw(ll a, ll b) {
  ll ans = 1, base = a;
  while (b) {
    if (b & 1)
      ans = (ans * base) % mod;
    base = (base * base) % mod;
    b >>= 1;
  }
  return ans;
}

inline ll C(ll N, ll M) { return (fac[N] * inv[M] % mod) * inv[N - M] % mod; }

int main() {
  fac[0] = 1;
  inv[0] = 1;
  rep(i, 1, (maxn - 5)) fac[i] = (fac[i - 1] * i) % mod;
  rep(i, 1, (maxn - 5)) inv[i] = pw(fac[i], mod - 2);
  cin >> n >> a >> b >> k;
  for (ll i = 0; i <= n; i++) {
    ll res = k - i * a;
    if (res < 0)
      break;
    if (res % b != 0)
      continue;
    ll x = i, y = res / b;
    ans = (ans + C(n, x) * C(n, y)) % mod;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define maxn 1000005
#define inf 1e9
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

ll fac[maxn], inv[maxn], n, k, a, b, c, ans;
const ll mod = 998244353;

inline ll pw(ll a, ll b) {
  ll ans = 1, base = a;
  while (b) {
    if (b & 1)
      ans = (ans * base) % mod;
    base = (base * base) % mod;
    b >>= 1;
  }
  return ans;
}

inline ll C(ll N, ll M) { return (fac[N] * inv[M] % mod) * inv[N - M] % mod; }

int main() {
  fac[0] = 1;
  inv[0] = 1;
  rep(i, 1, (maxn - 5)) fac[i] = (fac[i - 1] * i) % mod;
  rep(i, 1, (maxn - 5)) inv[i] = pw(fac[i], mod - 2);
  cin >> n >> a >> b >> k;
  for (ll i = 0; i <= n; i++) {
    ll res = k - i * a;
    if (res < 0)
      break;
    if (res % b != 0 || res / b > n)
      continue;
    ll x = i, y = res / b;
    ans = (ans + C(n, x) * C(n, y)) % mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

typedef long long ll;
const ll p = 998244353;
ll n, a, b, k, ans;
const ll maxn = 3e5 + 10;
ll fac[maxn], inv[maxn];

ll qpow(ll a, ll b) {
  ll sum = 1;
  while (b) {
    if (b & 1)
      sum = sum * a % p;
    a = a * a % p, b >>= 1;
  }
  return sum;
}

ll c(ll a, ll b) {
  if (b > a)
    return 0;
  return fac[a] * inv[b] % p * inv[a - b] % p;
}

int main() {
  scanf("%lld%lld%lld%lld", &n, &a, &b, &k);

  if (!k) {
    puts("1");
    return 0;
  }

  if (ceil((double)k / (a + b)) > n) {
    puts("0");
    return 0;
  }
  fac[0] = 1;
  for (ll i = 1; i <= n; i++)
    fac[i] = i * fac[i - 1] % p;
  inv[n] = qpow(fac[n], p - 2);
  for (ll i = n; i; i--)
    inv[i - 1] = inv[i] * i % p;
  for (ll i = 1; i < n; i++) {
    ll last = k - a * i;
    if (last % b)
      continue;
    ll nb = last / b;
    ans = (ans + c(n, i) * c(n, nb) % p) % p;
  }
  printf("%lld", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

typedef long long ll;
const ll p = 998244353;
ll n, a, b, k, ans;
const ll maxn = 3e5 + 10;
ll fac[maxn], inv[maxn];

ll qpow(ll a, ll b) {
  ll sum = 1;
  while (b) {
    if (b & 1)
      sum = sum * a % p;
    a = a * a % p, b >>= 1;
  }
  return sum;
}

ll c(ll a, ll b) {
  if (b > a)
    return 0;
  return fac[a] * inv[b] % p * inv[a - b] % p;
}

int main() {
  scanf("%lld%lld%lld%lld", &n, &a, &b, &k);

  if (!k) {
    puts("1");
    return 0;
  }

  if (ceil((double)k / (a + b)) > n) {
    puts("0");
    return 0;
  }
  fac[0] = 1;
  for (ll i = 1; i <= n; i++)
    fac[i] = i * fac[i - 1] % p;
  inv[n] = qpow(fac[n], p - 2);
  for (ll i = n; i; i--)
    inv[i - 1] = inv[i] * i % p;
  for (ll i = 1; i <= n && a * i <= k; i++) {
    ll last = k - a * i;
    if (last % b)
      continue;
    ll nb = last / b;
    ans = (ans + c(n, i) * c(n, nb) % p) % p;
  }
  printf("%lld", ans);
  return 0;
}
Problem p03332 (C++)

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 998244353;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <typename T> void vprint(T &v) {
  REP(i, v.size()) { cout << v[i] << " "; }
  cout << endl;
}

const ll N_MAX = 400005;

ll inv[N_MAX], fac[N_MAX], finv[N_MAX];
void make() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < N_MAX; i++) {
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    fac[i] = fac[i - 1] * (ll)i % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

ll Combination(ll C, ll D) {
  if (C < D || C < 1)
    return 0;
  return fac[C] * (finv[D] * finv[C - D] % mod) % mod;
}

int main() {
  ll N, A, B, K;
  cin >> N >> A >> B >> K;
  make();
  ll res = 0;
  REP(i, N + 1) {
    if ((K - A * i) % B == 0) {
      ll j = (K - A * i) / B;
      res += (Combination(N, i) * Combination(N, j) % mod);
    }
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 998244353;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <typename T> void vprint(T &v) {
  REP(i, v.size()) { cout << v[i] << " "; }
  cout << endl;
}

const ll N_MAX = 400005;

ll inv[N_MAX], fac[N_MAX], finv[N_MAX];
void make() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < N_MAX; i++) {
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    fac[i] = fac[i - 1] * (ll)i % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

ll Combination(ll C, ll D) {
  if (C < D || C < 1)
    return 0;
  return fac[C] * (finv[D] * finv[C - D] % mod) % mod;
}

int main() {
  ll N, A, B, K;
  cin >> N >> A >> B >> K;
  make();
  ll res = 0;
  REP(i, N + 1) {
    if ((K - A * i) % B == 0) {
      ll j = (K - A * i) / B;
      if (j >= 0 && j <= N)
        res += (Combination(N, i) * Combination(N, j) % mod);
      res %= mod;
    }
  }
  cout << res << endl;
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100100
const ll mod = 998244353;

int n, flag, step = 1;
ll a, b, k, p, q, ans;
ll fac[N], inv[N];

ll power(ll x, ll y) {
  ll ret = 1;
  for (; y; y >>= 1, x = x * x % mod)
    if (y & 1)
      ret = ret * x % mod;
  return ret;
}

ll C(int x, int y) { return fac[x] * inv[y] % mod * inv[x - y] % mod; }

int main() {
  scanf("%d%lld%lld%lld", &n, &a, &b, &k);
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % mod;
  inv[n] = power(fac[n], mod - 2);
  for (int i = n - 1; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % mod;

  for (int i = 0; i <= n; i += step)
    if ((k - a * i) % b == 0) {
      if (!flag)
        step = b / __gcd(a, b), flag = 1;
      p = i;
      q = (k - a * i) / b;
      if (q > n || q < 0)
        continue;
      ans = (ans + C(n, p) * C(n, q) % mod) % mod;
    }
  printf("%lld\n", ans % mod);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 10010000
const ll mod = 998244353;

int n, flag, step = 1;
ll a, b, k, p, q, ans;
ll fac[N], inv[N];

ll power(ll x, ll y) {
  ll ret = 1;
  for (; y; y >>= 1, x = x * x % mod)
    if (y & 1)
      ret = ret * x % mod;
  return ret;
}

ll C(int x, int y) { return fac[x] * inv[y] % mod * inv[x - y] % mod; }

int main() {
  scanf("%d%lld%lld%lld", &n, &a, &b, &k);
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % mod;
  inv[n] = power(fac[n], mod - 2);
  for (int i = n - 1; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % mod;

  for (int i = 0; i <= n; i += step)
    if ((k - a * i) % b == 0) {
      if (!flag)
        step = b / __gcd(a, b), flag = 1;
      p = i;
      q = (k - a * i) / b;
      if (q > n || q < 0)
        continue;
      ans = (ans + C(n, p) * C(n, q) % mod) % mod;
    }
  printf("%lld\n", ans % mod);
  return 0;
}
Problem p03332 (C++)

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

#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define REP1(i, j, n) for (int i = j; i < (int)n; i++)
#define pb(x) push_back(x)
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define double long double
#define mp(x, y) make_pair(x, y)

typedef long long ll;

using namespace std;

const int MAX = 5 * 100000 + 1;
int fact[MAX];
int MOD = 998244353;
ll inv(ll x, ll mod) {
  ll r, y;
  for (r = 1, y = mod - 2; y != 0; x = x * x % mod, y >>= 1)
    if ((y & 1) == 1)
      r = r * x % mod;
  return r;
}
int ncr(int n, int r) {
  // cerr<<n<<" "<<r<<"\n";
  if (r == 0)
    return 1;
  return (int)((ll)fact[n] *
               ((inv(fact[r], MOD) * inv(fact[n - r], MOD)) % MOD) % MOD) %
         MOD;
}

void factorial() {
  fact[0] = fact[1] = 1;
  for (int i = 2; i < MAX; i++)
    fact[i] = (int)((ll)i * fact[i - 1] % MOD);
}

ll sum[MAX];
ll mem[MAX];

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);

  ll n, A, B, k;
  cin >> n >> A >> B >> k;
  factorial();
  int MOD = 998244353;
  ll res = 0;
  REP(a, n + 1) {
    ll r = k - a * A;
    if (r % B == 0) {
      ll b = r / B;
      if (max(1ll * a, b) <= n)
        res += 1ll * ncr(n, a) * ncr(n, b) % MOD;
      res %= MOD;
    }
  }
  cout << res << "\n";

  return 0;
}

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

#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define REP1(i, j, n) for (int i = j; i < (int)n; i++)
#define pb(x) push_back(x)
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define double long double
#define mp(x, y) make_pair(x, y)

typedef long long ll;

using namespace std;

const int MAX = 5 * 100000 + 1;
int fact[MAX];
int MOD = 998244353;
ll inv(ll x, ll mod) {
  ll r, y;
  for (r = 1, y = mod - 2; y != 0; x = x * x % mod, y >>= 1)
    if ((y & 1) == 1)
      r = r * x % mod;
  return r;
}
int ncr(int n, int r) {
  // cerr<<n<<" "<<r<<"\n";
  if (r == 0)
    return 1;
  return (int)((ll)fact[n] *
               ((inv(fact[r], MOD) * inv(fact[n - r], MOD)) % MOD) % MOD) %
         MOD;
}

void factorial() {
  fact[0] = fact[1] = 1;
  for (int i = 2; i < MAX; i++)
    fact[i] = (int)((ll)i * fact[i - 1] % MOD);
}

ll sum[MAX];
ll mem[MAX];

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);

  ll n, A, B, k;
  cin >> n >> A >> B >> k;
  factorial();
  int MOD = 998244353;
  ll res = 0;
  REP(a, n + 1) {
    if (1ll * a * A > k)
      continue;
    ll r = k - 1ll * a * A;
    if (r % B == 0) {
      ll b = r / B;
      if (max(1ll * a, b) <= n)
        res += 1ll * ncr(n, a) * ncr(n, b) % MOD;
      res %= MOD;
    }
  }
  cout << res << "\n";

  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
using namespace std;
typedef long long ll;

const ll MOD = 998244353LL;

class ModuloCombination {
private:
  vector<ll> fact, inv_fact;

  static ll modulo_power(ll a, ll n) {
    if (n == 0)
      return 1;
    if (n % 2 == 0)
      return modulo_power((a * a) % MOD, n / 2);
    return (a * modulo_power(a, n - 1)) % MOD;
  }

public:
  ModuloCombination(ll N) : fact(N + 1), inv_fact(N + 1) {
    fact[0] = 1;
    REP(i, 1, N + 1) fact[i] = (i * fact[i - 1]) % MOD;
    REP(i, 0, N + 1) inv_fact[i] = modulo_power(fact[i], MOD - 2);
  }

  ll operator()(ll n, ll r) {
    return fact[n] * inv_fact[r] % MOD * inv_fact[n - r] % MOD;
  }
};

int main(void) {
  ll N, A, B, K;
  cin >> N >> A >> B >> K;

  ModuloCombination C(500000);
  ll ans = 0;

  REP(x, 0, N + 1) {
    if (K - x * A >= 0 && (K - x * A) % B == 0) {
      ll y = (K - x * A) / B;

      // cerr << "x: " << x << ", y: " << y << endl;
      // ll w1 = max(x, y);
      // ll w2 = min(x + y, N) + 1;
      // REP(w, w1, w2) {
      //   ll t = N - w;
      //   ll g = x + y - w;
      //   ll r = x - g;
      //   ll b = y - g;
      //   cerr << "(r, g, b, t) = " << "(" << r << ", " << b << ", " << g << ",
      //   " << t << ")" << endl;
      // }

      ans = (ans + C(N, x) * C(N, y) % MOD) % MOD;
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
using namespace std;
typedef long long ll;

const ll MOD = 998244353LL;

class ModuloCombination {
private:
  vector<ll> fact, inv_fact;

  static ll modulo_power(ll a, ll n) {
    if (n == 0)
      return 1;
    if (n % 2 == 0)
      return modulo_power((a * a) % MOD, n / 2);
    return (a * modulo_power(a, n - 1)) % MOD;
  }

public:
  ModuloCombination(ll N) : fact(N + 1), inv_fact(N + 1) {
    fact[0] = 1;
    REP(i, 1, N + 1) fact[i] = (i * fact[i - 1]) % MOD;
    REP(i, 0, N + 1) inv_fact[i] = modulo_power(fact[i], MOD - 2);
  }

  ll operator()(ll n, ll r) {
    return fact[n] * inv_fact[r] % MOD * inv_fact[n - r] % MOD;
  }
};

int main(void) {
  ll N, A, B, K;
  cin >> N >> A >> B >> K;

  ModuloCombination C(500000);
  ll ans = 0;

  REP(x, 0, N + 1) {
    if (K - x * A >= 0 && (K - x * A) % B == 0) {
      ll y = (K - x * A) / B;
      if (!(0 <= y && y <= N))
        continue;

      // cerr << "x: " << x << ", y: " << y << endl;
      // ll w1 = max(x, y);
      // ll w2 = min(x + y, N) + 1;
      // REP(w, w1, w2) {
      //   ll t = N - w;
      //   ll g = x + y - w;
      //   ll r = x - g;
      //   ll b = y - g;
      //   cerr << "(r, g, b, t) = " << "(" << r << ", " << b << ", " << g << ",
      //   " << t << ")" << endl;
      // }

      ans = (ans + C(N, x) * C(N, y) % MOD) % MOD;
    }
  }

  cout << ans << endl;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 998244353;

const int Nmax = 200010;

ll mpow(ll a, ll p) {
  ll x = a;
  ll y = 1;
  while (p > 0) {
    if ((p & 1) == 1) {
      y = x * y % MOD;
    }
    x = x * x % MOD;
    p >>= 1;
  }
  return y;
}

ll minv(ll a) { return mpow(a, MOD - 2); }

ll fact[333333];
ll ncr[333333];
void init_ncr(ll n) {
  ll t = 1;
  fact[0] = 1;
  for (ll i = 1; i <= n; i++) {
    t = (t * i) % MOD;
    fact[i] = t;
  }
  for (ll r = 0; r <= n / 2; r++) {
    ncr[r] = fact[n] * minv(fact[n - r] * fact[r] % MOD) % MOD;
  }
  for (ll r = 0; r <= n / 2; r++) {
    ncr[n - r] = ncr[r];
  }
}

int main() {
  ll N, R, B, K;
  cin >> N >> R >> B >> K;
  // NブロックR点B点K目標
  init_ncr(N);
  ll cnt = 0, aa, b;
  for (ll a = 0; a <= N; a++) { // a:Rの個数,b:Bの個数
    aa = K - a * R;
    if (aa < 0)
      continue;
    if (aa % B == 0) {
      b = aa / B;
      cnt = (cnt + ncr[a] * ncr[b]) % MOD;
    }
  }

  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 998244353;

const int Nmax = 200010;

ll mpow(ll a, ll p) {
  ll x = a;
  ll y = 1;
  while (p > 0) {
    if ((p & 1) == 1) {
      y = x * y % MOD;
    }
    x = x * x % MOD;
    p >>= 1;
  }
  return y;
}

ll minv(ll a) { return mpow(a, MOD - 2); }

ll fact[333333];
ll ncr[333333];
void init_ncr(ll n) {
  ll t = 1;
  fact[0] = 1;
  for (ll i = 1; i <= n; i++) {
    t = (t * i) % MOD;
    fact[i] = t;
  }
  for (ll r = 0; r <= n / 2; r++) {
    ncr[r] = fact[n] * minv(fact[n - r] * fact[r] % MOD) % MOD;
  }
  for (ll r = 0; r <= n / 2; r++) {
    ncr[n - r] = ncr[r];
  }
}

int main() {
  ll N, R, B, K;
  cin >> N >> R >> B >> K;
  // NブロックR点B点K目標
  init_ncr(N);
  ll cnt = 0, aa, b;
  for (ll a = 0; a <= N; a++) { // a:Rの個数,b:Bの個数
    aa = K - a * R;
    if (aa < 0)
      continue;
    if (aa % B == 0) {
      b = aa / B;
      if (b >= 0 && b <= N)
        cnt = (cnt + (ncr[a] * ncr[b]) % MOD) % MOD;
    }
  }

  cout << cnt << endl;
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define M 300010
#define LL long long
#define MOD 998244353
int fac[M], inv[M];
inline int C(int n, int m) {
  return 1ll * fac[n] * inv[m] % MOD * inv[n - m] % MOD;
}
int main() {
  int n, A, B;
  LL K;
  scanf("%d%d%d%lld", &n, &A, &B, &K);
  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = 1ll * fac[i - 1] * i % MOD;
  inv[0] = inv[1] = 1;
  for (int i = 2; i <= n; ++i)
    inv[i] = 1ll * inv[MOD % i] * (MOD - MOD / i) % MOD;
  for (int i = 2; i <= n; ++i)
    inv[i] = 1ll * inv[i - 1] * inv[i] % MOD;
  int Ans = 0;
  for (int i = 0; i <= n; ++i) {
    if (K - 1ll * A * i >= 0 && (K - 1ll * A * i) % B == 0) {
      Ans += 1ll * C(n, i) * C(n, (K - 1ll * A * i) / B) % MOD;
      if (Ans >= MOD)
        Ans -= MOD;
    }
  }
  printf("%d\n", Ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define M 300010
#define LL long long
#define MOD 998244353
int fac[M], inv[M];
inline int C(int n, int m) {
  return 1ll * fac[n] * inv[m] % MOD * inv[n - m] % MOD;
}
int main() {
  int n, A, B;
  LL K;
  scanf("%d%d%d%lld", &n, &A, &B, &K);
  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = 1ll * fac[i - 1] * i % MOD;
  inv[0] = inv[1] = 1;
  for (int i = 2; i <= n; ++i)
    inv[i] = 1ll * inv[MOD % i] * (MOD - MOD / i) % MOD;
  for (int i = 2; i <= n; ++i)
    inv[i] = 1ll * inv[i - 1] * inv[i] % MOD;
  int Ans = 0;
  for (int i = 0; i <= n; ++i) {
    if (K - 1ll * A * i >= 0 && (K - 1ll * A * i) % B == 0 &&
        (K - 1ll * A * i) / B <= n) {
      Ans += 1ll * C(n, i) * C(n, (K - 1ll * A * i) / B) % MOD;
      if (Ans >= MOD)
        Ans -= MOD;
    }
  }
  printf("%d\n", Ans);
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
const int N = 4e5 + 10, M = 998244353;
long long c[N], l[N];
int main() {
  int n;
  long long a, b, k, an = 0;
  scanf("%d%lld%lld%lld", &n, &a, &b, &k);
  l[1] = 1;
  c[0] = 1;
  c[1] = n;
  for (int i = 2; i <= n; i++) {
    l[i] = (M - M / i) * l[M % i] % M;
    c[i] = c[i - 1] * (n - i + 1) % M * l[i] % M;
  }
  for (int i = 0; i <= n; i++)
    if ((k - a * i) % b == 0 && (k - a * i) / b <= n)
      an = (an + c[i] * c[(k - a * i) / b] % M) % M;
  printf("%lld", an);
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
const int N = 4e5 + 10, M = 998244353;
long long c[N], l[N];
int main() {
  int n;
  long long a, b, k, an = 0;
  scanf("%d%lld%lld%lld", &n, &a, &b, &k);
  l[1] = 1;
  c[0] = 1;
  c[1] = n;
  for (int i = 2; i <= n; i++) {
    l[i] = (M - M / i) * l[M % i] % M;
    c[i] = c[i - 1] * (n - i + 1) % M * l[i] % M;
  }
  for (int i = 0; i <= n; i++)
    if ((k - a * i) % b == 0 && (k - a * i) / b <= n && (k - a * i) / b >= 0)
      an = (an + c[i] * c[(k - a * i) / b] % M) % M;
  printf("%lld", an);
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 400010
#define int long long
#define mod 998244353
using namespace std;

inline int qpow(int bas, int pw) {
  int ret = 1;
  for (; pw; pw >>= 1, bas = 1ll * bas * bas % mod)
    if (pw & 1)
      ret = 1ll * ret * bas % mod;
  return ret;
}
int n, a, b, k, fac[N + 10], inv[N + 10], sum;
inline int C(int n, int m) { return fac[n] * inv[n - m] % mod * inv[m] % mod; }
inline void Add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  if (x < 0)
    x += mod;
}
signed main() {
  fac[0] = 1;
  for (int i = 1; i <= N; i++)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  inv[N] = qpow(fac[N], mod - 2);
  for (int i = N - 1; i >= 0; i--)
    inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
  cin >> n >> a >> b >> k;
  for (int i = 0; i <= n; i++) {
    int t = k - a * i;
    if (t % b == 0 && t / b <= n)
      Add(sum, 1ll * C(n, i) * C(n, t / b) % mod);
  }
  cout << sum << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 400010
#define int long long
#define mod 998244353
using namespace std;

inline int qpow(int bas, int pw) {
  int ret = 1;
  for (; pw; pw >>= 1, bas = 1ll * bas * bas % mod)
    if (pw & 1)
      ret = 1ll * ret * bas % mod;
  return ret;
}
int n, a, b, k, fac[N + 10], inv[N + 10], sum;
inline int C(int n, int m) { return fac[n] * inv[n - m] % mod * inv[m] % mod; }
inline void Add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  if (x < 0)
    x += mod;
}
signed main() {
  fac[0] = 1;
  for (int i = 1; i <= N; i++)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  inv[N] = qpow(fac[N], mod - 2);
  for (int i = N - 1; i >= 0; i--)
    inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
  cin >> n >> a >> b >> k;
  for (int i = 0; i <= n; i++) {
    int t = k - a * i;
    if (t < 0)
      continue;
    if (t % b == 0 && t / b <= n)
      Add(sum, 1ll * C(n, i) * C(n, t / b) % mod);
  }
  cout << sum << endl;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
using namespace std;

const int mod = 998244353;
const int N = 3e5 + 5;
ll fac[N], ifac[N], inv[N];

int main() {
  // ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
  ll n, a, b, k;
  cin >> n >> a >> b >> k;

  fac[0] = 1;
  ifac[0] = 1;
  fac[1] = 1;
  ifac[1] = 1;
  for (ll i = 2; i <= n; i++) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= mod;
  }

  inv[1] = 1;
  for (ll i = 2; i <= n; i++) {
    inv[i] = mod - (mod / i) * inv[mod % i] % mod;
    ifac[i] = ifac[i - 1] * inv[i];
    ifac[i] %= mod;
    // cout << ifac[i] << endl;
  }

  ll answer = 0;
  for (ll x = 0; x <= n; x++) {
    if ((k - a * x) % b)
      continue;
    ll y = (k - a * x) / b;
    if (y > n)
      continue;
    ll t = ((fac[n] * ifac[x]) % mod * ifac[n - x]) % mod;
    ll p = ((fac[n] * ifac[y]) % mod * ifac[n - y]) % mod;
    answer += t * p % mod;
    answer %= mod;
  }

  cout << answer << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
using namespace std;

const int mod = 998244353;
const int N = 3e5 + 5;
ll fac[N], ifac[N], inv[N];

int main() {
  // ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
  ll n, a, b, k;
  cin >> n >> a >> b >> k;

  fac[0] = 1;
  ifac[0] = 1;
  fac[1] = 1;
  ifac[1] = 1;
  for (ll i = 2; i <= n; i++) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= mod;
  }

  inv[1] = 1;
  for (ll i = 2; i <= n; i++) {
    inv[i] = mod - (mod / i) * inv[mod % i] % mod;
    ifac[i] = ifac[i - 1] * inv[i];
    ifac[i] %= mod;
    // cout << ifac[i] << endl;
  }

  ll answer = 0;
  for (ll x = 0; x <= n; x++) {
    if (k - a * x < 0)
      break;
    if ((k - a * x) % b)
      continue;
    ll y = (k - a * x) / b;
    if (y > n)
      continue;
    ll t = ((fac[n] * ifac[x]) % mod * ifac[n - x]) % mod;
    ll p = ((fac[n] * ifac[y]) % mod * ifac[n - y]) % mod;
    answer += t * p % mod;
    answer %= mod;
  }

  cout << answer << endl;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
/*
ID: jerrywcy
LINK: https://atcoder.jp/contests/agc025/tasks/agc025_b
LANG: C++
STATUS:
*/
#include <bits/stdc++.h>

#define init(array, x) memset(array, x, sizeof(array))

using namespace std;

typedef long long ll;

const ll inf = 0x3f3f3f3f;
const ll mod = 998244353;

ll n, a, b, k;
ll f[3000010], g[3000010];
ll ans;

ll read() {
  ll f = 1, x = 0;
  char ss = getchar();
  while (ss < '0' || ss > '9') {
    if (ss == '-')
      f = -1;
    ss = getchar();
  }
  while (ss >= '0' && ss <= '9') {
    x = x * 10 + ss - '0';
    ss = getchar();
  }
  return f * x;
}

ll pow(ll x, ll y) {
  ll ret = 1;
  while (y) {
    if (y & 1)
      ret = (ret * x) % mod;
    x = (x * x) % mod;
    y >>= 1;
  }
  return ret;
}

void getf() {
  f[1] = 1;
  for (int i = 2; i <= n; i++)
    f[i] = (f[i - 1] * i) % mod;
  g[n] = pow(f[n], mod - 2);
  for (int i = n - 1; i >= 1; i--)
    g[i] = (g[i + 1] * (i + 1)) % mod;
  return;
}

ll C(int x, int y) {
  if (y == 0 || y == x)
    return 1;
  return ((((f[x] * g[y]) % mod) * g[x - y]) % mod);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  n = read();
  a = read();
  b = read();
  k = read();
  getf();
  for (int i = 0; i * a <= k; i++) {
    if ((k - i * a) % b == 0) {
      int j = (k - i * a) / b;
      ans = (ans + (C(n, i) * C(n, j)) % mod) % mod;
    }
  }
  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
/*
ID: jerrywcy
LINK: https://atcoder.jp/contests/agc025/tasks/agc025_b
LANG: C++
STATUS:
*/
#include <bits/stdc++.h>

#define init(array, x) memset(array, x, sizeof(array))

using namespace std;

typedef long long ll;

const ll inf = 0x3f3f3f3f;
const ll mod = 998244353;

ll n, a, b, k;
ll f[3000010], g[3000010];
ll ans;

ll read() {
  ll f = 1, x = 0;
  char ss = getchar();
  while (ss < '0' || ss > '9') {
    if (ss == '-')
      f = -1;
    ss = getchar();
  }
  while (ss >= '0' && ss <= '9') {
    x = x * 10 + ss - '0';
    ss = getchar();
  }
  return f * x;
}

ll pow(ll x, ll y) {
  ll ret = 1;
  while (y) {
    if (y & 1)
      ret = (ret * x) % mod;
    x = (x * x) % mod;
    y >>= 1;
  }
  return ret;
}

void getf() {
  f[1] = 1;
  for (int i = 2; i <= n; i++)
    f[i] = (f[i - 1] * i) % mod;
  g[n] = pow(f[n], mod - 2);
  for (int i = n - 1; i >= 1; i--)
    g[i] = (g[i + 1] * (i + 1)) % mod;
  return;
}

ll C(int x, int y) {
  if (x < y)
    return 0;
  if (y == 0 || y == x)
    return 1;
  return ((((f[x] * g[y]) % mod) * g[x - y]) % mod);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  n = read();
  a = read();
  b = read();
  k = read();
  getf();
  for (int i = 0; i * a <= k; i++) {
    if ((k - i * a) % b == 0) {
      int j = (k - i * a) / b;
      ans = (ans + (C(n, i) * C(n, j)) % mod) % mod;
    }
  }
  printf("%lld\n", ans);

  return 0;
}
Problem p03332 (C++)

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

using namespace std;

constexpr int N = 3e5 + 10;
constexpr int MOD = 998244353;

long long n, A, B, K, fact[N], ans;

long long mypow(long long a, long long b) {
  return b ? (mypow(a * a % MOD, b >> 1) * (b & 1 ? a : 1)) % MOD : 1;
}

long long ch(long long a, long long b) {
  if (b > a)
    return 0;
  return (fact[a] * mypow(fact[b] * fact[a - b] % MOD, MOD - 2)) % MOD;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> A >> B >> K;
  fact[0] = 1;
  for (int i = 1; i < N; i++)
    fact[i] = (fact[i - 1] * i) % MOD;
  for (int i = 0; i <= n; i++) {
    // i ta A biad
    long long remain = K - i * A;
    if (remain % B)
      continue;
    int z = remain / B;
    ans += ch(n, i) * ch(n, z) % MOD;
    ans %= MOD;
  }
  cout << ans << '\n';
  return 0;
}


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

using namespace std;

constexpr int N = 3e5 + 10;
constexpr int MOD = 998244353;

long long n, A, B, K, fact[N], ans;

long long mypow(long long a, long long b) {
  return b ? (mypow(a * a % MOD, b >> 1) * (b & 1 ? a : 1)) % MOD : 1;
}

long long ch(long long a, long long b) {
  if (b > a)
    return 0;
  return (fact[a] * mypow(fact[b] * fact[a - b] % MOD, MOD - 2)) % MOD;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> A >> B >> K;
  fact[0] = 1;
  for (int i = 1; i < N; i++)
    fact[i] = (fact[i - 1] * i) % MOD;
  for (int i = 0; i <= n && i * A <= K; i++) {
    // i ta A biad
    long long remain = K - i * A;
    if (remain % B)
      continue;
    int z = remain / B;
    ans += ch(n, i) * ch(n, z) % MOD;
    ans %= MOD;
  }
  cout << ans << '\n';
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll MOD = 998244353;
ll fact[300001];
ll inv[300001];
ll pow1(ll n, ll p) {
  ll res, cur;
  res = 1;
  cur = n;
  while (p) {
    if (p & 1) {
      res *= cur;
      res %= MOD;
    }
    p /= 2;
    cur *= cur;
    cur %= MOD;
  }
  return res;
}

void init() {
  inv[0] = 1;
  fact[0] = 1;
  for (int i = 1; i < 300001; i++) {
    fact[i] = fact[i - 1] * i % MOD;
    inv[i] = pow1(fact[i], MOD - 2);
  }
}

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

int main() {
  //  freopen("in.txt", "r", stdin);
  //  freopen("out.txt", "w", stdout);
  init();
  ll res = 0;
  long long n, a, b, k;
  cin >> n >> a >> b >> k;
  for (int i = 0; i <= n; i++) {
    if (a * i > k)
      break;
    if ((k - a * i) % b != 0)
      continue;
    res = (res + cnk(n, i) * cnk(n, (k - a * i) / b) % MOD) % MOD;
  }
  cout << res;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll MOD = 998244353;
ll fact[300001];
ll inv[300001];
ll pow1(ll n, ll p) {
  ll res, cur;
  res = 1;
  cur = n;
  while (p) {
    if (p & 1) {
      res *= cur;
      res %= MOD;
    }
    p /= 2;
    cur *= cur;
    cur %= MOD;
  }
  return res;
}

void init() {
  inv[0] = 1;
  fact[0] = 1;
  for (int i = 1; i < 300001; i++) {
    fact[i] = fact[i - 1] * i % MOD;
    inv[i] = pow1(fact[i], MOD - 2);
  }
}

ll cnk(ll n, ll k) {
  if (k > n)
    return 0;
  return fact[n] * inv[k] % MOD * inv[n - k] % MOD;
}

int main() {
  //  freopen("in.txt", "r", stdin);
  //  freopen("out.txt", "w", stdout);
  init();
  ll res = 0;
  long long n, a, b, k;
  cin >> n >> a >> b >> k;
  for (int i = 0; i <= n; i++) {
    if (a * i > k)
      break;
    if ((k - a * i) % b != 0)
      continue;
    res = (res + cnk(n, i) * cnk(n, (k - a * i) / b) % MOD) % MOD;
  }
  cout << res;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;
typedef long long ll;
const int MAXN = 300005;
const int MOD = 998244353;

int n, a, b;
ll k, ans;
ll inv[MAXN], fac[MAXN], ifac[MAXN];

inline ll C(int a, int b) {
  if (a == b || b == 0)
    return 1;
  return fac[a] * ifac[b] % MOD * ifac[a - b] % MOD;
}

int main() {
  scanf("%d%d%d%lld", &n, &a, &b, &k);
  inv[1] = 1;
  for (int i = 2; i <= n; ++i)
    inv[i] = (ll)(MOD - MOD / i) * inv[MOD % i] % MOD;
  fac[0] = ifac[0] = 1;
  for (int i = 1; i <= n; ++i) {
    fac[i] = fac[i - 1] * i % MOD;
    ifac[i] = ifac[i - 1] * inv[i] % MOD;
  }
  for (int i = 0; i <= n; ++i) {
    if ((k - (ll)i * a) % b)
      continue;
    ll j = (k - (ll)i * a) / b;
    if (j > n)
      continue;
    ans = (ans + C(n, i) * C(n, (int)j) % MOD) % MOD;
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;
typedef long long ll;
const int MAXN = 300005;
const int MOD = 998244353;

int n, a, b;
ll k, ans;
ll inv[MAXN], fac[MAXN], ifac[MAXN];

inline ll C(int a, int b) {
  if (a == b || b == 0)
    return 1;
  return fac[a] * ifac[b] % MOD * ifac[a - b] % MOD;
}

int main() {
  scanf("%d%d%d%lld", &n, &a, &b, &k);
  inv[1] = 1;
  for (int i = 2; i <= n; ++i)
    inv[i] = (ll)(MOD - MOD / i) * inv[MOD % i] % MOD;
  fac[0] = ifac[0] = 1;
  for (int i = 1; i <= n; ++i) {
    fac[i] = fac[i - 1] * i % MOD;
    ifac[i] = ifac[i - 1] * inv[i] % MOD;
  }
  for (int i = 0; i <= n; ++i) {
    if ((ll)i * a > k)
      break;
    if ((k - (ll)i * a) % b)
      continue;
    ll j = (k - (ll)i * a) / b;
    if (j > n)
      continue;
    ans = (ans + C(n, i) * C(n, (int)j) % MOD) % MOD;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
// const int INF=1e9,MOD=1e9+7;
const ll LINF = 1e18;
using namespace std;
#define int long long
// template
const int MOD = 998244353;
std::vector<int> v(1000000);
std::vector<int> w(1000000);
int pw(int n, int k) {
  int res = 1;
  while (k) {
    if (k % 2)
      res *= n;
    res %= MOD;
    k /= 2;
    n *= n;
    n %= MOD;
  }
  return res;
}
int nCk(int n, int k) {
  int res = v[n];
  res *= w[k];
  res %= MOD;
  res *= w[n - k];
  res %= MOD;
  return res;
}
// main
signed main() {
  int N, A, B, K;
  cin >> N >> A >> B >> K;
  v[0] = 1;
  for (int i = 1; i < 1000000; i++)
    v[i] = v[i - 1] * i % MOD;
  for (int i = 0; i < 1000000; i++)
    w[i] = pw(v[i], MOD - 2);
  int ans = 0;
  for (int a = 0; a <= N; a++) {
    if (K - A * a < 0)
      break;
    if ((K - A * a) % B)
      continue;
    int b = (K - A * a) / B;
    if (b > N)
      continue;
    for (int t = max(0LL, a + b - N); t <= min(a, b); t++) {
      ans +=
          (nCk(N, t) * nCk(N - t, a - t)) % MOD * nCk(N - t - (a - t), b - t);
      ans %= MOD;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
// const int INF=1e9,MOD=1e9+7;
const ll LINF = 1e18;
using namespace std;
#define int long long
// template
const int MOD = 998244353;
std::vector<int> v(1000000);
std::vector<int> w(1000000);
int pw(int n, int k) {
  int res = 1;
  while (k) {
    if (k % 2)
      res *= n;
    res %= MOD;
    k /= 2;
    n *= n;
    n %= MOD;
  }
  return res;
}
int nCk(int n, int k) {
  int res = v[n];
  res *= w[k];
  res %= MOD;
  res *= w[n - k];
  res %= MOD;
  return res;
}
// main
signed main() {
  int N, A, B, K;
  cin >> N >> A >> B >> K;
  v[0] = 1;
  for (int i = 1; i < 1000000; i++)
    v[i] = v[i - 1] * i % MOD;
  for (int i = 0; i < 1000000; i++)
    w[i] = pw(v[i], MOD - 2);
  int ans = 0;
  for (int a = 0; a <= N; a++) {
    if (K - A * a < 0)
      break;
    if ((K - A * a) % B)
      continue;
    int b = (K - A * a) / B;
    if (b > N)
      continue;
    ans += nCk(N, a) * nCk(N, b);
    ans %= MOD;
  }
  cout << ans << endl;
}
Problem p03332 (C++)

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

#define rep(i, j, n) for (int i = j; i < n; ++i)
#define all(i) i.begin(), i.end()
#define rall(i) i.rbegin(), i.rend()
#define INF 1e9
#define Puts(x) puts(x ? "Yes" : "No")
const int mod = 998244353;

typedef long long i64;
typedef pair<int, int> pi;

template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vector<T>>;

i64 gcd(int n, int m) { return (m == 0 ? n : gcd(m, n % m)); }
i64 lcm(int n, int m) { return (n / gcd(n, m) * m); }
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};

vt<i64> f;

i64 power(i64 x, i64 n) {
  i64 ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= mod;
    (x *= x) %= mod;
    n >>= 1;
  }
  return ret;
}

i64 comb(i64 n, i64 k) {
  i64 res = ((f[n] * power(f[k], mod - 2)) % mod) * power(f[n - k], mod - 2);
  res %= mod;
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  i64 n, a, b, k;
  cin >> n >> a >> b >> k;

  f.resize(n + 1);
  f[0] = 1;
  for (i64 i = 1; i <= n; ++i) {
    f[i] = f[i - 1] * i;
    f[i] %= mod;
  }

  i64 ans = 0;
  for (i64 i = 0; i <= n; ++i) {
    if (k < a * i)
      continue;

    if ((k - a * i) % b == 0) {
      i64 j = (k - a * i) / b;

      ans += comb(n, i) * comb(n, j);
      ans %= mod;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, j, n) for (int i = j; i < n; ++i)
#define all(i) i.begin(), i.end()
#define rall(i) i.rbegin(), i.rend()
#define INF 1e9
#define Puts(x) puts(x ? "Yes" : "No")
const int mod = 998244353;

typedef long long i64;
typedef pair<int, int> pi;

template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vector<T>>;

i64 gcd(int n, int m) { return (m == 0 ? n : gcd(m, n % m)); }
i64 lcm(int n, int m) { return (n / gcd(n, m) * m); }
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};

vt<i64> f;

i64 power(i64 x, i64 n) {
  i64 ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= mod;
    (x *= x) %= mod;
    n >>= 1;
  }
  return ret;
}

i64 comb(i64 n, i64 k) {
  i64 res = ((f[n] * power(f[k], mod - 2)) % mod) * power(f[n - k], mod - 2);
  res %= mod;
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  i64 n, a, b, k;
  cin >> n >> a >> b >> k;

  f.resize(n + 1);
  f[0] = 1;
  for (i64 i = 1; i <= n; ++i) {
    f[i] = f[i - 1] * i;
    f[i] %= mod;
  }

  i64 ans = 0;
  for (i64 i = 0; i <= n; ++i) {
    if (k < a * i)
      continue;

    if ((k - a * i) % b == 0) {
      i64 j = (k - a * i) / b;
      if (j > n)
        continue;

      ans += comb(n, i) * comb(n, j);
      ans %= mod;
    }
  }
  cout << ans << endl;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 998244353;
const ll INF = 1000000010;
const ll LINF = 4000000000000000010LL;
const int MAX = 310;
const double EPS = 1e-9;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
ll mod_pow(ll x, ll n, ll mod) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1) {
      ret = ret * x % mod;
    }
    x = x * x % mod;
    n >>= 1;
  }
  return ret;
}

ll f[300010], fr[300010];

ll ncr(ll n, ll r) {
  if (r == 0) {
    return 1;
  } else {
    return ((f[n] * fr[r]) % MOD * fr[n - r]) % MOD;
  }
}

int main() {
  f[0] = 1;
  ll n, a, b, k;
  cin >> n >> a >> b >> k;
  for (ll i = 1; i <= n + 1; i++) {
    f[i] = (f[i - 1] * i) % MOD;
  }
  fr[n + 1] = mod_pow(f[n + 1], MOD - 2, MOD);
  for (ll i = n; i >= 0; i--) {
    fr[i] = (fr[i + 1] * (i + 1)) % MOD;
  }
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    if ((k - (a * i)) % b)
      continue;
    ll p = (k - (a * i)) / b;
    if (p < 0 || a * i + p * b != k)
      continue;
    (ans += (ncr(n, i) * ncr(n, p))) %= MOD;
  }
  cout << ans % MOD << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 998244353;
const ll INF = 1000000010;
const ll LINF = 4000000000000000010LL;
const int MAX = 310;
const double EPS = 1e-9;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
ll mod_pow(ll x, ll n, ll mod) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1) {
      ret = ret * x % mod;
    }
    x = x * x % mod;
    n >>= 1;
  }
  return ret;
}

ll f[300010], fr[300010];

ll ncr(ll n, ll r) {
  if (r == 0) {
    return 1;
  } else {
    return ((f[n] * fr[r]) % MOD * fr[n - r]) % MOD;
  }
}

int main() {
  f[0] = 1;
  ll n, a, b, k;
  cin >> n >> a >> b >> k;
  for (ll i = 1; i <= n + 1; i++) {
    f[i] = (f[i - 1] * i) % MOD;
  }
  fr[n + 1] = mod_pow(f[n + 1], MOD - 2, MOD);
  for (ll i = n; i >= 0; i--) {
    fr[i] = (fr[i + 1] * (i + 1)) % MOD;
  }
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    if ((k - (a * i)) % b)
      continue;
    ll p = (k - (a * i)) / b;
    if (p < 0 || a * i + p * b != k || p > n)
      continue;
    (ans += (ncr(n, i) * ncr(n, p))) %= MOD;
  }
  cout << ans % MOD << endl;
  return 0;
}