Problem p03332 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
typedef long long ll;
typedef double dd;
#define For(i, j, k) for (int i = (int)j; i <= (int)k; ++i)
#define Forr(i, j, k) for (int i = (int)j; i >= (int)k; --i)
#define Set(a, p) memset(a, p, sizeof(a))
using namespace std;

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

const int maxn = 3e5 + 1e2;
const int N = 3e5;
const int modd = 998244353;
int n;
int Ans;
ll a, b, k;
int fac[maxn], inv[maxn];

inline void file() {
  freopen("AGC025B.in", "r", stdin);
  freopen("AGC025B.out", "w", stdout);
}

inline int quick(int a, int b) {
  int s = 1;
  while (b) {
    if (b % 2 == 1)
      s = 1ll * s * a % modd;
    a = 1ll * a * a % modd;
    b /= 2;
  }
  return s;
}

inline void init() {
  fac[0] = inv[0] = 1;
  For(i, 1, N) fac[i] = 1ll * fac[i - 1] * i % modd;
  inv[N] = quick(fac[N], modd - 2);
  Forr(i, N - 1, 1) inv[i] = 1ll * inv[i + 1] * (i + 1) % modd;
}

inline void Add(int &a, int b) {
  a += b;
  a = (a >= modd ? a - modd : a);
  a = (a < 0 ? a + modd : a);
}

inline int C(int n, int m) {
  if (n < m)
    return 0;
  return 1ll * fac[n] * inv[m] % modd * inv[n - m] % modd;
}

int main() {
  //	file();
  init();
  scanf("%d%lld%lld%lld", &n, &a, &b, &k);
  For(i, 0, n) {
    if (a * i > k)
      break;
    if ((k - a * i) % b != 0)
      continue;
    int j = (k - a * i) / b;
    For(l, 0, min(i, j)) if (i + j - l <= n)
        Add(Ans, 1ll * C(n, i + j - l) * fac[i + j - l] % modd * inv[i - l] %
                     modd * inv[j - l] % modd * inv[l] % modd);
  }
  printf("%d", Ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
typedef long long ll;
typedef double dd;
#define For(i, j, k) for (int i = (int)j; i <= (int)k; ++i)
#define Forr(i, j, k) for (int i = (int)j; i >= (int)k; --i)
#define Set(a, p) memset(a, p, sizeof(a))
using namespace std;

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

const int maxn = 3e5 + 1e2;
const int N = 3e5;
const int modd = 998244353;
int n;
int Ans;
ll a, b, k;
int fac[maxn], inv[maxn];

inline void file() {
  freopen("AGC025B.in", "r", stdin);
  freopen("AGC025B.out", "w", stdout);
}

inline int quick(int a, int b) {
  int s = 1;
  while (b) {
    if (b % 2 == 1)
      s = 1ll * s * a % modd;
    a = 1ll * a * a % modd;
    b /= 2;
  }
  return s;
}

inline void init() {
  fac[0] = inv[0] = 1;
  For(i, 1, N) fac[i] = 1ll * fac[i - 1] * i % modd;
  inv[N] = quick(fac[N], modd - 2);
  Forr(i, N - 1, 1) inv[i] = 1ll * inv[i + 1] * (i + 1) % modd;
}

inline void Add(int &a, int b) {
  a += b;
  a = (a >= modd ? a - modd : a);
  a = (a < 0 ? a + modd : a);
}

inline int C(int n, int m) {
  if (n < m)
    return 0;
  return 1ll * fac[n] * inv[m] % modd * inv[n - m] % modd;
}

int main() {
  //	file();
  init();
  scanf("%d%lld%lld%lld", &n, &a, &b, &k);
  For(i, 0, n) {
    if (a * i > k)
      break;
    if ((k - a * i) % b != 0)
      continue;
    int j = (k - a * i) / b;
    //		For (l,0,min(i,j))
    //			if (i+j-l<=n)
    //				Add(Ans,1ll*C(n,i+j-l)*fac[i+j-l]%modd*inv[i-l]%modd*inv[j-l]%modd*inv[l]%modd);
    Add(Ans, 1ll * C(n, i) * C(n, j) % modd);
  }
  printf("%d", Ans);
  return 0;
}