Problem p02950 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

//-------------------------------------------------
//--ModInt
//-------------------------------------------------
::std::uint_fast64_t MOD;

class mint {
private:
  using value_type = ::std::uint_fast64_t;
  value_type n;

public:
  mint() : n(0) {}
  mint(::std::int_fast64_t _n) : n(_n < 0 ? MOD - (-_n) % MOD : _n % MOD) {}
  mint(const mint &m) : n(m.n) {}

  friend ::std::ostream &operator<<(::std::ostream &os, const mint &a) {
    return os << a.n;
  }
  friend ::std::istream &operator>>(::std::istream &is, mint &a) {
    value_type temp;
    is >> temp;
    a = mint(temp);
    return is;
  }

  mint &operator+=(const mint &m) {
    n += m.n;
    n = (n < MOD) ? n : n - MOD;
    return *this;
  }
  mint &operator-=(const mint &m) {
    n += MOD - m.n;
    n = (n < MOD) ? n : n - MOD;
    return *this;
  }
  mint &operator*=(const mint &m) {
    n = n * m.n % MOD;
    return *this;
  }
  mint &operator/=(const mint &m) { return *this *= m.inv(); }
  mint &operator++() { return *this += 1; }
  mint &operator--() { return *this -= 1; }

  mint operator+(const mint &m) const { return mint(*this) += m; }
  mint operator-(const mint &m) const { return mint(*this) -= m; }
  mint operator*(const mint &m) const { return mint(*this) *= m; }
  mint operator/(const mint &m) const { return mint(*this) /= m; }
  mint operator++(int) {
    mint t(*this);
    *this += 1;
    return t;
  }
  mint operator--(int) {
    mint t(*this);
    *this -= 1;
    return t;
  }

  bool operator==(const mint &m) const { return n == m.n; }
  bool operator!=(const mint &m) const { return n != m.n; }
  mint operator-() const { return mint(MOD - n); }

  mint pow(value_type b) const {
    mint ret(1), m(*this);
    while (b) {
      if (b & 1)
        ret *= m;
      m *= m;
      b >>= 1;
    }
    return ret;
  }

  mint inv() const { return pow(MOD - 2); }
};

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int p;
  cin >> p;
  MOD = p;
  vi a(p);
  rep(i, p) cin >> a[i];
  vector<mint> dp(p + 1);
  dp[0] = 1;
  rep(i, p) RFOR(j, p, 0) {
    dp[j] *= -i;
    if (j >= 1)
      dp[j] += dp[j - 1];
  }
  reverse(all(dp));
  vector<mint> ans(p);
  mint deno = 1;
  rep(i, p - 1) deno *= -(i + 1);
  rep(i, p) {
    // mint deno=1;
    // rep(j,p)if(j!=i) deno*=i-j;
    if (a[i] == 0)
      continue;
    vector<mint> poly = dp;
    rep(j, p) {
      ans[j] += poly[j] / deno;
      poly[j + 1] += poly[j] * i;
    }
    deno *= mint(i + 1) / (i - p + 1);
  }
  reverse(all(ans));
  rep(i, p) {
    if (i > 0)
      cout << " ";
    cout << ans[i];
  }
  cout << "\n";
  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

//-------------------------------------------------
//--ModInt
//-------------------------------------------------
::std::uint_fast64_t MOD;

class mint {
private:
  using value_type = ::std::uint_fast64_t;
  value_type n;

public:
  mint() : n(0) {}
  mint(::std::int_fast64_t _n) : n(_n < 0 ? MOD - (-_n) % MOD : _n % MOD) {}
  mint(const mint &m) : n(m.n) {}

  friend ::std::ostream &operator<<(::std::ostream &os, const mint &a) {
    return os << a.n;
  }
  friend ::std::istream &operator>>(::std::istream &is, mint &a) {
    value_type temp;
    is >> temp;
    a = mint(temp);
    return is;
  }

  mint &operator+=(const mint &m) {
    n += m.n;
    n = (n < MOD) ? n : n - MOD;
    return *this;
  }
  mint &operator-=(const mint &m) {
    n += MOD - m.n;
    n = (n < MOD) ? n : n - MOD;
    return *this;
  }
  mint &operator*=(const mint &m) {
    n = n * m.n % MOD;
    return *this;
  }
  mint &operator/=(const mint &m) { return *this *= m.inv(); }
  mint &operator++() { return *this += 1; }
  mint &operator--() { return *this -= 1; }

  mint operator+(const mint &m) const { return mint(*this) += m; }
  mint operator-(const mint &m) const { return mint(*this) -= m; }
  mint operator*(const mint &m) const { return mint(*this) *= m; }
  mint operator/(const mint &m) const { return mint(*this) /= m; }
  mint operator++(int) {
    mint t(*this);
    *this += 1;
    return t;
  }
  mint operator--(int) {
    mint t(*this);
    *this -= 1;
    return t;
  }

  bool operator==(const mint &m) const { return n == m.n; }
  bool operator!=(const mint &m) const { return n != m.n; }
  mint operator-() const { return mint(MOD - n); }

  mint pow(value_type b) const {
    mint ret(1), m(*this);
    while (b) {
      if (b & 1)
        ret *= m;
      m *= m;
      b >>= 1;
    }
    return ret;
  }

  mint inv() const { return pow(MOD - 2); }
};

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int p;
  cin >> p;
  MOD = p;
  vi a(p);
  rep(i, p) cin >> a[i];
  vector<mint> dp(p + 1);
  dp[0] = 1;
  rep(i, p) RFOR(j, p, 0) {
    dp[j] *= -i;
    if (j >= 1)
      dp[j] += dp[j - 1];
  }
  reverse(all(dp));
  vector<mint> ans(p);
  mint deno = 1;
  rep(i, p - 1) deno *= -(i + 1);
  rep(i, p) {
    // mint deno=1;
    // rep(j,p)if(j!=i) deno*=i-j;
    if (a[i] == 0)
      continue;
    vector<mint> poly = dp;
    mint inv = deno.inv();
    rep(j, p) if (poly[j] != 0) {
      ans[j] += poly[j] * inv;
      poly[j + 1] += poly[j] * i;
    }
    deno *= mint(i + 1) / (i - p + 1);
  }
  reverse(all(ans));
  rep(i, p) {
    if (i > 0)
      cout << " ";
    cout << ans[i];
  }
  cout << "\n";
  return 0;
}