Problem p02950 (C++)

Buggy submission (status: Time Limit Exceeded):

// editorial見た。

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long ll;

long long inv(int a, int p) {
  return a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p;
}
int MODVAL;
struct mint {
  int val;
  mint() : val(0) {}
  mint(int x) : val(x % MODVAL) {
    if (val < 0)
      val += MODVAL;
  }
  mint(size_t x) : val(x % MODVAL) {}
  mint(long long x) : val(x % MODVAL) {
    if (val < 0)
      val += MODVAL;
  }
  mint &operator+=(mint y) {
    val += y.val;
    if (val >= MODVAL)
      val -= MODVAL;
    return *this;
  }
  mint &operator-=(mint y) {
    val -= y.val;
    if (val < 0)
      val += MODVAL;
    return *this;
  }
  mint &operator*=(mint y) {
    val = (val * (long long)y.val) % MODVAL;
    return *this;
  }
  mint &operator/=(mint y) {
    val = (val * inv(y.val, MODVAL)) % MODVAL;
    return *this;
  }
};
inline mint operator+(mint x, mint y) { return x += y; }
inline mint operator-(mint x, mint y) { return x -= y; }
inline mint operator*(mint x, mint y) { return x *= y; }
inline mint operator/(mint x, mint y) { return x /= y; }
mint POW(mint x, long long n) {
  mint r(1);
  for (; n; x *= x, n >>= 1)
    if (n & 1)
      r *= x;
  return r;
}
mint FAC(int n) {
  static vector<mint> FAC_(1, 1);
  while (int(FAC_.size()) <= n)
    FAC_.push_back(FAC_.back() * FAC_.size());
  return FAC_[n];
}
inline mint CMB(int n, int k) {
  return k < 0 || n < k ? 0 : FAC(n) / (FAC(k) * FAC(n - k));
}
inline ostream &operator<<(ostream &os, mint a) { return os << a.val; }

int as[3010];
mint ans[3010];

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

  int P;
  cin >> P;
  MODVAL = P;
  REP(i, P) {
    cin >> as[i];
    if (as[i] == 1) {
      ans[0] += 1;
      REP(j, P) { ans[P - 1 - j] -= CMB(P - 1, j) * POW(-i, j); }
    }
  }
  REP(i, P) {
    cout << ans[i];
    if (i == P - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }

  return 0;
}


Fixed submission:

// editorial見た。

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long ll;

long long inv(int a, int p) {
  return a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p;
}
int MODVAL;
struct mint {
  int val;
  mint() : val(0) {}
  mint(int x) : val(x % MODVAL) {
    if (val < 0)
      val += MODVAL;
  }
  mint(size_t x) : val(x % MODVAL) {}
  mint(long long x) : val(x % MODVAL) {
    if (val < 0)
      val += MODVAL;
  }
  mint &operator+=(mint y) {
    val += y.val;
    if (val >= MODVAL)
      val -= MODVAL;
    return *this;
  }
  mint &operator-=(mint y) {
    val -= y.val;
    if (val < 0)
      val += MODVAL;
    return *this;
  }
  mint &operator*=(mint y) {
    val = (val * (long long)y.val) % MODVAL;
    return *this;
  }
  mint &operator/=(mint y) {
    val = (val * inv(y.val, MODVAL)) % MODVAL;
    return *this;
  }
};
inline mint operator+(mint x, mint y) { return x += y; }
inline mint operator-(mint x, mint y) { return x -= y; }
inline mint operator*(mint x, mint y) { return x *= y; }
inline mint operator/(mint x, mint y) { return x /= y; }
mint POW(mint x, long long n) {
  mint r(1);
  for (; n; x *= x, n >>= 1)
    if (n & 1)
      r *= x;
  return r;
}
mint FAC(int n) {
  static vector<mint> FAC_(1, 1);
  while (int(FAC_.size()) <= n)
    FAC_.push_back(FAC_.back() * FAC_.size());
  return FAC_[n];
}
inline mint CMB(int n, int k) {
  return k < 0 || n < k ? 0 : FAC(n) / (FAC(k) * FAC(n - k));
}
inline ostream &operator<<(ostream &os, mint a) { return os << a.val; }

int as[3010];
mint ans[3010];

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

  int P;
  cin >> P;
  MODVAL = P;
  REP(i, P) {
    cin >> as[i];
    if (as[i] == 1) {
      ans[0] += 1;
      mint cur = 1;
      REP(j, P) {
        ans[P - 1 - j] -= CMB(P - 1, j) * cur;
        cur *= -i;
      }
    }
  }
  REP(i, P) {
    cout << ans[i];
    if (i == P - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }

  return 0;
}