Problem p03332 (C++)

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

using namespace std;

class inverse {
public:
  inverse(int n, int mod);
  ~inverse();
  long long get_inv(int x);
  long long get_fact(int x);
  long long get_invfact(int x);
  long long get_comb(int x, int y);

private:
  int mod;
  long long *inv;
  long long *fact;
  long long *invfact;
};

inverse::inverse(int n, int mod) : mod(mod) {
  inv = (long long *)malloc(sizeof(long long) * (n + 1));
  fact = (long long *)malloc(sizeof(long long) * (n + 1));
  invfact = (long long *)malloc(sizeof(long long) * (n + 1));
  inv[1] = 1;
  for (int i = 2; i <= n; i++)
    inv[i] = inv[mod % i] * (mod - mod / i) % mod;
  fact[0] = invfact[0] = 1;
  for (int i = 1; i <= n; i++)
    fact[i] = fact[i - 1] * i % mod;
  for (int i = 1; i <= n; i++)
    invfact[i] = invfact[i - 1] * inv[i] % mod;
}

inverse::~inverse() {
  free(inv);
  free(fact);
  free(invfact);
}

long long inverse::get_inv(int x) { return inv[x]; }

long long inverse::get_fact(int x) { return fact[x]; }

long long inverse::get_invfact(int x) { return invfact[x]; }

long long inverse::get_comb(int x, int y) {
  return fact[x] * invfact[y] % mod * invfact[x - y] % mod;
}

int main() {
  int m = 998244353, i;
  long long n, a, b, k, ans = 0;

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

  inverse inv(n, m);

  for (i = 0; i <= n; i++) {
    long long x = k - a * i;

    if (x % b == 0 && x / b <= n) {
      ans += inv.get_comb(n, i) * inv.get_comb(n, x / b) % m;
      if (ans >= m)
        ans -= m;
    }
  }

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

  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstdlib>

using namespace std;

class inverse {
public:
  inverse(int n, int mod);
  ~inverse();
  long long get_inv(int x);
  long long get_fact(int x);
  long long get_invfact(int x);
  long long get_comb(int x, int y);

private:
  int mod;
  long long *inv;
  long long *fact;
  long long *invfact;
};

inverse::inverse(int n, int mod) : mod(mod) {
  inv = (long long *)malloc(sizeof(long long) * (n + 1));
  fact = (long long *)malloc(sizeof(long long) * (n + 1));
  invfact = (long long *)malloc(sizeof(long long) * (n + 1));
  inv[1] = 1;
  for (int i = 2; i <= n; i++)
    inv[i] = inv[mod % i] * (mod - mod / i) % mod;
  fact[0] = invfact[0] = 1;
  for (int i = 1; i <= n; i++)
    fact[i] = fact[i - 1] * i % mod;
  for (int i = 1; i <= n; i++)
    invfact[i] = invfact[i - 1] * inv[i] % mod;
}

inverse::~inverse() {
  free(inv);
  free(fact);
  free(invfact);
}

long long inverse::get_inv(int x) { return inv[x]; }

long long inverse::get_fact(int x) { return fact[x]; }

long long inverse::get_invfact(int x) { return invfact[x]; }

long long inverse::get_comb(int x, int y) {
  return fact[x] * invfact[y] % mod * invfact[x - y] % mod;
}

int main() {
  int m = 998244353, i;
  long long n, a, b, k, ans = 0;

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

  inverse inv(n, m);

  for (i = 0; i <= n && a * i <= k; i++) {
    long long x = k - a * i;

    if (x % b == 0 && x / b <= n) {
      ans += inv.get_comb(n, i) * inv.get_comb(n, x / b) % m;
      if (ans >= m)
        ans -= m;
    }
  }

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

  return 0;
}