Problem p03332 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define For(i, l, r)                                                           \
  for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l)                                                       \
  for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
using namespace std;

inline bool chkmin(int &a, int b) { return b < a ? a = b, 1 : 0; }
inline bool chkmax(int &a, int b) { return b > a ? a = b, 1 : 0; }

void File() {
#ifdef zjp_shadow
  freopen("B.in", "r", stdin);
  freopen("B.out", "w", stdout);
#endif
}

typedef long long ll;

const int N = 3e5 + 1e3;
const ll Mod = 998244353;

ll fac[N], ifac[N];

ll fpm(ll x, int power) {
  ll res = 1;
  for (; power; power >>= 1, (x *= x) %= Mod)
    if (power & 1)
      (res *= x) %= Mod;
  return res;
}

ll Comb(int n, int m) {
  if (n < 0 || m < 0 || m > n)
    return 0;
  return fac[n] * ifac[m] % Mod * ifac[n - m] % Mod;
}

void Init(int maxn) {
  fac[0] = ifac[0] = 1;
  For(i, 1, maxn) fac[i] = fac[i - 1] * i % Mod;
  ifac[maxn] = fpm(fac[maxn], Mod - 2);
  Fordown(i, maxn - 1, 1) ifac[i] = ifac[i + 1] * (i + 1) % Mod;
}

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

  Init(3e5);

  ll ans = 0;

  For(A, 0, n) {
    if ((k - a * A) % b)
      continue;
    int B = (k - a * A) / b;
    For(C, 0, min(A, B)) {
      (ans += Comb(n, C) * Comb(n - C, A - C) % Mod *
              Comb(n - (A - C) - C, B - C) % Mod) %= Mod;
    }
  }

  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define For(i, l, r)                                                           \
  for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l)                                                       \
  for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
using namespace std;

inline bool chkmin(int &a, int b) { return b < a ? a = b, 1 : 0; }
inline bool chkmax(int &a, int b) { return b > a ? a = b, 1 : 0; }

void File() {
#ifdef zjp_shadow
  freopen("B.in", "r", stdin);
  freopen("B.out", "w", stdout);
#endif
}

typedef long long ll;

const int N = 3e5 + 1e3;
const ll Mod = 998244353;

ll fac[N], ifac[N];

ll fpm(ll x, int power) {
  ll res = 1;
  for (; power; power >>= 1, (x *= x) %= Mod)
    if (power & 1)
      (res *= x) %= Mod;
  return res;
}

ll Comb(int n, int m) {
  if (n < 0 || m < 0 || m > n)
    return 0;
  return fac[n] * ifac[m] % Mod * ifac[n - m] % Mod;
}

void Init(int maxn) {
  fac[0] = ifac[0] = 1;
  For(i, 1, maxn) fac[i] = fac[i - 1] * i % Mod;
  ifac[maxn] = fpm(fac[maxn], Mod - 2);
  Fordown(i, maxn - 1, 1) ifac[i] = ifac[i + 1] * (i + 1) % Mod;
}

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

  Init(3e5);

  ll ans = 0;

  For(A, 0, n) {
    if ((k - a * A) % b)
      continue;
    int B = (k - a * A) / b;
    if (B < 0 || B > n)
      continue;
    (ans += Comb(n, A) * Comb(n, B) % Mod) %= Mod;
    //	For (C, 0, min(A, B)) (ans += Comb(n, C) * Comb(n - C, A - C) % Mod *
    //Comb(n - (A - C) - C, B - C) % Mod) %= Mod;
  }

  printf("%lld\n", ans);

  return 0;
}