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

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

#define DIV 998244353

long long N, A, B, K;

long long fact[300005];

long long modpow(long long ori, long long po) {
  long long res = 1;
  while (po > 0) {
    if (po & 1) {
      res *= ori;
      res %= DIV;
    }
    ori *= ori;
    ori %= DIV;
    po >>= 1;
  }
  return res;
}

long long comb(long long m, long long n) {
  long long ret = fact[m];
  ret *= modpow(fact[m - n], DIV - 2);
  ret %= DIV;
  ret *= modpow(fact[n], DIV - 2);
  ret %= DIV;
  return ret;
}

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

  fact[0] = 1;
  for (long long i = 1; i <= N; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= DIV;
  }

  long long ans = 0;
  for (long long i = 0; i <= N; i++) {
    long long rest = K - A * i;
    if (rest % B != 0)
      continue;
    long long j = rest / B;
    if (j > N)
      continue;
    ans += (comb(N, i) * comb(N, j)) % DIV;
    ans %= DIV;
  }
  cout << ans << endl;
}

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

#define DIV 998244353

long long N, A, B, K;

long long fact[300005];

long long modpow(long long ori, long long po) {
  long long res = 1;
  while (po > 0) {
    if (po & 1) {
      res *= ori;
      res %= DIV;
    }
    ori *= ori;
    ori %= DIV;
    po >>= 1;
  }
  return res;
}

long long comb(long long m, long long n) {
  long long ret = fact[m];
  ret *= modpow(fact[m - n], DIV - 2);
  ret %= DIV;
  ret *= modpow(fact[n], DIV - 2);
  ret %= DIV;
  return ret;
}

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

  fact[0] = 1;
  for (long long i = 1; i <= N; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= DIV;
  }

  long long ans = 0;
  for (long long i = 0; i <= N; i++) {
    long long rest = K - A * i;
    if (rest % B != 0)
      continue;
    long long j = rest / B;
    if (j > N || j < 0)
      continue;
    ans += (comb(N, i) * comb(N, j)) % DIV;
    ans %= DIV;
  }
  cout << ans << endl;
}
Problem p03332 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
#define FOR(i, begin, end)                                                     \
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
constexpr int MOD = 998244353;
constexpr int INF = 2147483647;
void yes(bool expr) { cout << (expr ? "Yes" : "No") << "\n"; }
lint power(lint x, lint n, lint mod) {
  lint ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= mod;
    (x *= x) %= mod;
    n >>= 1;
  }
  return ret;
}
lint ncr(lint n, lint r, lint mod, vector<lint> fact) {
  if (n < 0 || r < 0 || n - r < 0)
    return 0;
  return fact[n] * power(fact[r], mod - 2, mod) % mod *
         power(fact[n - r], mod - 2, mod) % mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  lint N, A, B, K;
  cin >> N >> A >> B >> K;
  vector<lint> fact(N + 1);
  fact[0] = 1;
  FOR(i, 1, N + 1) fact[i] = fact[i - 1] * i % MOD;
  lint ans = 0;
  REP(i, N + 1) {
    lint p = i;
    if (K - p * A < 0 || (K - p * A) % B != 0)
      continue;
    lint q = (K - p * A) / B;
    if (q < 0 || q > N)
      continue;
    ans = (ans + ncr(N, p, MOD, fact) * ncr(N, q, MOD, fact) % MOD) % MOD;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
#define FOR(i, begin, end)                                                     \
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
constexpr int MOD = 998244353;
constexpr int INF = 2147483647;
void yes(bool expr) { cout << (expr ? "Yes" : "No") << "\n"; }
lint power(lint x, lint n, lint mod) {
  lint ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= mod;
    (x *= x) %= mod;
    n >>= 1;
  }
  return ret;
}
lint ncr(lint n, lint r, lint mod, vector<lint> &fact) {
  if (n < 0 || r < 0 || n - r < 0)
    return 0;
  return fact[n] * power(fact[r], mod - 2, mod) % mod *
         power(fact[n - r], mod - 2, mod) % mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  lint N, A, B, K;
  cin >> N >> A >> B >> K;
  vector<lint> fact(N + 1);
  fact[0] = 1;
  FOR(i, 1, N + 1) fact[i] = fact[i - 1] * i % MOD;
  lint ans = 0;
  REP(i, N + 1) {
    lint p = i;
    if (K - p * A < 0 || (K - p * A) % B != 0)
      continue;
    lint q = (K - p * A) / B;
    if (q < 0 || q > N)
      continue;
    ans = (ans + ncr(N, p, MOD, fact) * ncr(N, q, MOD, fact) % MOD) % MOD;
  }
  cout << ans << endl;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sqr(x) ((x) * (x))
#define mp make_pair
#define uint unsigned
#define PI pair<int, int>
#define int long long
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
inline int read() {
  int x = 0;
  char ch = gc();
  bool positive = 1;
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      positive = 0;
  for (; isdigit(ch); ch = gc())
    x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(int a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10)
    write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(int a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  write(a);
  puts("");
}
inline int rnd(int x) { return (rand() << 16 | rand()) % x; }
const int N = 200005, mod = 998244353;
int n, a, b, k, ans, c[N];
inline int ksm(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
  }
  return ans;
}
signed main() {
  n = read();
  a = read();
  b = read();
  cin >> k;
  c[0] = 1;
  for (int i = 1; i <= n; i++)
    c[i] = c[i - 1] * (n - i + 1) % mod * ksm(i, mod - 2) % mod;
  for (int i = 0; i <= n; i++)
    if ((k - i * a) % b == 0) {
      int t = (k - i * a) / b;
      if (t >= 0 && t <= n)
        ans = (ans + c[i] * c[t]) % mod;
    }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sqr(x) ((x) * (x))
#define mp make_pair
#define uint unsigned
#define PI pair<int, int>
#define int long long
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
inline int read() {
  int x = 0;
  char ch = gc();
  bool positive = 1;
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      positive = 0;
  for (; isdigit(ch); ch = gc())
    x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(int a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10)
    write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(int a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  write(a);
  puts("");
}
inline int rnd(int x) { return (rand() << 16 | rand()) % x; }
const int N = 300005, mod = 998244353;
int n, a, b, k, ans, c[N];
inline int ksm(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
  }
  return ans;
}
signed main() {
  n = read();
  a = read();
  b = read();
  cin >> k;
  c[0] = 1;
  for (int i = 1; i <= n; i++)
    c[i] = c[i - 1] * (n - i + 1) % mod * ksm(i, mod - 2) % mod;
  for (int i = 0; i <= n; i++)
    if ((k - i * a) % b == 0) {
      int t = (k - i * a) / b;
      if (t >= 0 && t <= n)
        ans = (ans + c[i] * c[t]) % mod;
    }
  cout << ans << endl;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<double, ll> pdl;
#define F first
#define S second
#define MK make_pair
const ll E = 1e18 + 7;
const ll MOD = 1000000007;
const ll mod = 998244353;

ll mod_pow(ll a, ll x) {
  ll ans = 1;
  for (int i = 0; i < 63; i++) {
    if (x >> i & 1) {
      ans *= a;
      ans %= mod;
    }
    a *= a;
    a %= mod;
  }
  return ans;
}

ll R(ll a) { return mod_pow(a, mod - 2); }

vector<ll> F = {1, 1};

ll c(ll n, ll k) {
  if (n == 0 || k == 0) {
    return 1;
  }
  return F[n] * R(F[n - k]) % mod * R(F[k]) % mod;
}

int main() {
  ll n, a, b, k;
  cin >> n >> a >> b >> k;
  for (ll i = 2; i < 1e6; i++) {
    F.push_back(F[i - 1] * i % mod);
  }
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    ll K = k - i * a;
    if (K < 0) {
      break;
    }
    if (K % b == 0) {
      ans += c(n, i) * c(n, K / b) % mod;
      ans %= mod;
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<double, ll> pdl;
#define F first
#define S second
#define MK make_pair
const ll E = 1e18 + 7;
const ll MOD = 1000000007;
const ll mod = 998244353;

ll mod_pow(ll a, ll x) {
  ll ans = 1;
  for (int i = 0; i < 63; i++) {
    if (x >> i & 1) {
      ans *= a;
      ans %= mod;
    }
    a *= a;
    a %= mod;
  }
  return ans;
}

ll R(ll a) { return mod_pow(a, mod - 2); }

vector<ll> F = {1, 1};

ll c(ll n, ll k) {
  if (n == 0 || k == 0) {
    return 1;
  }
  if (n < k) {
    return 0;
  }
  return F[n] * R(F[n - k]) % mod * R(F[k]) % mod;
}

int main() {
  ll n, a, b, k;
  cin >> n >> a >> b >> k;
  for (ll i = 2; i < 1e6; i++) {
    F.push_back(F[i - 1] * i % mod);
  }
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    ll K = k - i * a;
    if (K < 0) {
      break;
    }
    if (K % b == 0) {
      ans += c(n, i) * c(n, K / b) % mod;
      ans %= mod;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03332 (C++)

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

using namespace std;
const int maxn = 3e5 + 10;
const long long mod = 998244353;

int n;
long long a, b, k;
long long fac[maxn];

long long pot(long long a, long long b) {
  if (b == 0)
    return 1;
  else if (b & 1)
    return (a * pot(a, b - 1)) % mod;
  else {
    long long out = pot(a, b / 2);
    return (out * out) % mod;
  }
}

long long inv(long long a) { return pot(a, mod - 2); }

long long divi(long long a, long long b) { return (a * inv(b)) % mod; }

long long choose(int n, int k) {
  // printf("choose: %lld %lld %lld\n", fac[n], fac[n - k], fac[k]);
  // if (k > n) return 1;
  return divi(fac[n], (fac[k] * fac[n - k]) % 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;

  long long sol = 0;
  for (int i = 0; i <= n; i++) {
    // printf("%lld\n", i);
    if (k >= a * i && (k - a * i) % b == 0) {
      long long j = (k - a * i) / b;
      // if (j > n) continue;
      sol += (choose(n, i) * choose(n, (int)j)) % mod, sol %= mod;
      // printf("%d %d -> %lld\n", i, j, (choose(n, i) * choose(n, j)) % mod);
    }
  }
  printf("%lld", sol);
  return 0;
}


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

using namespace std;
const int maxn = 3e5 + 10;
const long long mod = 998244353;

int n;
long long a, b, k;
long long fac[maxn];

long long pot(long long a, long long b) {
  if (b == 0)
    return 1;
  else if (b & 1)
    return (a * pot(a, b - 1)) % mod;
  else {
    long long out = pot(a, b / 2);
    return (out * out) % mod;
  }
}

long long inv(long long a) { return pot(a, mod - 2); }

long long divi(long long a, long long b) { return (a * inv(b)) % mod; }

long long choose(int n, int k) {
  // printf("choose: %lld %lld %lld\n", fac[n], fac[n - k], fac[k]);
  // if (k > n) return 1;
  return divi(fac[n], (fac[k] * fac[n - k]) % 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;

  long long sol = 0;
  for (int i = 0; i <= n; i++) {
    // printf("%lld\n", i);
    if (k >= a * i && (k - a * i) % b == 0) {
      long long j = (k - a * i) / b;
      if (j > n)
        continue;
      sol += (choose(n, i) * choose(n, (int)j)) % mod, sol %= mod;
      // printf("%d %d -> %lld\n", i, j, (choose(n, i) * choose(n, j)) % mod);
    }
  }
  printf("%lld", sol);
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int>>
#define VLL vector<ll>
#define PII pair<int, int>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
using namespace std;

#define int ll
const int mod = 998244353;
const int N = 1e5 + 7;

int n, a, b, k, ans;

int fact[N];
int inv[N];

int fpow(int a, int b) {
  int res = 1;

  while (b) {
    if (b & 1)
      res = res * a % mod;

    a = a * a % mod;
    b /= 2;
  }

  return res;
}

int kom(int n, int k) {
  if (k > n)
    return 0;

  if (k < 0)
    return 0;

  return fact[n] * inv[n - k] % mod * inv[k] % mod;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  inv[0] = fact[0] = 1;

  for (int i = 1; i < N; ++i) {
    fact[i] = i * fact[i - 1] % mod;
    inv[i] = fpow(fact[i], mod - 2);
  }

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

  for (int i = 0; i <= n; ++i) {
    int zos = k - a * i;

    if (zos % b == 0) {
      ans += kom(n, i) * kom(n, zos / b) % mod;
      ans %= mod;
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int>>
#define VLL vector<ll>
#define PII pair<int, int>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
using namespace std;

#define int ll
const int mod = 998244353;
const int N = 3e5 + 7;

int n, a, b, k, ans;

int fact[N];
int inv[N];

int fpow(int a, int b) {
  int res = 1;

  while (b) {
    if (b & 1)
      res = res * a % mod;

    a = a * a % mod;
    b /= 2;
  }

  return res;
}

int kom(int n, int k) {
  if (k > n)
    return 0;

  if (k < 0)
    return 0;

  return fact[n] * inv[n - k] % mod * inv[k] % mod;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  inv[0] = fact[0] = 1;

  for (int i = 1; i < N; ++i) {
    fact[i] = i * fact[i - 1] % mod;
    inv[i] = fpow(fact[i], mod - 2);
  }

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

  for (int i = 0; i <= n; ++i) {
    int zos = k - a * i;

    if (zos % b == 0) {
      ans += kom(n, i) * kom(n, zos / b) % mod;
      ans %= mod;
    }
  }

  cout << ans << 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 MX = 300010, inf = 2e9, MOD = 998244353;

ll n, a, b, k;
ll F[MX];

ll pw(ll a, int e) {
  if (e == 0)
    return 1;
  ll t = pw(a, e / 2);
  t = (t * t) % MOD;
  return (e % 2 == 0) ? t : t * a % MOD;
}

ll inv(ll a) { return pw(a, MOD - 2); }

ll C(int n, int r) { return F[n] * inv(F[n - r]) % MOD * inv(F[r]) % MOD; }

ll count(ll a, ll b) { return (C(n, a) * C(n, b)) % MOD; }

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

  ll ans = 0;
  F[0] = 1;
  for (int i = 1; i <= n; i++)
    F[i] = (F[i - 1] * i) % MOD;

  for (int i = 0; i <= n; i++) {
    ll mb = k - a * i;
    if (mb < 0 || mb % b != 0)
      continue;
    ans = (ans + count(i, mb / b)) % MOD;
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MX = 300010, inf = 2e9, MOD = 998244353;

ll n, a, b, k;
ll F[MX];

ll pw(ll a, int e) {
  if (e == 0)
    return 1;
  ll t = pw(a, e / 2);
  t = (t * t) % MOD;
  return (e % 2 == 0) ? t : t * a % MOD;
}

ll inv(ll a) { return pw(a, MOD - 2); }

ll C(int n, int r) { return F[n] * inv(F[n - r]) % MOD * inv(F[r]) % MOD; }

ll count(ll a, ll b) { return (C(n, a) * C(n, b)) % MOD; }

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

  ll ans = 0;
  F[0] = 1;
  for (int i = 1; i <= n; i++)
    F[i] = (F[i - 1] * i) % MOD;

  for (int i = 0; i <= n; i++) {
    ll mb = k - a * i;
    if (mb < 0 || mb % b != 0 || mb / b > n)
      continue;
    ans = (ans + count(i, mb / b)) % MOD;
  }
  cout << ans;
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define F first
#define S second
#define M 998244353
ll n, a, b, k, ans;
ll o[300005];
ll pw(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x % M;
    x = x * x % M;
    y >>= 1;
  }
  return res;
}
ll cm(ll x, ll y) {
  if (y == 0)
    return 1;
  return o[x] * pw(o[y], M - 2) % M * pw(o[x - y], M - 2) % M;
}
int main(void) {
  cin >> n >> a >> b >> k;
  o[0] = 1;
  for (ll i = 1; i <= n; i++)
    o[i] = o[i - 1] * i % M;
  for (ll i = 0; i <= n; i++) {
    if (a * i <= k && (k - a * i) % b == 0) {
      ll j = (k - a * i) / b;
      ans = (ans + cm(n, i) * cm(n, j) % M) % M;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define F first
#define S second
#define M 998244353
ll n, a, b, k, ans;
ll o[300005];
ll pw(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x % M;
    x = x * x % M;
    y >>= 1;
  }
  return res;
}
ll cm(ll x, ll y) {
  if (y == 0)
    return 1;
  return o[x] * pw(o[y], M - 2) % M * pw(o[x - y], M - 2) % M;
}
int main(void) {
  cin >> n >> a >> b >> k;
  o[0] = 1;
  for (ll i = 1; i <= n; i++)
    o[i] = o[i - 1] * i % M;
  for (ll i = 0; i <= n; i++) {
    if (a * i <= k && (k - a * i) % b == 0) {
      ll j = (k - a * i) / b;
      if (j <= n)
        ans = (ans + cm(n, i) * cm(n, j) % M) % M;
    }
  }
  cout << ans << endl;
}
Problem p03332 (C++)

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

using namespace std;

#define allof(x) (x).begin(), (x).end()

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

const ll mod = 998244353;
const ll MAXN = 3e5 + 5;

ll N, A, B, K;
ll fact[MAXN];

ll qpow(ll x, ll y) {
  ll ans = 1;
  while (y) {
    if (y & 1) {
      ans = ans * x % mod;
    }

    y >>= 1;
    x = x * x % mod;
  }
  return ans;
}

ll nck(ll n, ll k) {
  return fact[n] * qpow(fact[k], mod - 2) % mod * qpow(fact[n - k], mod - 2) %
         mod;
}

int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);

  fact[0] = 1;
  for (int a = 1; a < MAXN; a++) {
    fact[a] = fact[a - 1] * a % mod;
  }

  cin >> N >> A >> B >> K;

  ll ans = 0;
  for (int a = 0; a <= N; a++) {
    ll remain = K - a * A;
    if (remain % B != 0) {
      continue;
    }
    ll numB = remain / B;
    if (numB < 0) {
      continue;
    }

    ll tmp = nck(N, a) * nck(N, numB) % mod;
    ans += tmp;
    ans %= mod;
  }
  cout << ans;
}


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

using namespace std;

#define allof(x) (x).begin(), (x).end()

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

const ll mod = 998244353;
const ll MAXN = 3e5 + 5;

ll N, A, B, K;
ll fact[MAXN];

ll qpow(ll x, ll y) {
  ll ans = 1;
  while (y) {
    if (y & 1) {
      ans = ans * x % mod;
    }

    y >>= 1;
    x = x * x % mod;
  }
  return ans;
}

ll nck(ll n, ll k) {
  return fact[n] * qpow(fact[k], mod - 2) % mod * qpow(fact[n - k], mod - 2) %
         mod;
}

int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);

  fact[0] = 1;
  for (int a = 1; a < MAXN; a++) {
    fact[a] = fact[a - 1] * a % mod;
  }

  cin >> N >> A >> B >> K;

  ll ans = 0;
  for (int a = 0; a <= N; a++) {
    ll remain = K - a * A;
    if (remain % B != 0) {
      continue;
    }
    ll numB = remain / B;
    if (numB < 0 || numB > N) {
      continue;
    }

    ll tmp = nck(N, a) * nck(N, numB) % mod;
    ans += tmp;
    ans %= mod;
  }
  cout << ans;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using ll = int64_t;
using ld = long double;
using ull = uint64_t;
using namespace std;
using namespace __gnu_pbds;

const int MAXN = 200228;

ll fact[MAXN];
ll rfact[MAXN];

const ll MOD = 998244353;

ll pw(ll a, ll b) {
  if (!b) {
    return 1;
  }

  ll v = pw(a, b / 2);
  v = (v * v) % MOD;

  if (b & 1) {
    v = (v * a) % MOD;
  }

  return v;
}

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

int main() {
#ifdef BZ
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout.setf(ios::fixed);
  cout.precision(20);
  int n, a, b;
  ll k;
  cin >> n >> a >> b >> k;
  fact[0] = 1;
  for (int i = 1; i <= n; ++i) {
    fact[i] = fact[i - 1] * i % MOD;
  }

  rfact[n] = pw(fact[n], MOD - 2);
  for (int i = n; i > 0; --i) {
    rfact[i - 1] = rfact[i] * i % MOD;
  }

  ll ans = 0;

  for (ll acnt = 0; acnt <= n; ++acnt) {
    ll bcnt = (k - (acnt * a)) / b;
    if (bcnt <= n && bcnt >= 0 && (a * acnt + b * bcnt == k)) {
      ans = (ans + cnk(n, acnt) * cnk(n, bcnt)) % MOD;
    }
  }

  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using ll = int64_t;
using ld = long double;
using ull = uint64_t;
using namespace std;
using namespace __gnu_pbds;

const int MAXN = 300228;

ll fact[MAXN];
ll rfact[MAXN];

const ll MOD = 998244353;

ll pw(ll a, ll b) {
  if (!b) {
    return 1;
  }

  ll v = pw(a, b / 2);
  v = (v * v) % MOD;

  if (b & 1) {
    v = (v * a) % MOD;
  }

  return v;
}

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

int main() {
#ifdef BZ
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout.setf(ios::fixed);
  cout.precision(20);
  int n, a, b;
  ll k;
  cin >> n >> a >> b >> k;
  fact[0] = 1;
  for (int i = 1; i <= n; ++i) {
    fact[i] = fact[i - 1] * i % MOD;
  }

  rfact[n] = pw(fact[n], MOD - 2);
  for (int i = n; i > 0; --i) {
    rfact[i - 1] = rfact[i] * i % MOD;
  }

  ll ans = 0;

  for (ll acnt = 0; acnt <= n; ++acnt) {
    ll bcnt = (k - (acnt * a)) / b;
    if (bcnt <= n && bcnt >= 0 && (a * acnt + b * bcnt == k)) {
      ans = (ans + cnk(n, acnt) * cnk(n, bcnt)) % MOD;
    }
  }

  cout << ans << "\n";
}
Problem p03332 (C++)

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

using namespace std;

#define long long long
#define INF ((long)1e18)
#define MOD 998244353
#define mll map<long, long>
#define pll pair<long, long>
#define pb push_back
#define FOR(i, a, b) for (long i = (a); i < (long)(b); i++)
#define PRINTV(v)                                                              \
  FOR(_i, 0, v.size())                                                         \
  cout << v[_i] << " ";                                                        \
  cout << "\n";

long c[400100];

long inv(long a, long b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; }

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

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

  c[0] = 1;
  FOR(i, 1, n + 1) {
    c[i] = (((c[i - 1] * (n - i + 1)) % MOD) * inv(i, MOD)) % MOD;
  }
  long total = 0;
  for (long i = 0; i <= n; i++) {
    long soma = i * a;
    long aux = k - soma;
    if (aux < 0)
      continue;
    long x = c[i];
    if (aux % b == 0) {
      long j = aux / b;
      x = (x * c[j]) % MOD;
      total = (total + x) % MOD;
    }
  }
  printf("%lld\n", total);
}


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

using namespace std;

#define long long long
#define INF ((long)1e18)
#define MOD 998244353
#define mll map<long, long>
#define pll pair<long, long>
#define pb push_back
#define FOR(i, a, b) for (long i = (a); i < (long)(b); i++)
#define PRINTV(v)                                                              \
  FOR(_i, 0, v.size())                                                         \
  cout << v[_i] << " ";                                                        \
  cout << "\n";

long c[400100];

long inv(long a, long b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; }

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

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

  c[0] = 1;
  FOR(i, 1, n + 1) {
    c[i] = (((c[i - 1] * (n - i + 1)) % MOD) * inv(i, MOD)) % MOD;
  }
  long total = 0;
  for (long i = 0; i <= n; i++) {
    long soma = i * a;
    long aux = k - soma;
    if (aux < 0)
      continue;
    long x = c[i];
    if (aux % b == 0) {
      long j = aux / b;
      if (j > n)
        continue;
      x = (x * c[j]) % MOD;
      total = (total + x) % MOD;
    }
  }
  printf("%lld\n", total);
}
Problem p03332 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define N 600005
#define ll long long
#define MOD 998244353
using namespace std;
ll n, a, b, k, inv[N], fact[N];
ll qpow(ll x, ll y) {
  ll ans = 1;
  for (; y; y >>= 1, x = x * x % MOD) {
    if (y & 1)
      ans = ans * x % MOD;
  }
  return ans;
}
ll cc(ll a, ll b) {
  // printf("%lld %lld %lld\n",a,b,fact[a]*inv[a-b]%MOD*inv[b]%MOD);
  // system("pause");
  return fact[a] * inv[a - b] % MOD * inv[b] % MOD;
}
int main() {
  cin >> n >> a >> b >> k;
  fact[0] = fact[1] = 1;
  for (int i = 2; i <= n; i++)
    fact[i] = fact[i - 1] * i % MOD;
  inv[0] = inv[1] = 1;
  for (int i = 2; i <= n; i++)
    inv[i] = qpow(fact[i], MOD - 2);
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    if (a * i > k)
      break;
    ll l = 0, r = k / b + 1;
    while (r - l > 1) {
      ll mid = l + r >> 1;
      if (mid * b + a * i <= k)
        l = mid;
      else
        r = mid;
    }
    // printf("%lld %lld\n",l*b+a*i,k);
    if (l * b + a * i == k && (l <= n)) {
      // printf("%lld %lld\n",i,l);
      for (ll c = max(l + i - n, 0ll); c <= min(i, l); c++) {
        ans =
            (ans + cc(n, c) * cc(n - c, i - c) % MOD * cc(n - i, l - c) % MOD) %
            MOD;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 600005
#define ll long long
#define MOD 998244353
using namespace std;
ll n, a, b, k, inv[N], fact[N];
ll qpow(ll x, ll y) {
  ll ans = 1;
  for (; y; y >>= 1, x = x * x % MOD) {
    if (y & 1)
      ans = ans * x % MOD;
  }
  return ans;
}
ll cc(ll a, ll b) {
  // printf("%lld %lld %lld\n",a,b,fact[a]*inv[a-b]%MOD*inv[b]%MOD);
  // system("pause");
  return fact[a] * inv[a - b] % MOD * inv[b] % MOD;
}
int main() {
  cin >> n >> a >> b >> k;
  fact[0] = fact[1] = 1;
  for (int i = 2; i <= n; i++)
    fact[i] = fact[i - 1] * i % MOD;
  inv[0] = inv[1] = 1;
  for (int i = 2; i <= n; i++)
    inv[i] = qpow(fact[i], MOD - 2);
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    if (a * i > k)
      break;
    ll l = 0, r = k / b + 1;
    while (r - l > 1) {
      ll mid = l + r >> 1;
      if (mid * b + a * i <= k)
        l = mid;
      else
        r = mid;
    }
    // printf("%lld %lld\n",l*b+a*i,k);
    if (l * b + a * i == k && (l <= n)) {
      ans = (ans + cc(n, l) * cc(n, i)) % MOD;
      /*for (ll c=max(l+i-n,0ll);c<=min(i,l);c++){
              ans=(ans+cc(n,c)*cc(n-c,i-c)%MOD*cc(n-i,l-c)%MOD)%MOD;
      }*/
    }
  }
  cout << ans << endl;
}
Problem p03332 (C++)

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

#define mod 998244353

// F_mod 上でのべき乗, 蟻本p115
ll modPow(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

// Fermat の小定理を用いた F_mod 上での逆元計算x^(mod-2)
ll Inverse(ll x) { return modPow(x, mod - 2); }

// iの階乗 = fact[i], main内の階乗計算をする必要がある
#define MAX_FACT 300001
ll fact[MAX_FACT];

ll nCk(int n, int k) {
  // 　n_C_kの計算, 蟻本p.263
  ll ans = fact[n] * Inverse(fact[k] * fact[n - k] % mod);
  ans %= mod;
  return ans;
}

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

  // 階乗計算
  fact[0] = 1;
  for (int i = 1; i < MAX_FACT; ++i) {
    fact[i] = fact[i - 1] * i % mod;
  }

  ll ans = 0;
  // A点加点するブロックの数
  for (int i = 0; i <= N; ++i) {
    ll rem = K - A * i;
    if (rem % B != 0 || rem / B > N)
      continue;
    ll k = rem / B;
    ans += nCk(N, i) * nCk(N, k);
    ans %= mod;
  }
  cout << ans;
  return 0;
}

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

#define mod 998244353

// F_mod 上でのべき乗, 蟻本p115
ll modPow(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

// Fermat の小定理を用いた F_mod 上での逆元計算x^(mod-2)
ll Inverse(ll x) { return modPow(x, mod - 2); }

// iの階乗 = fact[i], main内の階乗計算をする必要がある
#define MAX_FACT 300001
ll fact[MAX_FACT];

ll nCk(int n, int k) {
  // 　n_C_kの計算, 蟻本p.263
  ll ans = fact[n] * Inverse(fact[k] * fact[n - k] % mod);
  ans %= mod;
  return ans;
}

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

  // 階乗計算
  fact[0] = 1;
  for (int i = 1; i < MAX_FACT; ++i) {
    fact[i] = fact[i - 1] * i % mod;
  }

  ll ans = 0;
  // A点加点するブロックの数
  for (int i = 0; i <= N; ++i) {
    ll rem = K - A * i;
    if (rem < 0)
      break;
    if (rem % B != 0 || rem / B > N)
      continue;
    ll k = rem / B;
    ans += nCk(N, i) * nCk(N, k);
    ans %= mod;
  }
  cout << ans;
  return 0;
}
Problem p03332 (C++)

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

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;
const ll MOD = 998244353;
const int N = 3e5 + 5;

ll n, A, B, k;
ll fakt[N];

ll add(ll a, ll b) { return (a + b) % MOD; }

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

ll pot(ll base, ll exponent) {
  ll result = 1;
  while (exponent > 0) {
    if (exponent & 1)
      result = mul(result, base);
    exponent = exponent / 2;
    base = mul(base, base);
  }
  return result;
}

ll povrh(ll n, ll k) {
  if (k == 0)
    return 1LL;
  return mul(fakt[n], pot(mul(fakt[n - k], fakt[k]), MOD - 2));
}

int main() {
  scanf("%lld %lld %lld %lld", &n, &A, &B, &k);
  fakt[0] = 1;
  for (int i = 1; i <= n + 2; ++i)
    fakt[i] = mul(fakt[i - 1], (ll)i);
  ll sol = 0;
  for (ll a = 0; a <= n; ++a) {
    if ((k - a * A) % B || k < a * A)
      continue;
    ll b = (k - a * A) / B;
    if (b < 0)
      continue;
    sol = add(sol, mul(povrh(n, (ll)a), povrh(n, (ll)b)));
  }
  printf("%lld\n", sol);
  return 0;
}


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

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;
const ll MOD = 998244353;
const int N = 3e5 + 5;

ll n, A, B, k;
ll fakt[N];

ll add(ll a, ll b) { return (a + b) % MOD; }

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

ll pot(ll base, ll exponent) {
  ll result = 1;
  while (exponent > 0) {
    if (exponent & 1)
      result = mul(result, base);
    exponent = exponent / 2;
    base = mul(base, base);
  }
  return result;
}

ll povrh(ll n, ll k) {
  if (k == 0)
    return 1LL;
  return mul(fakt[n], pot(mul(fakt[n - k], fakt[k]), MOD - 2));
}

int main() {
  scanf("%lld %lld %lld %lld", &n, &A, &B, &k);
  fakt[0] = 1;
  for (int i = 1; i <= n + 2; ++i)
    fakt[i] = mul(fakt[i - 1], (ll)i);
  ll sol = 0;
  for (ll a = 0; a <= n; ++a) {
    if ((k - a * A) % B || k < a * A)
      continue;
    ll b = (k - a * A) / B;
    if (b < 0)
      continue;
    if (b > n)
      continue;
    sol = add(sol, mul(povrh(n, (ll)a), povrh(n, (ll)b)));
  }
  printf("%lld\n", sol);
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define pb push_back
#define mp make_pair
#define X first
#define Y second

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

const int MOD = 998244353;
const int INF = 2e9;
const int MAX_N = 300000;

ll factorial[MAX_N + 1];
ll inverse[MAX_N + 1];

// 繰り返し２乗法による冪乗(x^n)計算 O(log(n))
int exponentiation(ll x, int n) {
  int res = 1;
  while (0 < n) {
    if (n & 1) {
      res = x * res % MOD;
    }
    x = x * x % MOD;
    n >>= 1;
  }
  return res;
}

// 組み合わせ nCr の計算 O(1)
ll combination(int n, int r) {
  if (n - r == 0 || r == 0)
    return 1;
  static bool flag = true;
  if (flag) {
    // 階乗と階乗の逆数の初期化 O(n)
    factorial[0] = 1;
    inverse[0] = 1;
    for (int i = 1; i <= MAX_N; i++) {
      factorial[i] = factorial[i - 1] * i % MOD;
      inverse[i] = exponentiation(factorial[i], MOD - 2);
    }
    flag = false;
  }
  return factorial[n] * inverse[n - r] % MOD * inverse[r] % MOD;
}

int main() {
  ll N, A, B, K, ans = 0;
  vii XYs;
  cin >> N >> A >> B >> K;
  for (int x = 0; x <= N; x++) {
    if (A * x <= K) {
      if ((K - A * x) % B == 0) {
        ll y = (K - A * x) / B;
        ans += (combination(N, x) % MOD) * (combination(N, y) % MOD);
        ans %= MOD;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define pb push_back
#define mp make_pair
#define X first
#define Y second

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

const int MOD = 998244353;
const int INF = 2e9;
const int MAX_N = 300000;

ll factorial[MAX_N + 1];
ll inverse[MAX_N + 1];

// 繰り返し２乗法による冪乗(x^n)計算 O(log(n))
int exponentiation(ll x, int n) {
  int res = 1;
  while (0 < n) {
    if (n & 1) {
      res = x * res % MOD;
    }
    x = x * x % MOD;
    n >>= 1;
  }
  return res;
}

// 組み合わせ nCr の計算 O(1)
ll combination(int n, int r) {
  if (n - r == 0 || r == 0)
    return 1;
  static bool flag = true;
  if (flag) {
    // 階乗と階乗の逆数の初期化 O(n)
    factorial[0] = 1;
    inverse[0] = 1;
    for (int i = 1; i <= MAX_N; i++) {
      factorial[i] = factorial[i - 1] * i % MOD;
      inverse[i] = exponentiation(factorial[i], MOD - 2);
    }
    flag = false;
  }
  return factorial[n] * inverse[n - r] % MOD * inverse[r] % MOD;
}

int main() {
  ll N, A, B, K, ans = 0;
  vii XYs;
  cin >> N >> A >> B >> K;
  for (int x = 0; x <= N; x++) {
    if (A * x > K) {
      continue;
    }
    if ((K - A * x) % B != 0) {
      continue;
    }

    ll y = (K - A * x) / B;
    if (y <= N) {
      // cout << x << ", " << y << endl;
      ans += (combination(N, x) % MOD) * (combination(N, y) % MOD);
      ans %= MOD;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define sl(x) scanf("%lld", &x)
#define pl(x) printf("%lld\n", x)
using namespace std;
const int N = 1e6 + 5;
const int mod = 998244353;
const int INF = 0x3f3f3f3f;
typedef long long ll;
ll inv(ll b) {
  if (b == 1)
    return 1;
  return (mod - mod / b) * inv(mod % b) % mod;
}
ll fac[N];
ll C(ll n, ll m) {
  ll t = (fac[m] * fac[n - m]) % mod;
  if (t == 0)
    return 1;
  return (fac[n] * inv(t)) % mod;
}
ll ans[N][2];
int main() {
  ll n, i, a, b, k;
  fac[0] = 1;
  fac[1] = 1;
  for (i = 2; i <= 3e5 + 5; i++)
    fac[i] = (fac[i - 1] * i) % mod;
  sl(n);
  sl(a);
  sl(b);
  sl(k);
  ll cnt = 0;
  for (i = 0; i <= n; i++) {
    if (k - (i * a) >= 0 && (k - a * i) % b == 0) {
      ans[cnt][0] = i;
      ans[cnt++][1] = (k - a * i) / b;
    }
  }
  ll res = 0;
  for (i = 0; i < cnt; i++) {
    res = (res + C(n, ans[i][0]) * C(n, ans[i][1]) % mod) % mod;
  }
  pl(res);
}

Fixed submission:
#include <bits/stdc++.h>
#define sl(x) scanf("%lld", &x)
#define pl(x) printf("%lld\n", x)
using namespace std;
const int N = 1e6 + 5;
const int mod = 998244353;
const int INF = 0x3f3f3f3f;
typedef long long ll;
ll inv(ll b) {
  if (b == 1)
    return 1;
  return (mod - mod / b) * inv(mod % b) % mod;
}
ll fac[N];
ll C(ll n, ll m) {
  ll t = (fac[m] * fac[n - m]) % mod;
  if (t == 0)
    return 1;
  return (fac[n] * inv(t)) % mod;
}
ll ans[N][2];
int main() {
  ll n, i, a, b, k;
  fac[0] = 1;
  fac[1] = 1;
  for (i = 2; i <= 3e5 + 5; i++)
    fac[i] = (fac[i - 1] * i) % mod;
  sl(n);
  sl(a);
  sl(b);
  sl(k);
  ll cnt = 0;
  for (i = 0; i <= n; i++) {
    if (k - (i * a) >= 0 && (k - a * i) % b == 0 && (k - a * i) / b <= n) {
      ans[cnt][0] = i;
      ans[cnt++][1] = (k - a * i) / b;
    }
  }
  ll res = 0;
  for (i = 0; i < cnt; i++) {
    res = (res + C(n, ans[i][0]) * C(n, ans[i][1]) % mod) % mod;
  }
  pl(res);
}
Problem p03332 (C++)

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

using namespace std;

typedef long long ll;

const ll MOD = 998244353;
const ll MAXN = 300010;

ll fact[MAXN + 1], rev_fact[MAXN + 1];

// 二分累乗法により、x^n %mod を求める
ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  ll res = mod_pow(x * x % MOD, n / 2);
  // nが奇数の場合, x^n = ((x^2)^(n/2)) * x
  if (n & 1)
    return res = res * x % MOD;
  // nが偶数の場合、x^n = (x^2)^(n/2)
  return res % MOD;
}

// nCk = n! / (n - k)!k!
ll comb(ll n, ll k) {
  ll res = 1;
  k = min(n - k, k);
  res = (((fact[n] * rev_fact[n - k]) % MOD) * rev_fact[k]) % MOD;
  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  ll n, a, b, k;
  cin >> n >> a >> b >> k;
  ll ans = 0;
  fact[0] = fact[1] = 1;
  rev_fact[0] = rev_fact[1] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
  }
  for (int i = 1; i <= n; i++) {
    rev_fact[i] = (rev_fact[i - 1] * mod_pow(i, MOD - 2)) % MOD;
  }
  // red
  for (ll cr = 0; cr <= n; cr++) {
    if ((k - a * cr) % b)
      continue;
    ll cb = (k - a * cr) / b;
    if (cb > n)
      continue;
    ans = (ans + (comb(n, cr) * comb(n, cb)) % MOD) % MOD;
  }
  cout << ans << endl;
}

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

using namespace std;

typedef long long ll;

const ll MOD = 998244353;
const ll MAXN = 300010;

ll fact[MAXN + 1], rev_fact[MAXN + 1];

// 二分累乗法により、x^n %mod を求める
ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  ll res = mod_pow(x * x % MOD, n / 2);
  // nが奇数の場合, x^n = ((x^2)^(n/2)) * x
  if (n & 1)
    return res = res * x % MOD;
  // nが偶数の場合、x^n = (x^2)^(n/2)
  return res % MOD;
}

// nCk = n! / (n - k)!k!
ll comb(ll n, ll k) {
  ll res = 1;
  k = min(n - k, k);
  res = (((fact[n] * rev_fact[n - k]) % MOD) * rev_fact[k]) % MOD;
  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  ll n, a, b, k;
  cin >> n >> a >> b >> k;
  ll ans = 0;
  fact[0] = fact[1] = 1;
  rev_fact[0] = rev_fact[1] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
  }
  for (int i = 1; i <= n; i++) {
    rev_fact[i] = (rev_fact[i - 1] * mod_pow(i, MOD - 2)) % MOD;
  }
  // red
  for (ll cr = 0; cr <= n; cr++) {
    if ((k - a * cr) < 0 || (k - a * cr) % b)
      continue;
    ll cb = (k - a * cr) / b;
    if (cb > n)
      continue;
    ans = (ans + (comb(n, cr) * comb(n, cb)) % MOD) % MOD;
  }
  cout << ans << endl;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define rep(i, n) FOR(i, 0, n - 1)
#define int long long
#define MOD 998244353
#define MAX_N 300005
#define P pair<int, int>

int fac[MAX_N];
int revfac[MAX_N];

int nCk(int n, int k) { return fac[n] * revfac[k] % MOD * revfac[n - k] % MOD; }

int pow(int a, int e) {
  int ans = 1;
  while (e > 0) {
    if (e & 1) {
      ans *= a;
      ans %= MOD;
    }
    a *= a;
    a %= MOD;
    e = e >> 1;
  }
  return ans % MOD;
}

void set_fac() {
  fac[0] = 1;
  fac[1] = 1;
  revfac[0] = 1;
  for (int i = 1; i < MAX_N; ++i) {
    fac[i] = i * fac[i - 1] % MOD;
    revfac[i] = pow(fac[i], MOD - 2) % MOD;
  }
}

signed main() {

  int n, a, b, k;
  cin >> n >> a >> b >> k;
  vector<P> g;
  int ans = 0;
  set_fac();
  rep(i, n + 1) {
    if ((k - i * a) % b)
      continue;
    if ((k - i * a) / b > n)
      continue;
    g.push_back(P(i, (k - i * a) / b));
  }

  rep(i, g.size()) {
    // cout << g[i].first << " " << g[i].second << endl;
    ans += (nCk(n, g[i].first) * nCk(n, g[i].second)) % MOD;
    ans %= MOD;
  }

  cout << ans << endl;
}

// cout << ans << endl;

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define rep(i, n) FOR(i, 0, n - 1)
#define int long long
#define MOD 998244353
#define MAX_N 300005
#define P pair<int, int>

int fac[MAX_N];
int revfac[MAX_N];

int nCk(int n, int k) { return fac[n] * revfac[k] % MOD * revfac[n - k] % MOD; }

int pow(int a, int e) {
  int ans = 1;
  while (e > 0) {
    if (e & 1) {
      ans *= a;
      ans %= MOD;
    }
    a *= a;
    a %= MOD;
    e = e >> 1;
  }
  return ans % MOD;
}

void set_fac() {
  fac[0] = 1;
  fac[1] = 1;
  revfac[0] = 1;
  for (int i = 1; i < MAX_N; ++i) {
    fac[i] = i * fac[i - 1] % MOD;
    revfac[i] = pow(fac[i], MOD - 2) % MOD;
  }
}

signed main() {

  int n, a, b, k;
  cin >> n >> a >> b >> k;
  vector<P> g;
  int ans = 0;
  set_fac();
  rep(i, n + 1) {
    if ((k - i * a) < 0)
      break;
    if ((k - i * a) % b)
      continue;
    if ((k - i * a) / b > n)
      continue;
    g.push_back(P(i, (k - i * a) / b));
  }

  rep(i, g.size()) {
    // cout << g[i].first << " " << g[i].second << endl;
    ans += (nCk(n, g[i].first) * nCk(n, g[i].second)) % MOD;
    ans %= MOD;
  }

  cout << ans << endl;
}

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

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

using namespace std;

const int MOD = 998244353;
const int MAXN = 300005;

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

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

int rek(int x, int b) {
  if (x == 0)
    return 1;
  if (x == 1)
    return b;

  int aa = rek(x / 2, b);
  if (x % 2 == 0) {
    return mul(aa, aa);
  } else {
    return mul(mul(aa, aa), b);
  }
}

int fak[MAXN];

int povrh(int n, int k) {
  return mul(fak[n], mul(rek(MOD - 2, fak[k]), rek(MOD - 2, fak[n - k])));
}

int main() {

  fak[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    fak[i] = mul(fak[i - 1], i);
  }

  int n;
  long long a, b, k;
  cin >> n >> a >> b >> k;

  int sol = 0;

  for (int i = 0; i <= n; i++) {
    long long tren = a * i;
    if (((k - tren) % b) == 0) {
      long long j = (k - tren) / b;
      if (j > n)
        continue;
      int curr = mul(povrh(n, i), povrh(n, j));
      // cout << "i je:  " << i << "    j je: " << j << endl;
      // cout << "curr je: " << curr << endl;
      sol = add(sol, curr);
    }
  }

  cout << sol << "\n";

  return 0;
}

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

using namespace std;

const int MOD = 998244353;
const int MAXN = 300005;

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

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

int rek(int x, int b) {
  if (x == 0)
    return 1;
  if (x == 1)
    return b;

  int aa = rek(x / 2, b);
  if (x % 2 == 0) {
    return mul(aa, aa);
  } else {
    return mul(mul(aa, aa), b);
  }
}

int fak[MAXN];

int povrh(int n, int k) {
  return mul(fak[n], mul(rek(MOD - 2, fak[k]), rek(MOD - 2, fak[n - k])));
}

int main() {

  fak[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    fak[i] = mul(fak[i - 1], i);
  }

  int n;
  long long a, b, k;
  cin >> n >> a >> b >> k;

  int sol = 0;

  for (int i = 0; i <= n; i++) {
    long long tren = a * i;
    if (tren > k)
      break;
    if (((k - tren) % b) == 0) {
      long long j = (k - tren) / b;
      if (j > n)
        continue;
      int curr = mul(povrh(n, i), povrh(n, j));
      // cout << "i je:  " << i << "    j je: " << j << endl;
      // cout << "curr je: " << curr << endl;
      sol = add(sol, curr);
    }
  }

  cout << sol << "\n";

  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

using ll = long long;

const ll MOD = 998244353;

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

int main() {
  ll N, A, B, K, ans = 0;
  cin >> N >> A >> B >> K;
  vector<ll> f(N + 1, 1);
  rep(i, N) f[i + 1] = (f[i] * (i + 1)) % MOD;
  rep(i, N + 1) {
    ll x = K - A * i;
    if (x % B != 0)
      continue;
    ll j = x / B;
    if (j > N)
      continue;
    // cout << i << j << endl;
    ll c1 = ((f[N] * mod_pow(f[i], MOD - 2, MOD)) % MOD *
             mod_pow(f[N - i], MOD - 2, MOD)) %
            MOD;
    ll c2 = ((f[N] * mod_pow(f[j], MOD - 2, MOD)) % MOD *
             mod_pow(f[N - j], MOD - 2, MOD)) %
            MOD;
    ans = (ans + c1 * c2) % MOD;
  }
  cout << ans << endl;
}

Fixed submission:
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

using ll = long long;

const ll MOD = 998244353;

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

int main() {
  ll N, A, B, K, ans = 0;
  cin >> N >> A >> B >> K;
  vector<ll> f(N + 1, 1);
  rep(i, N) f[i + 1] = (f[i] * (i + 1)) % MOD;
  rep(i, N + 1) {
    ll x = K - A * i;
    if (x < 0)
      break;
    if (x % B != 0)
      continue;
    ll j = x / B;
    if (j > N)
      continue;
    // cout << i << j << endl;
    ll c1 = ((f[N] * mod_pow(f[i], MOD - 2, MOD)) % MOD *
             mod_pow(f[N - i], MOD - 2, MOD)) %
            MOD;
    ll c2 = ((f[N] * mod_pow(f[j], MOD - 2, MOD)) % MOD *
             mod_pow(f[N - j], MOD - 2, MOD)) %
            MOD;
    ans = (ans + c1 * c2) % MOD;
  }
  cout << ans << endl;
}