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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define int long long
const int N = 3e5 + 5;
const int M = 1e6 + 5;
const int mod = 998244353;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef set<int> si;
typedef map<string, int> msi;
int n, a, b, k;
int mul[N], dv[N];
int po(int x, int y) {
  if (y == 0)
    return 1LL;
  int temp = po(x, y >> 1);
  if ((y & 1))
    return temp * temp % mod * x % mod;
  else
    return temp * temp % mod;
}
int pre() {
  mul[0] = dv[0] = 1;
  for (int i = 1; i <= n; ++i)
    mul[i] = mul[i - 1] * i % mod;
  dv[n] = po(mul[n], mod - 2) % mod;
  for (int i = n - 1; i >= 0; --i)
    dv[i] = dv[i + 1] * (i + 1) % mod;
}
int c(int k, int n) { return mul[n] * dv[k] % mod * dv[n - k] % mod; }

signed main() {
  cin >> n >> a >> b >> k;
  int ans = 0;
  pre();
  for (int i = 0; i <= n && a * i <= k; ++i)
    if ((k - a * i) % b == 0) {
      int j = (k - a * i) / b;
      ans = (ans + c(i, n) * c(j, n) % mod) % mod;
    }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define int long long
const int N = 3e5 + 5;
const int M = 1e6 + 5;
const int mod = 998244353;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef set<int> si;
typedef map<string, int> msi;
int n, a, b, k;
int mul[N], dv[N];
int po(int x, int y) {
  if (y == 0)
    return 1LL;
  int temp = po(x, y >> 1);
  if ((y & 1))
    return temp * temp % mod * x % mod;
  else
    return temp * temp % mod;
}
int pre() {
  mul[0] = dv[0] = 1;
  for (int i = 1; i <= n; ++i)
    mul[i] = mul[i - 1] * i % mod;
  dv[n] = po(mul[n], mod - 2) % mod;
  for (int i = n - 1; i >= 0; --i)
    dv[i] = dv[i + 1] * (i + 1) % mod;
}
int c(int k, int n) { return mul[n] * dv[k] % mod * dv[n - k] % mod; }

signed main() {
  cin >> n >> a >> b >> k;
  int ans = 0;
  pre();
  for (int i = 0; i <= n && a * i <= k; ++i)
    if ((k - a * i) % b == 0 && (k - a * i) <= b * n) {
      int j = (k - a * i) / b;
      ans = (ans + c(i, n) * c(j, n) % mod) % mod;
    }
  cout << ans;
}
Problem p03332 (C++)

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

typedef long long ll;
typedef double lf;
typedef long double Lf;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define TRACE(x) cerr << #x << "  " << x << endl
#define FOR(i, a, b) for (int i = (a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define _ << " " <<

#define fi first
#define sec second
#define mp make_pair

const int MOD = 998244353;
const int MAXN = 300100;

int add(int a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  return a;
}

int mul(int a, int b) { return (ll)a * b % MOD; }

int pot(int a, int b) {
  if (!b)
    return 1;
  if (b % 2)
    return mul(a, pot(a, b - 1));
  int k = pot(a, b / 2);
  return mul(k, k);
}

int inv(int a) { return pot(a, MOD - 2); }

int fak[MAXN];

int choose(int n, int k) { return mul(fak[n], inv(mul(fak[k], fak[n - k]))); }

int n, a, b;
ll k;

int main() {
  fak[0] = 1;
  FOR(i, 1, MAXN) fak[i] = mul(fak[i - 1], i);
  cin >> n >> a >> b >> k;
  ll sol = 0;
  REP(i, n + 1) {
    ll tream = (k - (ll)i * a) / b;
    if (tream * b != k - (ll)i * a)
      continue;
    if (tream > n)
      continue;
    sol = add(sol, mul(choose(n, i), choose(n, tream)));
  }

  cout << sol << endl;
  return 0;
}


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

typedef long long ll;
typedef double lf;
typedef long double Lf;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define TRACE(x) cerr << #x << "  " << x << endl
#define FOR(i, a, b) for (int i = (a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define _ << " " <<

#define fi first
#define sec second
#define mp make_pair

const int MOD = 998244353;
const int MAXN = 300100;

int add(int a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  return a;
}

int mul(int a, int b) { return (ll)a * b % MOD; }

int pot(int a, int b) {
  if (!b)
    return 1;
  if (b % 2)
    return mul(a, pot(a, b - 1));
  int k = pot(a, b / 2);
  return mul(k, k);
}

int inv(int a) { return pot(a, MOD - 2); }

int fak[MAXN];

int choose(int n, int k) { return mul(fak[n], inv(mul(fak[k], fak[n - k]))); }

int n, a, b;
ll k;

int main() {
  fak[0] = 1;
  FOR(i, 1, MAXN) fak[i] = mul(fak[i - 1], i);
  cin >> n >> a >> b >> k;
  ll sol = 0;
  REP(i, n + 1) {
    ll tream = (k - (ll)i * a) / b;
    if (tream * b != k - (ll)i * a)
      continue;
    if (tream > n || tream < 0)
      continue;
    sol = add(sol, mul(choose(n, i), choose(n, tream)));
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 998244353;
int mod(int n) { return (n % MOD + MOD) % MOD; }
int fp(int a, int p) {
  int ans = 1, cur = a;
  for (int i = 0; (1ll << i) <= p; ++i) {
    if ((p >> i) & 1)
      ans = mod(ans * cur);
    cur = mod(cur * cur);
  }
  return ans;
}
void add(int &a, int b) { a = mod(a + b); }
int mdiv(int a, int b) { return mod(a * fp(b, MOD - 2)); }
const int N = 1e6 + 7;
int f[N];
int C(int n, int k) { return mdiv(f[n], mod(f[k] * f[n - k])); }
signed main() {
#ifdef HOME
  freopen("input.txt", "r", stdin);
#else
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif
  f[0] = 1;
  for (int i = 1; i < N; ++i)
    f[i] = mod(f[i - 1] * i);
  int n, a, b, k;
  cin >> n >> a >> b >> k;
  int ans = 0;
  for (int i = 0; i <= n && i * a <= k; ++i) {
    int t = k - i * a;
    if (t % b == 0) {
      add(ans, C(n, i) * C(n, t / b));
    }
  }
  cout << ans << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 998244353;
int mod(int n) { return (n % MOD + MOD) % MOD; }
int fp(int a, int p) {
  int ans = 1, cur = a;
  for (int i = 0; (1ll << i) <= p; ++i) {
    if ((p >> i) & 1)
      ans = mod(ans * cur);
    cur = mod(cur * cur);
  }
  return ans;
}
void add(int &a, int b) { a = mod(a + b); }
int mdiv(int a, int b) { return mod(a * fp(b, MOD - 2)); }
const int N = 1e6 + 7;
int f[N];
int C(int n, int k) { return mdiv(f[n], mod(f[k] * f[n - k])); }
signed main() {
#ifdef HOME
  freopen("input.txt", "r", stdin);
#else
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif
  f[0] = 1;
  for (int i = 1; i < N; ++i)
    f[i] = mod(f[i - 1] * i);
  int n, a, b, k;
  cin >> n >> a >> b >> k;
  int ans = 0;
  for (int i = 0; i <= n && i * a <= k; ++i) {
    int t = k - i * a;
    if (t % b == 0 && t / b <= n) {
      add(ans, C(n, i) * C(n, t / b));
    }
  }
  cout << ans << '\n';
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

#define MAX_K 500000
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;

ll combination_n[MAX_K + 1]; // combination_n[i] := nCi

ll extend_gcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll r = a % b;
  ll d = extend_gcd(b, r, y, x);
  y -= (a / b) * x;

  return d;
}

void precompute_for_combination(ll n, ll k, ll p) {
  combination_n[0] = 1;

  for (int i = 1; i <= k; i++) {
    ll x, y;
    extend_gcd(i, p, x, y);

    x %= p;

    if (x < 0)
      x = p + x;

    combination_n[i] = combination_n[i - 1] * (n - i + 1) % p * x % p;
  }
}

ll N, A, B, K;
int main() {
  cin >> N >> A >> B >> K;
  ll p = 998244353;

  precompute_for_combination(N, N, p);

  ll ret = 0;
  rep(i, N + 1) {
    ll a = i;
    ll b = K - a * A;
    if (b % B)
      continue;
    b /= B;
    if (b > N)
      continue;
    (ret += combination_n[a] * combination_n[b] % p) %= p;
  }

  cout << ret << endl;

  return 0;
}

Fixed submission:
#include <iostream>

#define MAX_K 500000
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;

ll combination_n[MAX_K + 1]; // combination_n[i] := nCi

ll extend_gcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll r = a % b;
  ll d = extend_gcd(b, r, y, x);
  y -= (a / b) * x;

  return d;
}

void precompute_for_combination(ll n, ll k, ll p) {
  combination_n[0] = 1;

  for (int i = 1; i <= k; i++) {
    ll x, y;
    extend_gcd(i, p, x, y);

    x %= p;

    if (x < 0)
      x = p + x;

    combination_n[i] = combination_n[i - 1] * (n - i + 1) % p * x % p;
  }
}

ll N, A, B, K;
int main() {
  cin >> N >> A >> B >> K;
  ll p = 998244353;

  precompute_for_combination(N, N, p);

  ll ret = 0;
  rep(i, N + 1) {
    ll a = i;
    ll b = K - a * A;
    if (b < 0 || b % B)
      continue;
    b /= B;
    if (b > N)
      continue;
    (ret += combination_n[a] * combination_n[b] % p) %= p;
  }

  cout << ret << endl;

  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

#define MAX_N 100000

using namespace std;

typedef long long ll;

#define rep(i, n) for (int i = 0; i < (n); i++)

ll factorial[MAX_N + 1];         // factorial[n] := n! mod p
ll factorial_inverse[MAX_N + 1]; // factorial_inverse[n] = (n!)^-1 mod p

ll extend_gcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll r = a % b;
  ll d = extend_gcd(b, r, y, x);
  y -= (a / b) * x;

  return d;
}

void precompute_for_combination(ll n,
                                ll p) { // 前計算 階乗や階乗の逆限を求めておく
  factorial[0] = factorial[1] = 1;
  for (int i = 2; i <= n; i++) {
    factorial[i] = factorial[i - 1] * i % p;
  }

  ll y;
  extend_gcd(factorial[n], p, factorial_inverse[n], y);

  factorial_inverse[n] %= p;
  if (factorial_inverse[n] < 0)
    factorial_inverse[n] = p - factorial_inverse[n];

  for (int i = n; i > 0; i--) {
    factorial_inverse[i - 1] = factorial_inverse[i] * i % p;
  }
}

ll combination(ll n, ll k, ll p) {
  if (n < k || n < 0 || k < 0)
    return 0; // 不正な値
  return factorial[n] % p * factorial_inverse[k] % p *
         factorial_inverse[n - k] % p;
}
ll N, A, B, K;
int main() {
  cin >> N >> A >> B >> K;
  ll p = 998244353;

  precompute_for_combination(N, p);

  ll ret = 0;
  rep(i, N + 1) {
    ll a = i;
    ll b = K - a * A;
    if (b % B)
      continue;
    b /= B;
    (ret += combination(N, a, p) * combination(N, b, p) % p) %= p;
  }

  cout << ret << endl;

  return 0;
}

Fixed submission:
#include <iostream>

#define MAX_N 500000

using namespace std;

typedef long long ll;

#define rep(i, n) for (int i = 0; i < (n); i++)

ll factorial[MAX_N + 1];         // factorial[n] := n! mod p
ll factorial_inverse[MAX_N + 1]; // factorial_inverse[n] = (n!)^-1 mod p

ll extend_gcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll r = a % b;
  ll d = extend_gcd(b, r, y, x);
  y -= (a / b) * x;

  return d;
}

void precompute_for_combination(ll n,
                                ll p) { // 前計算 階乗や階乗の逆限を求めておく
  factorial[0] = factorial[1] = 1;
  for (int i = 2; i <= n; i++) {
    factorial[i] = factorial[i - 1] * i % p;
  }

  ll y;
  extend_gcd(factorial[n], p, factorial_inverse[n], y);

  factorial_inverse[n] %= p;
  if (factorial_inverse[n] < 0)
    factorial_inverse[n] = p - factorial_inverse[n];

  for (int i = n; i > 0; i--) {
    factorial_inverse[i - 1] = factorial_inverse[i] * i % p;
  }
}

ll combination(ll n, ll k, ll p) {
  if (n < k || n < 0 || k < 0)
    return 0; // 不正な値
  return factorial[n] % p * factorial_inverse[k] % p *
         factorial_inverse[n - k] % p;
}
ll N, A, B, K;
int main() {
  cin >> N >> A >> B >> K;
  ll p = 998244353;

  precompute_for_combination(N, p);

  ll ret = 0;
  rep(i, N + 1) {
    ll a = i;
    ll b = K - a * A;
    if (b % B)
      continue;
    b /= B;
    (ret += combination(N, a, p) * combination(N, b, p) % p) %= p;
  }

  cout << ret << endl;

  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define mp make_pair
#define fi first
#define sc second

ll mod = 998244353;

ll n, a, b, k;

ll nc[200000];

ll mod_pow(ll x, ll n) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= mod;
    (x *= x) %= mod;
    n /= 2;
  }
  return ret;
}

int main() {
  cin >> n >> a >> b >> k;

  nc[0] = 1;

  FOR(k, 1, n + 1) {
    (nc[k] = nc[k - 1] * (n - k + 1)) %= mod;

    (nc[k] *= mod_pow(k, mod - 2)) %= mod;
  }

  ll ans = 0;
  REP(i, n + 1) {
    if (k - a * i < 0 || (k - a * i) % b)
      continue;

    ll j = (k - a * i) / b;
    if (j > n)
      continue;

    (ans += nc[i] * nc[j]) %= mod;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define mp make_pair
#define fi first
#define sc second

ll mod = 998244353;

ll n, a, b, k;

ll nc[500000];

ll mod_pow(ll x, ll n) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= mod;
    (x *= x) %= mod;
    n /= 2;
  }
  return ret;
}

int main() {
  cin >> n >> a >> b >> k;

  nc[0] = 1;

  FOR(k, 1, n + 1) {
    (nc[k] = nc[k - 1] * (n - k + 1)) %= mod;

    (nc[k] *= mod_pow(k, mod - 2)) %= mod;
  }

  ll ans = 0;
  REP(i, n + 1) {
    if (k - a * i < 0 || (k - a * i) % b)
      continue;

    ll j = (k - a * i) / b;
    if (j > n)
      continue;

    (ans += nc[i] * nc[j]) %= mod;
  }
  cout << ans << endl;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define mod 998244353
using namespace std;
int fac[1000006], inv[1000006];
int fpw(int x, int y) {
  int ans = 1;
  for (int i = 1; i <= y; i <<= 1, x = 1LL * x * x % mod)
    if (y & i)
      ans = 1LL * ans * x % mod;
  return ans;
}
int C(int n, int m) { return 1LL * fac[n] * inv[m] % mod * inv[n - m] % mod; }
int main() {
  int n, a, b;
  long long k;
  cin >> n >> a >> b >> k;
  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = 1LL * fac[i - 1] * i % mod;
  inv[n] = fpw(fac[n], mod - 2);
  for (int i = n - 1; i >= 0; --i)
    inv[i] = 1LL * inv[i + 1] * (i + 1) % mod;
  int ans = 0;
  for (int i = 0; i <= n; ++i)
    if ((k - i * a) % b == 0 && (k - i * a) / b <= n)
      ans = (ans + 1LL * C(n, i) * C(n, (k - i * a) / b) % mod) % mod;
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
#define mod 998244353
using namespace std;
int fac[1000006], inv[1000006];
int fpw(int x, int y) {
  int ans = 1;
  for (int i = 1; i <= y; i <<= 1, x = 1LL * x * x % mod)
    if (y & i)
      ans = 1LL * ans * x % mod;
  return ans;
}
int C(int n, int m) { return 1LL * fac[n] * inv[m] % mod * inv[n - m] % mod; }
int main() {
  int n, a, b;
  long long k;
  cin >> n >> a >> b >> k;
  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = 1LL * fac[i - 1] * i % mod;
  inv[n] = fpw(fac[n], mod - 2);
  for (int i = n - 1; i >= 0; --i)
    inv[i] = 1LL * inv[i + 1] * (i + 1) % mod;
  int ans = 0;
  for (int i = 0; i <= min(1LL * n, k / a); ++i)
    if ((k - i * a) % b == 0 && (k - i * a) / b <= n)
      ans = (ans + 1LL * C(n, i) * C(n, (k - i * a) / b) % mod) % mod;
  cout << ans << endl;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

#define forsn(i, s, n) for (int i = (s); i < (int)(n); i++)
#define forn(i, n) forsn(i, 0, n)
#define dforsn(i, s, n) for (int i = (n)-1; i >= (int)(s); i--)
#define dforn(i, n) dforsn(i, 0, n)
#define all(v) ((v).begin(), (v).end())
#define F first
#define S second

typedef long long int tint;

const int MOD = 998244353;
tint fact[300010];
tint inv[300010];
tint factinv[300010];

tint choose(tint n, tint k) {
  return (((fact[n] * factinv[k]) % MOD) * factinv[n - k]) % MOD;
}

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

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

  fact[0] = 1;
  forsn(i, 1, n + 1) fact[i] = (fact[i - 1] * i) % MOD;
  inv[0] = 1;
  inv[1] = 1;
  forsn(i, 1 + 1, n + 1) {
    inv[i] = (MOD - (MOD / i) * inv[MOD % i] % MOD) % MOD;
  }
  factinv[0] = 1;
  factinv[1] = 1;
  forsn(i, 1 + 1, n + 1) { factinv[i] = (factinv[i - 1] * inv[i]) % MOD; }

  tint res = 0;
  forn(p, n + 1) if (!((k - p * a) % b)) {
    tint q = (k - p * a) / b;
    if (q <= n) {
      res = (res + (choose(n, p) * choose(n, q)) % MOD) % MOD;
    }
  }
  cout << res << endl;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

#define forsn(i, s, n) for (int i = (s); i < (int)(n); i++)
#define forn(i, n) forsn(i, 0, n)
#define dforsn(i, s, n) for (int i = (n)-1; i >= (int)(s); i--)
#define dforn(i, n) dforsn(i, 0, n)
#define all(v) ((v).begin(), (v).end())
#define F first
#define S second

typedef long long int tint;

const int MOD = 998244353;
tint fact[300010];
tint inv[300010];
tint factinv[300010];

tint choose(tint n, tint k) {
  return (((fact[n] * factinv[k]) % MOD) * factinv[n - k]) % MOD;
}

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

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

  fact[0] = 1;
  forsn(i, 1, n + 1) fact[i] = (fact[i - 1] * i) % MOD;
  inv[0] = 1;
  inv[1] = 1;
  forsn(i, 1 + 1, n + 1) {
    inv[i] = (MOD - (MOD / i) * inv[MOD % i] % MOD) % MOD;
  }
  factinv[0] = 1;
  factinv[1] = 1;
  forsn(i, 1 + 1, n + 1) { factinv[i] = (factinv[i - 1] * inv[i]) % MOD; }

  tint res = 0;
  forn(p, n + 1) if (k >= p * a && !((k - p * a) % b)) {
    tint q = (k - p * a) / b;
    if (q <= n) {
      res = (res + (choose(n, p) * choose(n, q)) % MOD) % MOD;
    }
  }
  cout << res << endl;
}
Problem p03332 (C++)

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

typedef long long ll;
typedef pair<int, int> pii;

const int mo = 998244353;
const int maxn = 300007;

ll n, a, b, k, fac[maxn], inv[maxn];

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

int qpow(int x, int p) {
  int rt = 1;
  while (p) {
    if (p & 1)
      rt = (1ll * rt * x) % mo;
    x = (1ll * x * x) % mo;
    p >>= 1;
  }
  return rt;
}
int res;
int main() {
  fac[0] = inv[0] = 1;
  for (int i = 1; i < maxn; ++i) {
    fac[i] = 1ll * fac[i - 1] * i % mo;
    inv[i] = qpow(fac[i], mo - 2);
  }
  scanf("%lld%lld%lld%lld", &n, &a, &b, &k);
  for (int i = 0; i <= n && i * a <= k; ++i)
    if ((k - i * a) % b == 0)
      res = (res + 1ll * C(n, i) * C(n, (k - i * a) / b) % mo) % mo;
  printf("%d\n", res);
  return 0;
}


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

typedef long long ll;
typedef pair<int, int> pii;

const int mo = 998244353;
const int maxn = 300007;

ll n, a, b, k, fac[maxn], inv[maxn];

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

int qpow(int x, int p) {
  int rt = 1;
  while (p) {
    if (p & 1)
      rt = (1ll * rt * x) % mo;
    x = (1ll * x * x) % mo;
    p >>= 1;
  }
  return rt;
}
int res;
int main() {
  fac[0] = inv[0] = 1;
  for (int i = 1; i < maxn; ++i) {
    fac[i] = 1ll * fac[i - 1] * i % mo;
    inv[i] = qpow(fac[i], mo - 2);
  }
  scanf("%lld%lld%lld%lld", &n, &a, &b, &k);
  for (int i = 0; i <= n && i * a <= k; ++i)
    if ((k - i * a) % b == 0 && n * b >= k - i * a)
      res = (res + 1ll * C(n, i) * C(n, (k - i * a) / b) % mo) % mo;
  printf("%d\n", res);
  return 0;
}
Problem p03332 (C++)

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

int n, a, b, k, mod = 998244353;

int factorialNumInverse[300003], naturalNumInverse[300003], fact[300003];

void InverseofNumber(int mod) {
  naturalNumInverse[0] = naturalNumInverse[1] = 1;
  for (int i = 2; i <= 300000; i++) {
    naturalNumInverse[i] = naturalNumInverse[mod % i] * (mod - mod / i) % mod;
  }
}

void InverseofFactorial(int mod) {
  factorialNumInverse[0] = factorialNumInverse[1] = 1;
  for (int i = 2; i <= 300000; i++) {
    factorialNumInverse[i] =
        (naturalNumInverse[i] * factorialNumInverse[i - 1]) % mod;
  }
}

void factorial(int mod) {
  fact[0] = 1;
  for (int i = 1; i <= 300000; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
  }
}

int nCr(int N, int R) {
  return ((fact[N] * factorialNumInverse[R]) % mod *
          factorialNumInverse[N - R]) %
         mod;
}

signed main() {
  InverseofNumber(mod);
  InverseofFactorial(mod);
  factorial(mod);
  int ans = 0;
  cin >> n >> a >> b >> k;
  for (int x = 0; x <= n; x++) {
    int y = (k - a * x) / b;
    if (y >= 0 && a * x + b * y == k) {
      ans = (ans + (nCr(n, x) * nCr(n, y)) % mod) % mod;
    }
  }
  cout << ans << '\n';
}

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

int n, a, b, k, mod = 998244353;

int factorialNumInverse[300003], naturalNumInverse[300003], fact[300003];

void InverseofNumber(int mod) {
  naturalNumInverse[0] = naturalNumInverse[1] = 1;
  for (int i = 2; i <= 300000; i++) {
    naturalNumInverse[i] = naturalNumInverse[mod % i] * (mod - mod / i) % mod;
  }
}

void InverseofFactorial(int mod) {
  factorialNumInverse[0] = factorialNumInverse[1] = 1;
  for (int i = 2; i <= 300000; i++) {
    factorialNumInverse[i] =
        (naturalNumInverse[i] * factorialNumInverse[i - 1]) % mod;
  }
}

void factorial(int mod) {
  fact[0] = 1;
  for (int i = 1; i <= 300000; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
  }
}

int nCr(int N, int R) {
  return ((fact[N] * factorialNumInverse[R]) % mod *
          factorialNumInverse[N - R]) %
         mod;
}

signed main() {
  InverseofNumber(mod);
  InverseofFactorial(mod);
  factorial(mod);
  int ans = 0;
  cin >> n >> a >> b >> k;
  for (int x = 0; x <= n; x++) {
    int y = (k - a * x) / b;
    if (y >= 0 && y <= n && a * x + b * y == k) {
      ans = (ans + (nCr(n, x) * nCr(n, y)) % mod) % mod;
    }
  }
  cout << ans << '\n';
}
Problem p03332 (C++)

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

typedef long long ll;

const int N = 301000, MOD = 998244353;

ll inv(int x) {
  return (x == 1 || x == -1) ? x : (-(MOD / x) * inv(MOD % x) % MOD);
}

int fact[N], ifact[N];

int C(int n, int m) {
  if (n < m)
    return 0;
  return (ll)fact[n] * ifact[m] % MOD * ifact[n - m] % MOD;
}

void initialize() {
  fact[0] = 1;
  for (int i = 1; i < N; i++)
    fact[i] = (ll)fact[i - 1] * i % MOD;
  ifact[N - 1] = inv(fact[N - 1]);
  for (int i = N - 1; i; i--)
    ifact[i - 1] = (ll)ifact[i] * i % MOD;
}

int n;
ll a, b, k;

int main() {
  initialize();

  scanf("%d%lld%lld%lld", &n, &a, &b, &k);

  int ans = 0;

  for (int i = 0, j; i <= n; i++)
    if ((k - i * a) % b == 0) {
      j = (k - i * a) / b;
      ans = (ans + (ll)C(n, i) * C(n, j)) % MOD;
    }

  ans = (ans + MOD) % MOD;
  printf("%d\n", ans);

  return 0;
}


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

typedef long long ll;

const int N = 301000, MOD = 998244353;

ll inv(int x) {
  return (x == 1 || x == -1) ? x : (-(MOD / x) * inv(MOD % x) % MOD);
}

int fact[N], ifact[N];

int C(int n, int m) {
  if (n < m)
    return 0;
  return (ll)fact[n] * ifact[m] % MOD * ifact[n - m] % MOD;
}

void initialize() {
  fact[0] = 1;
  for (int i = 1; i < N; i++)
    fact[i] = (ll)fact[i - 1] * i % MOD;
  ifact[N - 1] = inv(fact[N - 1]);
  for (int i = N - 1; i; i--)
    ifact[i - 1] = (ll)ifact[i] * i % MOD;
}

int n;
ll a, b, k;

int main() {
  initialize();

  scanf("%d%lld%lld%lld", &n, &a, &b, &k);

  int ans = 0;

  for (int i = 0, j; i <= n && i * a <= k; i++)
    if ((k - i * a) % b == 0) {
      j = (k - i * a) / b;
      ans = (ans + (ll)C(n, i) * C(n, j)) % MOD;
    }

  ans = (ans + MOD) % MOD;
  printf("%d\n", ans);

  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
#define MAXN 300000
#define LL long long
#define MO 998244353
LL fac[MAXN + 5], inv[MAXN + 5];
LL powmod(LL a, LL b) {
  LL ret = 1;
  while (b) {
    if (b & 1)
      ret = ret * a % MO;
    a = a * a % MO;
    b >>= 1;
  }
  return ret;
}
void Init() {
  fac[0] = inv[0] = 1;
  for (int i = 1; i <= MAXN; i++)
    fac[i] = i * fac[i - 1] % MO;
  inv[MAXN] = powmod(fac[MAXN], MO - 2);
  for (int i = MAXN - 1; i >= 1; i--)
    inv[i] = (i + 1) * inv[i + 1] % MO;
}
LL n, A, B, k, x, y, d, tot;
LL Solve(int i, int j) {
  return inv[i] * inv[j] % MO * inv[n - i] % MO * inv[n - j] % MO;
}
int main() {
  Init();
  scanf("%lld%lld%lld%lld", &n, &A, &B, &k);
  LL ans = 0;
  for (int i = 0; i <= n && i * A <= k; i++) {
    LL L = k - A * i;
    if (L % B == 0) {
      ans += Solve(i, L / B);
      if (ans >= MO)
        ans -= MO;
    }
  }
  printf("%lld\n", ans * fac[n] % MO * fac[n] % MO);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
#define MAXN 300000
#define LL long long
#define MO 998244353
LL fac[MAXN + 5], inv[MAXN + 5];
LL powmod(LL a, LL b) {
  LL ret = 1;
  while (b) {
    if (b & 1)
      ret = ret * a % MO;
    a = a * a % MO;
    b >>= 1;
  }
  return ret;
}
void Init() {
  fac[0] = inv[0] = 1;
  for (int i = 1; i <= MAXN; i++)
    fac[i] = i * fac[i - 1] % MO;
  inv[MAXN] = powmod(fac[MAXN], MO - 2);
  for (int i = MAXN - 1; i >= 1; i--)
    inv[i] = (i + 1) * inv[i + 1] % MO;
}
LL n, A, B, k, x, y, d, tot;
LL Solve(int i, int j) {
  return inv[i] * inv[j] % MO * inv[n - i] % MO * inv[n - j] % MO;
}
int main() {
  Init();
  scanf("%lld%lld%lld%lld", &n, &A, &B, &k);
  LL ans = 0;
  for (int i = 0; i <= n && i * A <= k; i++) {
    LL L = k - A * i;
    if (L % B == 0 && L / B <= n) {
      ans += Solve(i, L / B);
      if (ans >= MO)
        ans -= MO;
    }
  }
  printf("%lld\n", ans * fac[n] % MO * fac[n] % MO);
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

typedef long long ll;

ll mod = 998244353;

ll modinv(ll a) {
  ll b = mod, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

ll fac[300010];

void modfac() {
  fac[0] = 1;
  for (ll i = 1; i < 300010; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
  }
}

ll modcomb(ll n, ll k) {
  return ((fac[n] * modinv(fac[k])) % mod * modinv(fac[n - k])) % mod;
}

int main() {
  ll n, a, b, k;
  cin >> n >> a >> b >> k;
  modfac();
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    ll x = k - a * i;
    if (x >= 0 && x % b == 0) {
      ll count = (modcomb(n, i) * modcomb(n, x / b)) % mod;
      ans = (ans + count) % mod;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
using namespace std;

typedef long long ll;

ll mod = 998244353;

ll modinv(ll a) {
  ll b = mod, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

ll fac[300010];

void modfac() {
  fac[0] = 1;
  for (ll i = 1; i < 300010; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
  }
}

ll modcomb(ll n, ll k) {
  return ((fac[n] * modinv(fac[k])) % mod * modinv(fac[n - k])) % mod;
}

int main() {
  ll n, a, b, k;
  cin >> n >> a >> b >> k;
  modfac();
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    ll x = k - a * i;
    if (x >= 0 && x % b == 0 && x / b <= n) {
      ll count = (modcomb(n, i) * modcomb(n, x / b)) % mod;
      ans = (ans + count) % mod;
    }
  }
  cout << ans << endl;
}
Problem p03332 (C++)

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

#define fo(i, s, t) for (int i = s; i <= t; ++i)
#define fd(i, s, t) for (int i = s; i >= t; --i)
#define bf(i, s) for (int i = head[s]; i; i = e[i].next)
#define mp make_pair
#define fi first
#define se second
#define pii pair<int, int>
#define pb push_back
#define VI vector<int>
#define sf scanf
#define pf printf
#define fp freopen
#define SZ(x) ((int)(x).size())
#ifdef MPS
#define D(x...) printf(x)
#else
#define D(x...)
#endif
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const db Inf = 1e20;
const db eps = 1e-9;

void gmax(int &a, int b) { a = (a > b ? a : b); }
void gmin(int &a, int b) { a = (a < b ? a : b); }

const int p = 998244353;
const int maxn = 300050;

ll n, a, b, k, ans, fac[maxn], inv[maxn];

ll C(ll n, ll m) { return (fac[n] * inv[m] % p) * inv[n - m] % p; }
ll qpow(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % p;
    b >>= 1;
    a = a * a % p;
  }
  return ans;
}
int main() {
#ifdef MPS
  fp("1.in", "r", stdin);
  fp("1.out", "w", stdout);
#endif
  cin >> n >> a >> b >> k;
  fac[0] = 1;
  fo(i, 1, n) fac[i] = fac[i - 1] * i % p;
  inv[n] = qpow(fac[n], p - 2);
  fd(i, n - 1, 0) inv[i] = inv[i + 1] * (i + 1) % p;
  for (ll n1 = 0; n1 <= n; n1++) {
    if (k - n1 * a < 0)
      continue;
    if ((k - n1 * a) % b != 0)
      continue;
    ll n2 = (k - n1 * a) / b;
    ans = (ans + C(n, n1) * C(n, n2)) % p;
  }
  cout << ans << endl;
  return 0;
}

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

#define fo(i, s, t) for (int i = s; i <= t; ++i)
#define fd(i, s, t) for (int i = s; i >= t; --i)
#define bf(i, s) for (int i = head[s]; i; i = e[i].next)
#define mp make_pair
#define fi first
#define se second
#define pii pair<int, int>
#define pb push_back
#define VI vector<int>
#define sf scanf
#define pf printf
#define fp freopen
#define SZ(x) ((int)(x).size())
#ifdef MPS
#define D(x...) printf(x)
#else
#define D(x...)
#endif
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const db Inf = 1e20;
const db eps = 1e-9;

void gmax(int &a, int b) { a = (a > b ? a : b); }
void gmin(int &a, int b) { a = (a < b ? a : b); }

const int p = 998244353;
const int maxn = 300050;

ll n, a, b, k, ans, fac[maxn], inv[maxn];

ll C(ll n, ll m) { return (fac[n] * inv[m] % p) * inv[n - m] % p; }
ll qpow(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % p;
    b >>= 1;
    a = a * a % p;
  }
  return ans;
}
int main() {
#ifdef MPS
  fp("1.in", "r", stdin);
  fp("1.out", "w", stdout);
#endif
  cin >> n >> a >> b >> k;
  fac[0] = 1;
  fo(i, 1, n) fac[i] = fac[i - 1] * i % p;
  inv[n] = qpow(fac[n], p - 2);
  fd(i, n - 1, 0) inv[i] = inv[i + 1] * (i + 1) % p;
  for (ll n1 = 0; n1 <= n; n1++) {
    if (k - n1 * a < 0)
      continue;
    if ((k - n1 * a) % b != 0)
      continue;
    ll n2 = (k - n1 * a) / b;
    if (n1 <= n && n2 <= n)
      ans = (ans + C(n, n1) * C(n, n2)) % p;
  }
  cout << ans << endl;
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100, MOD = 998244353;
typedef long long ll;
ll n, a, b, k, ans, fac[N];
ll power(int a, int b) {
  if (!b)
    return 1;
  if (b == 1)
    return a;
  ll p = power(a, b / 2);
  p = (p * p) % MOD;
  if (b % 2)
    p = (p * a) % MOD;
  return p % MOD;
}
ll choose(int a, int b) {
  if (a > b)
    return 0;
  ll A = fac[b], B = (fac[a] * fac[b - a]) % MOD;
  return (A * power(B, MOD - 2)) % MOD;
}
int main() {
  fac[0] = 1;
  for (int i = 1; i < N; i++)
    fac[i] = (fac[i - 1] * i) % MOD;
  cin >> n >> a >> b >> k;
  for (int i = 0; i <= n; i++) {
    ll remain = k - (i * a);
    if (remain >= 0 && remain % b == 0) {
      ll add = (choose(i, n) * choose(remain / b, n)) % MOD;
      ans = (ans + add) % MOD;
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 100, MOD = 998244353;
typedef long long ll;
ll n, a, b, k, ans, fac[N];
ll power(int a, int b) {
  if (!b)
    return 1;
  if (b == 1)
    return a;
  ll p = power(a, b / 2);
  p = (p * p) % MOD;
  if (b % 2)
    p = (p * a) % MOD;
  return p % MOD;
}
ll choose(int a, int b) {
  if (a > b)
    return 0;
  ll A = fac[b], B = (fac[a] * fac[b - a]) % MOD;
  return (A * power(B, MOD - 2)) % MOD;
}
int main() {
  fac[0] = 1;
  for (int i = 1; i < N; i++)
    fac[i] = (fac[i - 1] * i) % MOD;
  cin >> n >> a >> b >> k;
  for (int i = 0; i <= n; i++) {
    ll remain = k - (i * a);
    if (remain >= 0 && remain % b == 0) {
      ll add = (choose(i, n) * choose(remain / b, n)) % MOD;
      ans = (ans + add) % MOD;
    }
  }
  cout << ans;
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <stdint.h>

const int64_t MOD = 998244353;

int64_t fact[100001] = {1};

int64_t modexp(int64_t base, int64_t exp) {
  int64_t ac = 1;
  for (; exp > 0; exp >>= 1) {
    if (exp & 1)
      ac = ac * base % MOD;
    base = base * base % MOD;
  }
  return ac;
}

int64_t inverse(int64_t x) { return modexp(x % MOD, MOD - 2); }

int main() {
  int64_t N, A, B, K;
  scanf("%ld %ld %ld %ld", &N, &A, &B, &K);
  for (int64_t i = 1; i <= N; i++) {
    fact[i] = fact[i - 1] * i % MOD;
    // printf("%ld!=%ld\n",i,fact[i]);
  }
  int64_t j = N;
  int64_t total = 0;
  for (int64_t i = 0; i <= N; i++) {
    while (A * i + B * j > K) {
      j--;
    }
    if (j < 0)
      break;
    if (A * i + B * j == K) {
      /*
      printf("ADD %ld,%ld
      =>%ld*%ld\n",i,j,fact[N]*inverse(fact[i]*fact[N-i])%MOD,
             fact[N]%MOD*inverse(fact[j]*fact[N-j])%MOD);
      */
      total =
          (total + fact[N] * inverse(fact[i] * fact[N - i]) % MOD * fact[N] %
                       MOD * inverse(fact[j] * fact[N - j]) % MOD) %
          MOD;
    }
  }
  printf("%ld\n", total);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <stdint.h>

const int64_t MOD = 998244353;

int64_t fact[300001] = {1};

int64_t modexp(int64_t base, int64_t exp) {
  int64_t ac = 1;
  for (; exp > 0; exp >>= 1) {
    if (exp & 1)
      ac = ac * base % MOD;
    base = base * base % MOD;
  }
  return ac;
}

int64_t inverse(int64_t x) { return modexp(x % MOD, MOD - 2); }

int main() {
  int64_t N, A, B, K;
  scanf("%ld %ld %ld %ld", &N, &A, &B, &K);
  for (int64_t i = 1; i <= N; i++) {
    fact[i] = fact[i - 1] * i % MOD;
    // printf("%ld!=%ld\n",i,fact[i]);
  }
  int64_t j = N;
  int64_t total = 0;
  for (int64_t i = 0; i <= N; i++) {
    while (A * i + B * j > K) {
      j--;
    }
    if (j < 0)
      break;
    if (A * i + B * j == K) {
      /*
      printf("ADD %ld,%ld
      =>%ld*%ld\n",i,j,fact[N]*inverse(fact[i]*fact[N-i])%MOD,
             fact[N]%MOD*inverse(fact[j]*fact[N-j])%MOD);
      */
      total =
          (total + fact[N] * inverse(fact[i] * fact[N - i]) % MOD * fact[N] %
                       MOD * inverse(fact[j] * fact[N - j]) % MOD) %
          MOD;
    }
  }
  printf("%ld\n", total);
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
/* ***********************************************
author        :dasinlsb
created time  :2018/6/3 20:45:40
file name     :c:\dasin\duipai.cpp
************************************************ */

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
const int N = 300005;
const int mod = 998244353;
int n, a, b, fac[N], inv[N];
ll kk;
int c(int x, int y) {
  // printf("c %d %d : %d %d %d,%d
  // \n",x,y,fac[x],inv[y],inv[x-y],(ll)fac[x]*inv[x-y]%mod);
  return (ll)fac[x] * inv[y] % mod * inv[x - y] % mod;
}
int main() {
  // freopen("c:/dasin/aa.in","r",stdin);
  // freopen("c:/dasin/mine.out","w",stdout);
  int i, j;
  cin >> n >> a >> b >> kk;
  for (fac[0] = i = 1; i <= n; ++i)
    fac[i] = (ll)fac[i - 1] * i % mod;
  for (inv[0] = inv[1] = 1, i = 2; i <= n; ++i)
    inv[i] = mod - (ll)mod / i * inv[mod % i] % mod;
  for (i = 1; i <= n; ++i)
    inv[i] = (ll)inv[i] * inv[i - 1] % mod;
  int ans = 0;
  for (i = 0; i <= n; ++i) {
    if ((kk - a * i) % b)
      continue;
    ll bs = (kk - a * i) / b;
    if (bs > n)
      continue;
    // printf("pro %d %d\n",i,bs);
    ans = (ans + (ll)c(n, i) * c(n, bs)) % mod;
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
/* ***********************************************
author        :dasinlsb
created time  :2018/6/3 20:45:40
file name     :c:\dasin\duipai.cpp
************************************************ */

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
const int N = 300005;
const int mod = 998244353;
int n, a, b, fac[N], inv[N];
ll kk;
int c(int x, int y) {
  // printf("c %d %d : %d %d %d,%d
  // \n",x,y,fac[x],inv[y],inv[x-y],(ll)fac[x]*inv[x-y]%mod);
  return (ll)fac[x] * inv[y] % mod * inv[x - y] % mod;
}
int main() {
  // freopen("c:/dasin/aa.in","r",stdin);
  // freopen("c:/dasin/mine.out","w",stdout);
  int i, j;
  cin >> n >> a >> b >> kk;
  for (fac[0] = i = 1; i <= n; ++i)
    fac[i] = (ll)fac[i - 1] * i % mod;
  for (inv[0] = inv[1] = 1, i = 2; i <= n; ++i)
    inv[i] = mod - (ll)mod / i * inv[mod % i] % mod;
  for (i = 1; i <= n; ++i)
    inv[i] = (ll)inv[i] * inv[i - 1] % mod;
  int ans = 0;
  for (i = 0; i <= n; ++i) {
    if ((kk - a * i) % b)
      continue;
    ll bs = (kk - a * i) / b;
    if (bs < 0 || bs > n)
      continue;
    // printf("pro %d %d\n",i,bs);
    ans = (ans + (ll)c(n, i) * c(n, bs)) % mod;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#define ll long long
using namespace std;
ll p = 998244353;
ll fac[300010], de[300010], inv[300010];
ll C(ll n, ll m) {
  if (m == 0)
    return 1;
  return fac[n] * de[m] % p * de[n - m] % p;
}
int main() {
  ll n, a, b, k, ans = 0;
  cin >> n >> a >> b >> k;
  if (k == 0) {
    cout << 1;
    return 0;
  }
  if (1ll * (a + b) * n < k) {
    cout << 0 << endl;
    return 0;
  }
  fac[0] = 1, inv[1] = 1, de[0] = 1;
  for (int i = 1; i <= 300000; i++)
    fac[i] = fac[i - 1] * i % p;
  for (int i = 2; i <= 300000; i++)
    inv[i] = (long long)(p - p / i) * inv[p % i] % p;
  for (int i = 1; i <= 300000; i++)
    de[i] = de[i - 1] * inv[i] % p;
  for (ll x = 0; x <= n; x++) {
    ll y = (k - a * x) / b;
    if (y * b + a * x != k)
      continue;
    if (y < 0)
      continue;
    ans += C(n, x) * C(n, y) % p;
    ans %= p;
  }
  cout << ans % p;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#define ll long long
using namespace std;
ll p = 998244353;
ll fac[300010], de[300010], inv[300010];
ll C(ll n, ll m) {
  if (m == 0)
    return 1;
  return fac[n] * de[m] % p * de[n - m] % p;
}
int main() {
  ll n, a, b, k, ans = 0;
  cin >> n >> a >> b >> k;
  if (k == 0) {
    cout << 1;
    return 0;
  }
  if (1ll * (a + b) * n < k) {
    cout << 0 << endl;
    return 0;
  }
  fac[0] = 1, inv[1] = 1, de[0] = 1;
  for (int i = 1; i <= 300000; i++)
    fac[i] = fac[i - 1] * i % p;
  for (int i = 2; i <= 300000; i++)
    inv[i] = (long long)(p - p / i) * inv[p % i] % p;
  for (int i = 1; i <= 300000; i++)
    de[i] = de[i - 1] * inv[i] % p;
  for (ll x = 0; x <= n; x++) {
    ll y = (k - a * x) / b;
    if (y * b + a * x != k)
      continue;
    if (y < 0 || y > n)
      continue;
    ans += C(n, x) * C(n, y) % p;
    ans %= p;
  }
  cout << ans % p;
  return 0;
}
Problem p03332 (C++)

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

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MOD = 998244353;

ll modpow(ll b, ll e, ll m) {
  ll r = 1;
  while (e) {
    if (e & 1) {
      r *= b;
      r %= m;
    }
    b *= b;
    b %= m;
    e /= 2;
  }
  return r;
}

ll fact[1000000];
ll invfact[1000000];

ll combo(int n, int k) {
  ll ret = fact[n];
  ret *= invfact[k];
  ret %= MOD;
  ret *= invfact[n - k];
  ret %= MOD;
  return ret;
}

int main() {
  ll n, a, b, k;
  scanf("%lld%lld%lld%lld", &n, &a, &b, &k);
  fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= MOD;
  }
  invfact[n] = modpow(fact[n], MOD - 2, MOD);
  for (int i = n - 1; i >= 0; i--) {
    invfact[i] = invfact[i + 1] * (i + 1);
    invfact[i] %= MOD;
  }
  ll ret = 0;
  for (int i = 0; i <= n; i++) {
    ll left = k - i * a;
    if (left % b || left / b > n)
      continue;
    int j = left / b;
    ret += combo(n, i) * combo(n, j);
    ret %= MOD;
  }
  printf("%lld\n", ret);
}

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

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MOD = 998244353;

ll modpow(ll b, ll e, ll m) {
  ll r = 1;
  while (e) {
    if (e & 1) {
      r *= b;
      r %= m;
    }
    b *= b;
    b %= m;
    e /= 2;
  }
  return r;
}

ll fact[1000000];
ll invfact[1000000];

ll combo(int n, int k) {
  ll ret = fact[n];
  ret *= invfact[k];
  ret %= MOD;
  ret *= invfact[n - k];
  ret %= MOD;
  return ret;
}

int main() {
  ll n, a, b, k;
  scanf("%lld%lld%lld%lld", &n, &a, &b, &k);
  fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= MOD;
  }
  invfact[n] = modpow(fact[n], MOD - 2, MOD);
  for (int i = n - 1; i >= 0; i--) {
    invfact[i] = invfact[i + 1] * (i + 1);
    invfact[i] %= MOD;
  }
  ll ret = 0;
  for (int i = 0; i <= n; i++) {
    ll left = k - i * a;
    if (left < 0 || left % b || left / b > n)
      continue;
    int j = left / b;
    ret += combo(n, i) * combo(n, j);
    ret %= MOD;
  }
  printf("%lld\n", ret);
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
// 03, 06, 2018, 09:25:45 Rafaelcs cpp
#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> pii;
typedef long double ld;
typedef long long ll;

const int N = int(3e5 + 10);
const ll mod = 998244353LL;
ll x, y;
ll fat[N];
ll d;

void gen() {
  fat[0] = 1LL;

  for (ll i = 1; i < N; i++) {
    fat[i] = fat[i - 1] * i;
    fat[i] %= mod;
  }
}

// store x, y, and d as global variables
void extendedEuclid(ll a, ll b) {
  if (b == 0) {
    x = 1;
    y = 0;
    d = a;
    return;
  }
  // base case
  extendedEuclid(b, a % b);
  // similar as the original gcd
  ll x1 = y;
  ll y1 = x - (a / b) * y;
  x = x1;
  y = y1;
}

ll MOD(ll n) { return ((n % mod) + mod) % mod; }

ll inverso(ll n) {
  extendedEuclid(n, mod);

  return MOD(x);
}

ll c(ll n, ll k) {
  ll a = inverso(fat[n - k]);
  ll b = inverso(fat[k]);

  return MOD(a * MOD(b * fat[n]));
}

int main() {
  gen();
  ll n, a, b, k;

  cin >> n >> a >> b >> k;

  ll sum = 0LL;

  for (ll ca = 0LL; ca <= n; ca++) {
    ll cb = k - ca * a;

    if (cb >= 0LL && cb % b == 0LL) {
      cb = cb / b;

      sum += c(n, cb) * c(n, ca);
      sum %= mod;
    }
  }

  cout << sum << endl;

  return 0;
}

Fixed submission:
// 03, 06, 2018, 09:25:45 Rafaelcs cpp
#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> pii;
typedef long double ld;
typedef long long ll;

const int N = int(3e5 + 10);
const ll mod = 998244353LL;
ll x, y;
ll fat[N];
ll d;

void gen() {
  fat[0] = 1LL;

  for (ll i = 1; i < N; i++) {
    fat[i] = fat[i - 1] * i;
    fat[i] %= mod;
  }
}

// store x, y, and d as global variables
void extendedEuclid(ll a, ll b) {
  if (b == 0) {
    x = 1;
    y = 0;
    d = a;
    return;
  }
  // base case
  extendedEuclid(b, a % b);
  // similar as the original gcd
  ll x1 = y;
  ll y1 = x - (a / b) * y;
  x = x1;
  y = y1;
}

ll MOD(ll n) { return ((n % mod) + mod) % mod; }

ll inverso(ll n) {
  extendedEuclid(n, mod);

  return MOD(x);
}

ll c(ll n, ll k) {
  ll a = inverso(fat[n - k]);
  ll b = inverso(fat[k]);

  return MOD(a * MOD(b * fat[n]));
}

int main() {
  gen();
  ll n, a, b, k;

  cin >> n >> a >> b >> k;

  ll sum = 0LL;

  for (ll ca = 0LL; ca <= n; ca++) {
    ll cb = k - ca * a;

    if (cb >= 0LL && cb % b == 0LL) {
      cb = cb / b;

      if (cb > n || ca > n) {
        continue;
      }

      sum += c(n, cb) * c(n, ca);
      sum %= mod;
    }
  }

  cout << sum << endl;

  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = (x); i <= (y); i++)
#define per(i, x, y) for (int i = (x); i >= (y); i--)
#define ll long long
#define inf 1000000001
using namespace std;
#define N 300005
#define mod 998244353
int n, a, b, fac[N], inv[N], ans;
ll K;
int ksm(int x, int p) {
  int ret = 1;
  for (; p; p >>= 1, x = (ll)x * x % mod)
    if (p & 1)
      ret = (ll)ret * x % mod;
  return ret;
}
int getinv(int x) { return ksm(x, mod - 2); }
void init(int n) {
  fac[0] = 1;
  rep(i, 1, n) fac[i] = (ll)fac[i - 1] * i % mod;
  inv[n] = getinv(fac[n]);
  per(i, n - 1, 0) inv[i] = (ll)inv[i + 1] * (i + 1) % mod;
}
int C(int n, int m) { return (ll)fac[n] * inv[m] % mod * inv[n - m] % mod; }
int main() {
  scanf("%d%d%d%lld", &n, &a, &b, &K);
  init(n);
  rep(i, 0, n) {
    ll j = K - (ll)a * i;
    if (j < 0 || j % b)
      continue;
    j /= b;
    ans = (ans + (ll)C(n, i) * C(n, j) % mod) % mod;
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = (x); i <= (y); i++)
#define per(i, x, y) for (int i = (x); i >= (y); i--)
#define ll long long
#define inf 1000000001
using namespace std;
#define N 300005
#define mod 998244353
int n, a, b, fac[N], inv[N], ans;
ll K;
int ksm(int x, int p) {
  int ret = 1;
  for (; p; p >>= 1, x = (ll)x * x % mod)
    if (p & 1)
      ret = (ll)ret * x % mod;
  return ret;
}
int getinv(int x) { return ksm(x, mod - 2); }
void init(int n) {
  fac[0] = 1;
  rep(i, 1, n) fac[i] = (ll)fac[i - 1] * i % mod;
  inv[n] = getinv(fac[n]);
  per(i, n - 1, 0) inv[i] = (ll)inv[i + 1] * (i + 1) % mod;
}
int C(int n, int m) { return (ll)fac[n] * inv[m] % mod * inv[n - m] % mod; }
int main() {
  scanf("%d%d%d%lld", &n, &a, &b, &K);
  init(n);
  rep(i, 0, n) {
    ll j = K - (ll)a * i;
    if (j < 0 || j % b)
      continue;
    j /= b;
    if (j <= n)
      ans = (ans + (ll)C(n, i) * C(n, j) % mod) % mod;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX_N 300005
#define MOD 998244353
#define int long long

using namespace std;

int n, a, b, K, ans = 0;
int f[MAX_N];
int inf[MAX_N];

void exgcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return;
  }
  exgcd(b, a % b, y, x);
  y -= (a / b) * x;
}

int inv(int a) {
  int x, y;
  exgcd(a, MOD, x, y);
  return (x % MOD + MOD) % MOD;
}

int c(int n, int m) { return f[n] * inf[m] % MOD * inf[n - m] % MOD; }

signed main() {
  scanf("%lld%lld%lld%lld", &n, &a, &b, &K);
  f[0] = 1;
  for (int i = 1; i <= n; i++)
    f[i] = f[i - 1] * i % MOD;
  inf[n] = inv(f[n]), inf[0] = 1;
  for (int i = n - 1; i >= 1; i--)
    inf[i] = inf[i + 1] * (i + 1) % MOD;
  for (int i = 0; i <= n; i++) {
    if (!((K - a * i) % b)) {
      int j = (K - a * i) / b;
      if (j <= n)
        ans = (ans + c(n, i) * c(n, j) % MOD) % MOD;
    }
  }
  printf("%lld\n", ans);
}


Fixed submission:
#include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX_N 300005
#define MOD 998244353
#define int long long

using namespace std;

int n, a, b, K, ans = 0;
int f[MAX_N];
int inf[MAX_N];

void exgcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return;
  }
  exgcd(b, a % b, y, x);
  y -= (a / b) * x;
}

int inv(int a) {
  int x, y;
  exgcd(a, MOD, x, y);
  return (x % MOD + MOD) % MOD;
}

int c(int n, int m) { return f[n] * inf[m] % MOD * inf[n - m] % MOD; }

signed main() {
  scanf("%lld%lld%lld%lld", &n, &a, &b, &K);
  f[0] = 1;
  for (int i = 1; i <= n; i++)
    f[i] = f[i - 1] * i % MOD;
  inf[n] = inv(f[n]), inf[0] = 1;
  for (int i = n - 1; i >= 1; i--)
    inf[i] = inf[i + 1] * (i + 1) % MOD;
  for (int i = 0; i <= n; i++) {
    if (!((K - a * i) % b)) {
      int j = (K - a * i) / b;
      if (0 <= j && j <= n)
        ans = (ans + c(n, i) * c(n, j) % MOD) % MOD;
    }
  }
  printf("%lld\n", ans);
}