Problem p02950 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \
  do {                                                                         \
  } while (0)
#define dump(x)                                                                \
  do {                                                                         \
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long int;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;

// struct edge {int from, to, cost;};

constexpr double EPS = 1e-10;
// constexpr double PI  = acos(-1.0);
// constexpr int INF = INT_MAX;
constexpr int MOD = 1'000'000'007;
// inline void modAdd(LL &l, LL &r) {l = (l + r) % MOD;}

template <class T> inline T sqr(T x) { return x * x; }

long long power(long long x, long long n, long long mod = 1000000007) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return power(x * x % mod, n / 2, mod);
  else
    return x * power(x, n - 1, mod) % mod;
}

void solve() {
  int p;
  cin >> p;
  VI a(p);
  REP(i, 0, p) cin >> a[i];
  VVI r(p, VI(p));
  VI g(p);
  REP(i, 1, p) {
    RREP(j, 1, p) {
      if ((i * j) % p == p - 1) {
        g[i] = j;
        break;
      }
    }
  }
  REP(i, 0, p) {
    REP(j, 0, p) { r[i][j] = g[power(i, j, p)]; }
    if (i)
      r[i][0] = 0;
  }
  r[0][0] = 1;
  r[0][p - 1] = p - 1;
  /*
  REP(i,0,p) {
      REP(j,0,p) cout << r[i][j] << " ";
      cout << endl;
  }
  */
  VI ans(p);
  REP(i, 0, p) {
    if (a[i] != 1)
      continue;
    REP(j, 0, p) ans[j] = (ans[j] + r[i][j]) % p;
  }
  REP(i, 0, p) cout << ans[i] << " ";
  cout << endl;
}

int main() {
  solve();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \
  do {                                                                         \
  } while (0)
#define dump(x)                                                                \
  do {                                                                         \
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long int;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;

// struct edge {int from, to, cost;};

constexpr double EPS = 1e-10;
// constexpr double PI  = acos(-1.0);
// constexpr int INF = INT_MAX;
constexpr int MOD = 1'000'000'007;
// inline void modAdd(LL &l, LL &r) {l = (l + r) % MOD;}

template <class T> inline T sqr(T x) { return x * x; }

long long power(long long x, long long n, long long mod = 1000000007) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return power(x * x % mod, n / 2, mod);
  else
    return x * power(x, n - 1, mod) % mod;
}

void solve() {
  int p;
  cin >> p;
  VI a(p);
  REP(i, 0, p) cin >> a[i];
  VVI r(p, VI(p));
  VI g(p);
  REP(i, 1, p) {
    RREP(j, 1, p) {
      if ((i * j) % p == p - 1) {
        g[i] = j;
        break;
      }
    }
  }
  REP(i, 0, p) {
    int mul = 1;
    REP(j, 0, p) {
      r[i][j] = g[mul];
      mul *= i;
      mul %= p;
    }
    if (i)
      r[i][0] = 0;
  }
  r[0][0] = 1;
  r[0][p - 1] = p - 1;
  /*
  REP(i,0,p) {
      REP(j,0,p) cout << r[i][j] << " ";
      cout << endl;
  }
  */
  VI ans(p);
  REP(i, 0, p) {
    if (a[i] != 1)
      continue;
    REP(j, 0, p) ans[j] = (ans[j] + r[i][j]) % p;
  }
  REP(i, 0, p) cout << ans[i] << " ";
  cout << endl;
}

int main() {
  solve();

  return 0;
}