prithivMLmods/Coder-Stat huggingface dataset:
Problem p02949 (Python)

Buggy submission (status: Runtime Error):
from collections import defaultdict, deque

N, M, P = map(int, input().split())
G = defaultdict(lambda: defaultdict(lambda: 10000000))
G_rev = defaultdict(lambda: defaultdict(lambda: 10000000))

for i in range(1, N + 1):
    G[i] = dict()
    G_rev[i] = dict()

for _ in range(M):
    A, B, C = map(int, input().split())
    G[A][B] = min(G[A][B], P - C)
    G_rev[B][A] = 1

reachable_N = [0] * (N + 1)
reachable_N[N] = 1
queue = deque([N])

while len(queue) > 0:
    p = queue.popleft()
    for q in G_rev[p].keys():
        if reachable_N[q] == 0:
            reachable_N[q] = 1
            queue.append(q)

reachable_1 = [0] * (N + 1)
reachable_1[1] = 1
queue = deque([1])

while len(queue) > 0:
    p = queue.popleft()
    for q in G[p].keys():
        if reachable_1[q] == 0:
            reachable_1[q] = 1
            queue.append(q)

reachable = [reachable_1[i] * reachable_N[i] for i in range(N + 1)]

dist = [10000000000 for i in range(N + 1)]
res = 0
dist[1] = 0
queue = deque([1])
cnt = 0

update = True

while update:
    update = False
    for p in range(1, N + 1):
        if reachable[p] == 0:
            continue
        for q in G[p].keys():
            if reachable[q] == 0:
                pass
            elif dist[p] + G[p][q] < dist[q]:
                dist[q] = dist[p] + G[p][q]
                update = True
    cnt += 1
    if cnt > N:
        res = -1
        break

if res == -1:
    print(res)
else:
    print(max(0, -dist[N]))


Fixed submission:
from collections import defaultdict, deque

N, M, P = map(int, input().split())
G = defaultdict(lambda: defaultdict(lambda: 10000000))
G_rev = defaultdict(lambda: defaultdict(lambda: 10000000))

for _ in range(M):
    A, B, C = map(int, input().split())
    G[A][B] = min(G[A][B], P - C)
    G_rev[B][A] = 1

reachable_N = [0] * (N + 1)
reachable_N[N] = 1
queue = deque([N])

while len(queue) > 0:
    p = queue.popleft()
    for q in G_rev[p].keys():
        if reachable_N[q] == 0:
            reachable_N[q] = 1
            queue.append(q)

reachable_1 = [0] * (N + 1)
reachable_1[1] = 1
queue = deque([1])

while len(queue) > 0:
    p = queue.popleft()
    for q in G[p].keys():
        if reachable_1[q] == 0:
            reachable_1[q] = 1
            queue.append(q)

reachable = [reachable_1[i] * reachable_N[i] for i in range(N + 1)]

dist = [10000000000 for i in range(N + 1)]
res = 0
dist[1] = 0
queue = deque([1])
cnt = 0

update = True

while update:
    update = False
    for p in range(1, N + 1):
        if reachable[p] == 0:
            continue
        for q in G[p].keys():
            if reachable[q] == 0:
                pass
            elif dist[p] + G[p][q] < dist[q]:
                dist[q] = dist[p] + G[p][q]
                update = True
    cnt += 1
    if cnt > N:
        res = -1
        break

if res == -1:
    print(res)
else:
    print(max(0, -dist[N]))
Problem p02949 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int MAXN = 2500;
const long long INF = 1e17;
int N, M, PF, vis[MAXN];
int Ok[MAXN], cnt[MAXN];
long long dist[MAXN], Ans;
vector<int> P[MAXN], T[MAXN], F[MAXN];
queue<int> Q;
void SPFA(int u, int val) {
  for (int i = 1; i <= N; i++)
    dist[i] = INF;
  while (!Q.empty())
    Q.pop();
  dist[u] = val;
  Q.push(u);
  cnt[u]++;
  while (!Q.empty()) {
    u = Q.front();
    Q.pop();
    int size = P[u].size();
    for (int i = 0; i < size; i++) {
      int v = P[u][i];
      if (dist[v] > dist[u] + T[u][i]) {
        dist[v] = dist[u] + T[u][i];
        if (cnt[v] > 30 * N + 5) {
          Ok[v] = 1;
          continue;
        }
        cnt[v]++;
        Q.push(v);
      }
    }
  }
  if (dist[N] == INF)
    Ans = -1;
  else
    Ans = max(0 * 1LL, -dist[N]);
}
void FA(int u) {
  int size = F[u].size();
  vis[u] = 1;
  if (Ok[u])
    Ans = -1;
  for (int i = 0; i < size; i++) {
    int v = F[u][i];
    if (vis[v])
      continue;
    FA(v);
  }
}
int main() {
  scanf("%d%d%d", &N, &M, &PF);
  for (int i = 1, x, y, z; i <= M; i++) {
    scanf("%d%d%d", &x, &y, &z);
    P[x].push_back(y);
    F[y].push_back(x);
    T[x].push_back(PF - z);
  }
  SPFA(1, 0);
  FA(N);
  printf("%lld\n", Ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int MAXN = 2500;
const long long INF = 1e17;
int N, M, PF, vis[MAXN];
int Ok[MAXN], cnt[MAXN];
long long dist[MAXN], Ans;
vector<int> P[MAXN], T[MAXN], F[MAXN];
queue<int> Q;
void SPFA(int u, int val) {
  for (int i = 1; i <= N; i++)
    dist[i] = INF;
  while (!Q.empty())
    Q.pop();
  dist[u] = val;
  Q.push(u);
  cnt[u]++;
  while (!Q.empty()) {
    u = Q.front();
    Q.pop();
    int size = P[u].size();
    for (int i = 0; i < size; i++) {
      int v = P[u][i];
      if (dist[v] > dist[u] + T[u][i]) {
        dist[v] = dist[u] + T[u][i];
        if (cnt[v] > 8 * N + 5) {
          Ok[v] = 1;
          continue;
        }
        cnt[v]++;
        Q.push(v);
      }
    }
  }
  if (dist[N] == INF)
    Ans = -1;
  else
    Ans = max(0 * 1LL, -dist[N]);
}
void FA(int u) {
  int size = F[u].size();
  vis[u] = 1;
  if (Ok[u])
    Ans = -1;
  for (int i = 0; i < size; i++) {
    int v = F[u][i];
    if (vis[v])
      continue;
    FA(v);
  }
}
int main() {
  scanf("%d%d%d", &N, &M, &PF);
  for (int i = 1, x, y, z; i <= M; i++) {
    scanf("%d%d%d", &x, &y, &z);
    P[x].push_back(y);
    F[y].push_back(x);
    T[x].push_back(PF - z);
  }
  SPFA(1, 0);
  FA(N);
  printf("%lld\n", Ans);
}
Problem p02949 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
typedef long long ll;
const int INF = 1001001001;

vector<int> to[2505];
vector<int> rto[2505];
bool reachableFrom1[2505];
bool reachableToN[2505];
bool ok[2505];

void dfs(int v) {
  if (reachableFrom1[v])
    return;
  reachableFrom1[v] = true;
  for (int u : to[v]) {
    dfs(u);
  }
}
void rdfs(int v) {
  if (reachableToN[v])
    return;
  reachableToN[v] = true;
  for (int u : rto[v]) {
    rdfs(u);
  }
}

int main() {
  int n, m, p;
  cin >> n >> m >> p;
  vector<tuple<int, int, int>> edges;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    c -= p;
    c = -c;
    edges.emplace_back(a, b, c);
    to[a].push_back(b);
    rto[b].push_back(a);
  }
  dfs(0);
  rdfs(n - 1);
  rep(i, n) ok[i] = reachableFrom1[i] & reachableToN[i];
  { // bellman-ford
    vector<int> d(n, INF);
    d[0] = 0;
    bool upd = true;
    int step = 0;
    while (upd) {
      upd = false;
      rep(i, n) {
        int a, b, c;
        tie(a, b, c) = edges[i];
        if (!ok[a])
          continue;
        if (!ok[b])
          continue;
        int newD = d[a] + c;
        if (newD < d[b]) {
          upd = true;
          d[b] = newD;
        }
      }
      step++;
      if (step > n) {
        puts("-1");
        return 0;
      }
    }
    int ans = -d[n - 1];
    ans = max(ans, 0);
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
typedef long long ll;
const int INF = 1001001001;

vector<int> to[2505];
vector<int> rto[2505];
bool reachableFrom1[2505];
bool reachableToN[2505];
bool ok[2505];

void dfs(int v) {
  if (reachableFrom1[v])
    return;
  reachableFrom1[v] = true;
  for (int u : to[v]) {
    dfs(u);
  }
}
void rdfs(int v) {
  if (reachableToN[v])
    return;
  reachableToN[v] = true;
  for (int u : rto[v]) {
    rdfs(u);
  }
}

int main() {
  int n, m, p;
  cin >> n >> m >> p;
  vector<tuple<int, int, int>> edges;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    c -= p;
    c = -c;
    edges.emplace_back(a, b, c);
    to[a].push_back(b);
    rto[b].push_back(a);
  }
  dfs(0);
  rdfs(n - 1);
  rep(i, n) ok[i] = reachableFrom1[i] & reachableToN[i];
  { // bellman-ford
    vector<int> d(n, INF);
    d[0] = 0;
    bool upd = true;
    int step = 0;
    while (upd) {
      upd = false;
      rep(i, m) {
        int a, b, c;
        tie(a, b, c) = edges[i];
        if (!ok[a])
          continue;
        if (!ok[b])
          continue;
        int newD = d[a] + c;
        if (newD < d[b]) {
          upd = true;
          d[b] = newD;
        }
      }
      step++;
      if (step > n) {
        puts("-1");
        return 0;
      }
    }
    int ans = -d[n - 1];
    ans = max(ans, 0);
    cout << ans << endl;
  }
  return 0;
}
Problem p02949 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define vi vector<ll>
#define vvi vector<vi>
#define llINF 1e18
#define ll long long
using namespace std;
bool vis[2222];
struct Edge {
  ll to, cost;
};
const ll MAX_V = 2010;
vector<vector<Edge>> E(MAX_V);
vi dist(MAX_V, llINF);
bool bellman_ford(ll s) {
  queue<ll> que;
  que.push(s);
  dist[s] = 0;
  for (int i = 0; i < MAX_V; i++) {
    ll n = que.size();
    bool used[MAX_V] = {};
    for (int j = 0; j < n; j++) {
      ll now = que.front();
      que.pop();
      for (int k = 0; k < E[now].size(); k++) {
        Edge e = E[now][k];
        if (dist[e.to] > dist[now] + e.cost) {
          dist[e.to] = dist[now] + e.cost;
          if (i == MAX_V - 1)
            if (vis[e.to])
              return true;
          if (used[e.to] == false)
            que.push(e.to);
          used[e.to] = true;
        }
      }
    }
  }
  return false;
}
vvi E2(MAX_V);
int main() {
  ll n, m, p;
  cin >> n >> m >> p;
  for (int i = 0; i < m; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    E[--a].push_back({--b, -(c - p)});
    E2[b].push_back(a);
  }
  queue<ll> que;
  que.push(n - 1);
  vis[n - 1] = true;
  while (que.size()) {
    ll hoge = que.front();
    que.pop();
    for (auto a : E2[hoge])
      if (vis[a] == false) {
        que.push(a);
        vis[a] = true;
      }
  }
  if (bellman_ford(0) == false) {
    cout << max(0LL, -dist[n - 1]) << endl;
  } else
    cout << -1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define vi vector<ll>
#define vvi vector<vi>
#define llINF 1e18
#define ll long long
using namespace std;
bool vis[2222];
struct Edge {
  ll to, cost;
};
const ll MAX_V = 2510;
vector<vector<Edge>> E(MAX_V);
vi dist(MAX_V, llINF);
bool bellman_ford(ll s) {
  queue<ll> que;
  que.push(s);
  dist[s] = 0;
  for (int i = 0; i < MAX_V; i++) {
    ll n = que.size();
    bool used[MAX_V] = {};
    for (int j = 0; j < n; j++) {
      ll now = que.front();
      que.pop();
      for (int k = 0; k < E[now].size(); k++) {
        Edge e = E[now][k];
        if (dist[e.to] > dist[now] + e.cost) {
          dist[e.to] = dist[now] + e.cost;
          if (i == MAX_V - 1)
            if (vis[e.to])
              return true;
          if (used[e.to] == false)
            que.push(e.to);
          used[e.to] = true;
        }
      }
    }
  }
  return false;
}
vvi E2(MAX_V);
int main() {
  ll n, m, p;
  cin >> n >> m >> p;
  for (int i = 0; i < m; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    E[--a].push_back({--b, -(c - p)});
    E2[b].push_back(a);
  }
  queue<ll> que;
  que.push(n - 1);
  vis[n - 1] = true;
  while (que.size()) {
    ll hoge = que.front();
    que.pop();
    for (auto a : E2[hoge])
      if (vis[a] == false) {
        que.push(a);
        vis[a] = true;
      }
  }
  if (bellman_ford(0) == false) {
    cout << max(0LL, -dist[n - 1]) << endl;
  } else
    cout << -1 << endl;
}
Problem p02949 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if (!(p))
#define until(p) while (!(p))

using ll = std::int64_t;

constexpr int INF = 1001001001;
int N, M, P;
int A[2600], B[2600], C[2600];
std::vector<int> G[2600], rG[2600];
bool used[2600], re1[2600], reN[2600];
int d[2600];

int calc() {
  std::fill(d, d + N, INF);
  d[0] = 0;

  for (int i = 1; i <= N; ++i) {
    bool updated = false;

    for (int j = 0; j < M; ++j) {
      if (!(re1[A[j]] && reN[B[j]])) {
        continue;
      }

      if (d[B[j]] > d[A[j]] + C[j]) {
        d[B[j]] = d[A[j]] + C[j];
        updated = true;
      }
    }

    if (updated && i == N) {
      return -1;
    }
  }

  return std::max(-d[N - 1], 0);
}

void dfs(int v, std::vector<int> (&G)[2600], bool (&re)[2600]) {
  used[v] = true;
  re[v] = true;

  for (int w : G[v]) {
    unless(used[w]) { dfs(w, G, re); }
  }
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  std::cin >> N >> M >> P;

  for (int i = 0; i < M; ++i) {
    std::cin >> A[i] >> B[i] >> C[i];

    A[i] -= 1;
    B[i] -= 1;
    C[i] = P - C[i];

    G[A[i]].emplace_back(B[i]);
    rG[B[i]].emplace_back(A[i]);
  }

  dfs(0, G, re1);
  memset(used, 0, sizeof(used));
  dfs(N - 1, rG, reN);

  std::cout << calc() << std::endl;
}


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

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if (!(p))
#define until(p) while (!(p))

using ll = std::int64_t;

constexpr int INF = 1001001001;
int N, M, P;
int A[5100], B[5100], C[5100];
std::vector<int> G[2600], rG[2600];
bool used[2600], re1[2600], reN[2600];
int d[2600];

int calc() {
  std::fill(d, d + N, INF);
  d[0] = 0;

  for (int i = 1; i <= N; ++i) {
    bool updated = false;

    for (int j = 0; j < M; ++j) {
      if (!(re1[A[j]] && reN[B[j]])) {
        continue;
      }

      if (d[B[j]] > d[A[j]] + C[j]) {
        d[B[j]] = d[A[j]] + C[j];
        updated = true;
      }
    }

    if (updated && i == N) {
      return -1;
    }
  }

  return std::max(-d[N - 1], 0);
}

void dfs(int v, std::vector<int> (&G)[2600], bool (&re)[2600]) {
  used[v] = true;
  re[v] = true;

  for (int w : G[v]) {
    unless(used[w]) { dfs(w, G, re); }
  }
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  std::cin >> N >> M >> P;

  for (int i = 0; i < M; ++i) {
    std::cin >> A[i] >> B[i] >> C[i];

    A[i] -= 1;
    B[i] -= 1;
    C[i] = P - C[i];

    G[A[i]].emplace_back(B[i]);
    rG[B[i]].emplace_back(A[i]);
  }

  dfs(0, G, re1);
  memset(used, 0, sizeof(used));
  dfs(N - 1, rG, reN);

  std::cout << calc() << std::endl;
}
Problem p02949 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define LLINF 9223372036854775807
#define MOD ll(1e9 + 7)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl

#define MAX_DIST 1LL << 50
#define NMAX 10
#define MMAX 5200

vector<ll> dist(NMAX, 0);
bool negative[NMAX] = {
    false,
};
ll a[MMAX];
ll b[MMAX];
ll c[MMAX];

void bellmanford(ll start, ll v, ll e, ll p) {
  for (int i = 0; i < NMAX; i++) {
    dist[i] = MAX_DIST;
  }
  dist[start] = 0;

  for (int i = 0; i < v - 1; i++) {
    for (int j = 0; j < e; j++) {
      if (dist[a[j]] == MAX_DIST) {
        continue;
      }
      if (dist[b[j]] > dist[a[j]] + c[j] + p) {
        dist[b[j]] = dist[a[j]] + c[j] + p;
      }
    }
  }
  for (int i = 0; i < v; i++) {
    for (int j = 0; j < e; j++) {
      if (dist[a[j]] == MAX_DIST) {
        continue;
      }
      if (dist[b[j]] > dist[a[j]] + c[j] + p) {
        dist[b[j]] = dist[a[j]] + c[j] + p;
        negative[b[j]] = true;
      }
      if (negative[a[j]]) {
        negative[b[j]] = true;
      }
    }
  }
}

int main() {
  ll n, m, p;
  cin >> n >> m >> p;

  for (int i = 0; i < m; i++) {
    ll ta, tb, tc;
    cin >> ta >> tb >> tc;
    ta--;
    tb--;
    a[i] = ta;
    b[i] = tb;
    c[i] = -tc;
  }

  bellmanford(0, n, m, p);
  ll ans = -dist[n - 1];
  if (negative[n - 1]) {
    cout << -1 << endl;
  } else {
    cout << max(ans, 0LL) << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define LLINF 9223372036854775807
#define MOD ll(1e9 + 7)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl

#define MAX_DIST 1LL << 50
#define NMAX 3000
#define MMAX 5200

vector<ll> dist(NMAX, 0);
bool negative[NMAX] = {
    false,
};
ll a[MMAX];
ll b[MMAX];
ll c[MMAX];

void bellmanford(ll start, ll v, ll e, ll p) {
  for (int i = 0; i < NMAX; i++) {
    dist[i] = MAX_DIST;
  }
  dist[start] = 0;

  for (int i = 0; i < v - 1; i++) {
    for (int j = 0; j < e; j++) {
      if (dist[a[j]] == MAX_DIST) {
        continue;
      }
      if (dist[b[j]] > dist[a[j]] + c[j] + p) {
        dist[b[j]] = dist[a[j]] + c[j] + p;
      }
    }
  }
  for (int i = 0; i < v; i++) {
    for (int j = 0; j < e; j++) {
      if (dist[a[j]] == MAX_DIST) {
        continue;
      }
      if (dist[b[j]] > dist[a[j]] + c[j] + p) {
        dist[b[j]] = dist[a[j]] + c[j] + p;
        negative[b[j]] = true;
      }
      if (negative[a[j]]) {
        negative[b[j]] = true;
      }
    }
  }
}

int main() {
  ll n, m, p;
  cin >> n >> m >> p;

  for (int i = 0; i < m; i++) {
    ll ta, tb, tc;
    cin >> ta >> tb >> tc;
    ta--;
    tb--;
    a[i] = ta;
    b[i] = tb;
    c[i] = -tc;
  }

  bellmanford(0, n, m, p);
  ll ans = -dist[n - 1];
  if (negative[n - 1]) {
    cout << -1 << endl;
  } else {
    cout << max(ans, 0LL) << endl;
  }

  return 0;
}
Problem p02949 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAX 5050
#define INF (1LL << 60LL)
typedef pair<int, int> P;
int V;
struct edge2 {
  int from, to, cost;
  edge2() {}
  edge2(int from, int to, int cost) : from(from), to(to), cost(cost) {}
};
edge2 es[MAX];
int d[MAX];
int ok[MAX];
int used[MAX];
int shortest_path(int s, int e) {
  for (int i = 0; i < V; i++)
    d[i] = INF;
  d[s] = 0;
  for (int j = 0; j < V; j++) {
    bool update = 0;
    for (int i = 0; i < e; i++) {
      edge2 e = es[i];
      if (!ok[e.from] || !ok[e.to])
        continue;
      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = 1;
      }
    }
    if (!update)
      break;
    if (j == V - 1) {
      cout << -1 << endl;
      exit(0);
    }
  }
  return d[V - 1];
}

vector<int> G[1111];
void dfs2(int v) {
  if (used[v])
    return;
  used[v] = 1;
  for (int u : G[v]) {
    dfs2(u);
  }
}

signed main() {
  int v, e, p;
  cin >> v >> e >> p;
  for (int i = 0; i < e; i++) {
    int u, v, d;
    cin >> u >> v >> d;
    u--;
    v--;
    G[u].push_back(v);
    es[i] = edge2(u, v, p - d);
  }
  for (int i = 0; i < v; i++) {
    memset(used, 0, sizeof(used));
    dfs2(i);
    ok[i] = used[v - 1];
  }
  V = v;
  cout << max<int>(-shortest_path(0, e), 0) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAX 5050
#define INF (1LL << 60LL)
typedef pair<int, int> P;
int V;
struct edge2 {
  int from, to, cost;
  edge2() {}
  edge2(int from, int to, int cost) : from(from), to(to), cost(cost) {}
};
edge2 es[MAX];
int d[MAX];
int ok[MAX];
int used[MAX];
int shortest_path(int s, int e) {
  for (int i = 0; i < V; i++)
    d[i] = INF;
  d[s] = 0;
  for (int j = 0; j < V; j++) {
    bool update = 0;
    for (int i = 0; i < e; i++) {
      edge2 e = es[i];
      if (!ok[e.from] || !ok[e.to])
        continue;
      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = 1;
      }
    }
    if (!update)
      break;
    if (j == V - 1) {
      cout << -1 << endl;
      exit(0);
    }
  }
  return d[V - 1];
}

vector<int> G[MAX];
void dfs2(int v) {
  if (used[v])
    return;
  used[v] = 1;
  for (int u : G[v]) {
    dfs2(u);
  }
}

signed main() {
  int v, e, p;
  cin >> v >> e >> p;
  for (int i = 0; i < e; i++) {
    int u, v, d;
    cin >> u >> v >> d;
    u--;
    v--;
    G[u].push_back(v);
    es[i] = edge2(u, v, p - d);
  }
  for (int i = 0; i < v; i++) {
    memset(used, 0, sizeof(used));
    dfs2(i);
    ok[i] = used[v - 1];
  }
  V = v;
  cout << max<int>(-shortest_path(0, e), 0) << endl;
  return 0;
}
Problem p02949 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 5e18;

int main() {
  ll n, m, p;
  cin >> n >> m >> p;
  vec d(n, -inf);
  d[0] = 0;
  vec a(n);
  vec b(n);
  vec c(n);
  for (ll i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> c[i];
    a[i]--;
    b[i]--;
    c[i] -= p;
  }
  for (ll i = 0; i < 2 * n; i++) {
    for (ll j = 0; j < m; j++) {
      if (d[a[j]] == -inf)
        continue;
      if (d[a[j]] + c[j] > d[b[j]]) {
        if (i < n - 1) {
          d[b[j]] = d[a[j]] + c[j];
        } else {
          d[b[j]] = inf;
        }
      }
    }
  }
  if (d[n - 1] == inf) {
    cout << -1 << endl;
  } else {
    cout << max(d[n - 1], 0LL) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 5e18;

int main() {
  ll n, m, p;
  cin >> n >> m >> p;
  vec d(n, -inf);
  d[0] = 0;
  vec a(m);
  vec b(m);
  vec c(m);
  for (ll i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> c[i];
    a[i]--;
    b[i]--;
    c[i] -= p;
  }
  for (ll i = 0; i < 2 * n; i++) {
    for (ll j = 0; j < m; j++) {
      if (d[a[j]] == -inf)
        continue;
      if (d[a[j]] + c[j] > d[b[j]]) {
        if (i < n - 1) {
          d[b[j]] = d[a[j]] + c[j];
        } else {
          d[b[j]] = inf;
        }
      }
    }
  }
  if (d[n - 1] == inf) {
    cout << -1 << endl;
  } else {
    cout << max(d[n - 1], 0LL) << endl;
  }
}
Problem p02949 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#include <cassert>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
const long long mod = 998244353;
const long long inf = 1ll << 61;
const double eps = 0.000000000001;
typedef pair<int, int> P;
typedef pair<P, int> PP;
struct edge {
  int from, to, cost;
};
edge es[5005];
edge res[5005];
int d[2505];
int df[2505];
int V, E;
void bel(int s) {
  rep(i, V) d[i] = inf;
  d[s] = 0;
  while (1) {
    bool upd = false;
    rep(i, E) {
      edge e = res[i];
      if (d[e.from] != inf && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        upd = true;
      }
    }
    if (!upd)
      break;
  }
}

bool findloop(int s, int t) {
  fill(df, df + V, inf);
  df[s] = 0;
  rep(i, 2 * V) {
    rep(j, E) {
      edge e = es[j];
      if (df[e.from] != inf && df[e.to] > df[e.from] + e.cost) {
        df[e.to] = df[e.from] + e.cost;
        if (i >= V - 1 && e.to == t)
          return true;
      }
    }
  }
  return false;
}

signed main() {
  int p;
  cin >> V >> E >> p;
  rep(i, E) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    c -= p;
    es[i] = {a, b, -c};
  }
  if (findloop(0, V - 1))
    cout << -1 << endl;
  else {
    vector<edge> v;
    rep(i, E) {
      if (es[i].from != es[i].to)
        v.push_back(es[i]);
    }
    rep(i, v.size()) res[i] = v[i];
    E = v.size();
    bel(0);
    cout << max(0ll, -d[V - 1]) << endl;
  }
}

Fixed submission:
#include "bits/stdc++.h"
#include <cassert>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
const long long mod = 998244353;
const long long inf = 1ll << 61;
const double eps = 0.000000000001;
typedef pair<int, int> P;
typedef pair<P, int> PP;
struct edge {
  int from, to, cost;
};
edge es[5005];
edge res[5005];
int d[2505];
int df[2505];
int V, E;
void bel(int s) {
  rep(i, V) d[i] = inf;
  d[s] = 0;
  while (1) {
    bool upd = false;
    rep(i, E) {
      edge e = res[i];
      if (d[e.from] != inf && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        upd = true;
      }
    }
    if (!upd)
      break;
  }
}

bool findloop(int s, int t) {
  fill(df, df + V, inf);
  df[s] = 0;
  rep(i, 2 * V) {
    rep(j, E) {
      edge e = es[j];
      if (df[e.from] != inf && df[e.to] > df[e.from] + e.cost) {
        df[e.to] = df[e.from] + e.cost;
        if (i >= V - 1 && e.to == t)
          return true;
      }
    }
  }
  return false;
}

signed main() {
  int p;
  cin >> V >> E >> p;
  rep(i, E) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    c -= p;
    es[i] = {a, b, -c};
  }
  if (findloop(0, V - 1))
    cout << -1 << endl;
  else {
    cout << max(0ll, -df[V - 1]) << endl;
  }
}
Problem p02949 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
typedef pair<int, int> pii;
queue<int> q;
int ver[3009], edge[5009 * 2], nxt[5009 * 2], head[5009 * 2], tot = 0;
ll d[3009];
int v[3009];
void add(int x, int y, int z) {
  ver[++tot] = y;
  edge[tot] = z;
  nxt[tot] = head[x];
  head[x] = tot;
}
int ban[3009], cntru[3009];
int dfsv[3009];
int n, m, p;
void spfa() {
  memset(d, 0xcf, sizeof(d));
  memset(v, 0, sizeof(v));
  d[1] = 0;
  v[1] = 1;
  q.push(1);
  while (q.size()) {
    int x = q.front();
    q.pop();
    v[x] = 0;
    for (int i = head[x]; i; i = nxt[i]) {
      int y = ver[i], z = edge[i];
      if (ban[y])
        continue;
      if (d[y] < d[x] + z) {
        d[y] = d[x] + z;
        if (!v[y]) {
          ++cntru[y];
          if (cntru[y] >= n) {
            puts("-1");
            exit(0);
          }
          q.push(y), v[y] = 1;
        }
      }
    }
  }
  cout << max(0ll, d[n]) << endl;
}
int dfs(int x) {
  if (dfsv[x])
    return 0;
  if (x == n)
    return 1;
  dfsv[x] = 1;
  for (int i = head[x]; i; i = nxt[i]) {
    if (dfs(ver[i]))
      return 1;
  }
  return 0;
}
int main() {
  scanf("%d%d%d", &n, &m, &p);
  for (int i = 1; i <= m; i++) {
    int u, vx, vv;
    scanf("%d%d%d", &u, &vx, &vv);
    add(u, vx, vv - p);
  }
  for (int i = 1; i <= n; i++) {
    memset(dfsv, 0, sizeof(dfsv));
    if (!dfs(i))
      ban[i] = 1;
  }
  spfa();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
typedef pair<int, int> pii;
queue<int> q;
int ver[5009 * 2], edge[5009 * 2], nxt[5009 * 2], head[3009], tot = 0;
ll d[3009];
int v[3009];
void add(int x, int y, int z) {
  ver[++tot] = y;
  edge[tot] = z;
  nxt[tot] = head[x];
  head[x] = tot;
}
int ban[3009], cntru[3009];
int dfsv[3009];
int n, m, p;
void spfa() {
  memset(d, 0xcf, sizeof(d));
  memset(v, 0, sizeof(v));
  d[1] = 0;
  v[1] = 1;
  q.push(1);
  while (q.size()) {
    int x = q.front();
    q.pop();
    v[x] = 0;
    for (int i = head[x]; i; i = nxt[i]) {
      int y = ver[i], z = edge[i];
      if (ban[y])
        continue;
      if (d[y] < d[x] + z) {
        d[y] = d[x] + z;
        if (!v[y]) {
          ++cntru[y];
          if (cntru[y] >= n) {
            puts("-1");
            exit(0);
          }
          q.push(y), v[y] = 1;
        }
      }
    }
  }
  cout << max(0ll, d[n]) << endl;
}
int dfs(int x) {
  if (dfsv[x])
    return 0;
  if (x == n)
    return 1;
  dfsv[x] = 1;
  for (int i = head[x]; i; i = nxt[i]) {
    if (dfs(ver[i]))
      return 1;
  }
  return 0;
}
int main() {
  scanf("%d%d%d", &n, &m, &p);
  for (int i = 1; i <= m; i++) {
    int u, vx, vv;
    scanf("%d%d%d", &u, &vx, &vv);
    add(u, vx, vv - p);
  }
  for (int i = 1; i <= n; i++) {
    memset(dfsv, 0, sizeof(dfsv));
    if (!dfs(i))
      ban[i] = 1;
  }
  spfa();
  return 0;
}
Problem p02950 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

int P;

ll powmod(ll n, ll k) {
  ll r = 1, t = n % P;
  for (; k != 0; k /= 2) {
    if (k & 1)
      r = r * t % P;
    t = t * t % P;
  }
  return r;
}
ll inv(ll n) { return powmod(n, P - 2); }

vector<int> f0, f1;
void init(int n) {
  f0.resize(n + 1);
  f0[0] = 1;
  for (int i = 1; i <= n; i++) {
    f0[i] = (ll)f0[i - 1] * i % P;
  }
  f1.resize(n + 1);
  f1[n] = inv(f0[n]);
  for (int i = n; i > 0; i--) {
    f1[i - 1] = (ll)f1[i] * i % P;
  }
}
ll fact(int k) { return f0[k]; }
ll comb(int n, int k) {
  if (n < k || k < 0)
    return 0;
  return (ll)f0[n] * f1[k] % P * f1[n - k] % P;
}

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

  cin >> P;

  init(P - 1);

  vector<ll> b(P);
  for (int i = 0; i < P; i++) {
    int a;
    cin >> a;

    if (a) {
      b[0] += 1;
      int s = 1;
      for (int j = 0; j < P; j++) {
        b[P - 1 - j] -= comb(P - 1, j) * powmod(i, j) * s;
        s *= -1;
      }
    }
  }

  for (int j = 0; j < P; j++) {
    ll t = b[j] % P;
    if (t < 0)
      t += P;
    cout << t << " \n"[j == P - 1];
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

int P;

ll powmod(ll n, ll k) {
  ll r = 1, t = n % P;
  for (; k != 0; k /= 2) {
    if (k & 1)
      r = r * t % P;
    t = t * t % P;
  }
  return r;
}
ll inv(ll n) { return powmod(n, P - 2); }

vector<int> f0, f1;
void init(int n) {
  f0.resize(n + 1);
  f0[0] = 1;
  for (int i = 1; i <= n; i++) {
    f0[i] = (ll)f0[i - 1] * i % P;
  }
  f1.resize(n + 1);
  f1[n] = inv(f0[n]);
  for (int i = n; i > 0; i--) {
    f1[i - 1] = (ll)f1[i] * i % P;
  }
}
ll fact(int k) { return f0[k]; }
ll comb(int n, int k) {
  if (n < k || k < 0)
    return 0;
  return (ll)f0[n] * f1[k] % P * f1[n - k] % P;
}

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

  cin >> P;

  init(P - 1);

  vector<ll> b(P);
  for (int i = 0; i < P; i++) {
    int a;
    cin >> a;

    if (a) {
      b[0] += 1;
      int s = 1;
      for (int j = 0; j < P; j++) {
        b[P - 1 - j] -= comb(P - 1, j) * s;
        s *= -i;
        s %= P;
      }
    }
  }

  for (int j = 0; j < P; j++) {
    ll t = b[j] % P;
    if (t < 0)
      t += P;
    cout << t << " \n"[j == P - 1];
  }

  return 0;
}
Problem p02950 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"

using namespace std;
long long MOD = (long long)1e9 + 7;

long long quickpow(long long b, int e) {
  return e ? ((e & 1 ? b : 1) * quickpow((b * b) % MOD, e >> 1)) % MOD : 1;
}

int p, a[3000], fac[3000], inv[3000], b[3000], cnt, dat[3000][3000];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> p;
  MOD = p;
  for (int i = 0; i < p; i++) {
    cin >> a[i];
    if (a[i] == 1)
      cnt++;
  }
  fac[0] = 1;
  for (int i = 1; i < p; i++) {
    fac[i] = i * fac[i - 1];
    fac[i] %= p;
  }
  for (int i = 0; i < p; i++) {
    inv[i] = quickpow(fac[i], p - 2);
    for (int j = 0; j < p; j++) {
      dat[i][j] = quickpow(i, j);
    }
  }
  for (int i = 0; i < p; i++) {
    int tmp = 0;
    for (int j = 0; j < p; j++) {
      if (a[j] == 0)
        continue;
      b[i] = fac[p - 1];
      b[i] *= inv[i];
      b[i] %= p;
      b[i] *= inv[p - 1 - i];
      b[i] %= p;
      b[i] *= dat[(p - j) % p][p - 1 - i];
      b[i] %= p;
      b[i] = p - b[i];
      b[i] %= p;
      tmp += b[i];
      tmp %= p;
    }
    b[i] = tmp;
  }
  b[0] += cnt;
  b[0] %= p;
  for (int i = 0; i < p; i++) {
    cout << b[i] << " ";
  }
  // system("pause");
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"

using namespace std;
long long MOD = (long long)1e9 + 7;

long long quickpow(long long b, int e) {
  return e ? ((e & 1 ? b : 1) * quickpow((b * b) % MOD, e >> 1)) % MOD : 1;
}

int p, a[3000], fac[3000], inv[3000], b[3000], cnt, dat[3000][3000];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> p;
  MOD = p;
  for (int i = 0; i < p; i++) {
    cin >> a[i];
    if (a[i] == 1)
      cnt++;
  }
  fac[0] = 1;
  for (int i = 1; i < p; i++) {
    fac[i] = i * fac[i - 1];
    fac[i] %= p;
  }
  for (int i = 0; i < p; i++) {
    inv[i] = quickpow(fac[i], p - 2);
    dat[i][0] = 1;
    for (int j = 1; j < p; j++) {
      dat[i][j] = dat[i][j - 1] * i;
      dat[i][j] %= p;
    }
  }
  for (int i = 0; i < p; i++) {
    int tmp = 0;
    for (int j = 0; j < p; j++) {
      if (a[j] == 0)
        continue;
      b[i] = fac[p - 1];
      b[i] *= inv[i];
      b[i] %= p;
      b[i] *= inv[p - 1 - i];
      b[i] %= p;
      b[i] *= dat[(p - j) % p][p - 1 - i];
      b[i] %= p;
      b[i] = p - b[i];
      b[i] %= p;
      tmp += b[i];
      tmp %= p;
    }
    b[i] = tmp;
  }
  b[0] += cnt;
  b[0] %= p;
  for (int i = 0; i < p; i++) {
    cout << b[i] << " ";
  }
  // system("pause");
  return 0;
}
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++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;

ll kai[200001];
ll mokai[200001];
ll p;

ll mod_pow(ll x, ll y) {
  ll ret = 1;
  while (y) {
    if (y & 1)
      ret = ret * x % p;
    x = x * x % p;
    y /= 2;
  }
  return ret;
}

void init(int _n) {
  kai[0] = 1;
  for (int i = 1; i <= _n; i++)
    kai[i] = kai[i - 1] * i % p;
  for (int i = 0; i <= _n; i++)
    mokai[i] = mod_pow(kai[i], p - 2);
}

ll conb(ll x, ll y) {
  ll z = x - y;
  ll ret = kai[x] * mokai[y] % p;
  ret = ret * mokai[z] % p;
  return ret;
}

ll c[3000];
ll prep[3000][3000];

int main() {
  cin >> p;
  init(3000);
  rep(i, p + 1) {
    prep[i][0] = 1;
    for (int j = 1; j <= p; j++)
      prep[i][j] = prep[i][j - 1] * i % p;
  }
  rep(i, p) {
    ll a;
    cin >> a;
    if (a == 0)
      continue;
    (c[0] += 1) %= p;
    rep(j, p) {
      (c[j] += -1 * conb(p - 1, p - 1 - j) * mod_pow(p - i, p - 1 - j) +
               p * p * p) %= p;
    }
  }
  rep(i, p) {
    cout << c[i];
    if (i != p - 1)
      cout << " ";
  }
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;

ll kai[200001];
ll mokai[200001];
ll p;

ll mod_pow(ll x, ll y) {
  ll ret = 1;
  while (y) {
    if (y & 1)
      ret = ret * x % p;
    x = x * x % p;
    y /= 2;
  }
  return ret;
}

void init(int _n) {
  kai[0] = 1;
  for (int i = 1; i <= _n; i++)
    kai[i] = kai[i - 1] * i % p;
  for (int i = 0; i <= _n; i++)
    mokai[i] = mod_pow(kai[i], p - 2);
}

ll conb(ll x, ll y) {
  ll z = x - y;
  ll ret = kai[x] * mokai[y] % p;
  ret = ret * mokai[z] % p;
  return ret;
}

ll c[3000];
ll prep[3000][3000];

int main() {
  cin >> p;
  init(3000);
  rep(i, p + 1) {
    prep[i][0] = 1;
    for (int j = 1; j <= p; j++)
      prep[i][j] = prep[i][j - 1] * i % p;
  }
  rep(i, p) {
    ll a;
    cin >> a;
    if (a == 0)
      continue;
    (c[0] += 1) %= p;
    rep(j, p) {
      (c[j] +=
       -1 * conb(p - 1, p - 1 - j) * prep[p - i][p - 1 - j] + p * p * p) %= p;
    }
  }
  rep(i, p) {
    cout << c[i];
    if (i != p - 1)
      cout << " ";
  }
  cout << endl;
}
Problem p02950 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
using ll = int64_t;

ll MODpow(ll n, ll m, ll mod) {
  ll result = 1;
  while (m) {
    if (m % 2 == 1) {
      result *= n;
      result %= mod;
    }

    m /= 2;
    n *= n;
    n %= mod;
  }

  return result;
}

class Combination {
public:
  Combination(ll max_num, ll mod) {
    fact_.resize(max_num + 1, 1);
    inv_fact_.resize(max_num + 1, 1);
    mod_ = mod;
    for (ll i = 2; i <= max_num; i++) {
      fact_[i] = i * fact_[i - 1] % mod_;
      inv_fact_[i] = MODpow(fact_[i], mod_ - 2, mod_);
      assert(fact_[i] * inv_fact_[i] % mod_ == 1);
    }
  }
  ll operator()(ll n, ll m) const {
    if (m < 0 || m > n)
      return 0;
    return fact_[n] * inv_fact_[n - m] % mod_ * inv_fact_[m] % mod_;
  }

private:
  vector<ll> fact_, inv_fact_;
  ll mod_;
};

int main() {
  ll p;
  cin >> p;
  vector<ll> a(p);
  for (ll i = 0; i < p; i++) {
    cin >> a[i];
  }

  Combination comb(p - 1, p);

  vector<ll> b(p);
  for (ll j = 0; j < p; j++) {
    if (a[j] == 0) {
      continue;
    }
    // 1 - (x - j)^(p - 1)を展開
    // この第二項についてx^iの項の係数を考えると - p-1_C_i (-j)^(p-1-i)
    for (ll i = 0; i < p; i++) {
      ll c = comb(p - 1, i) * MODpow(j, p - 1 - i, p);
      if ((p - 1 - i) % 2 == 0) {
        c = p - c;
      }
      if (i == 0) {
        c += 1;
      }
      (b[i] += c) %= p;
    }
  }

  for (ll i = 0; i < p; i++) {
    cout << b[i] << " \n"[i == p - 1];
  }
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = int64_t;

ll MODpow(ll n, ll m, ll mod) {
  ll result = 1;
  while (m) {
    if (m % 2 == 1) {
      result *= n;
      result %= mod;
    }

    m /= 2;
    n *= n;
    n %= mod;
  }

  return result;
}

class Combination {
public:
  Combination(ll max_num, ll mod) {
    fact_.resize(max_num + 1, 1);
    inv_fact_.resize(max_num + 1, 1);
    mod_ = mod;
    for (ll i = 2; i <= max_num; i++) {
      fact_[i] = i * fact_[i - 1] % mod_;
      inv_fact_[i] = MODpow(fact_[i], mod_ - 2, mod_);
      assert(fact_[i] * inv_fact_[i] % mod_ == 1);
    }
  }
  ll operator()(ll n, ll m) const {
    if (m < 0 || m > n)
      return 0;
    return fact_[n] * inv_fact_[n - m] % mod_ * inv_fact_[m] % mod_;
  }

private:
  vector<ll> fact_, inv_fact_;
  ll mod_;
};

int main() {
  ll p;
  cin >> p;
  vector<ll> a(p);
  for (ll i = 0; i < p; i++) {
    cin >> a[i];
  }

  Combination comb(p - 1, p);

  vector<ll> b(p);
  for (ll j = 0; j < p; j++) {
    if (a[j] == 0) {
      continue;
    }
    // 1 - (x - j)^(p - 1)を展開
    // この第二項についてx^iの項の係数を考えると - p-1_C_i (-j)^(p-1-i)
    ll pow = 1;
    for (ll i = p - 1; i >= 0; i--) {
      ll c = comb(p - 1, i) * pow % p;
      (pow *= j) %= p;
      if ((p - 1 - i) % 2 == 0) {
        c = p - c;
      }
      if (i == 0) {
        c += 1;
      }
      (b[i] += c) %= p;
    }
  }

  for (ll i = 0; i < p; i++) {
    cout << b[i] << " \n"[i == p - 1];
  }
}
Problem p02950 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

int main() {
  int p;
  std::cin >> p;

  std::vector<int> a(p);
  for (int i = 0; i < p; i++)
    std::cin >> a[i];

  auto pow = [&p](long long a, int n) {
    long long res = 1;
    for (; n; n >>= 1) {
      if (n & 1)
        res = res * a % p;
      a = a * a % p;
    }
    return res;
  };

  std::vector<long long> fact(p), inv_fact(p);
  fact[0] = 1;
  for (int i = 1; i < p; i++)
    fact[i] = fact[i - 1] * i % p;
  inv_fact[p - 1] = pow(fact[p - 1], p - 2);
  for (int i = p - 1; i > 0; i--)
    inv_fact[i - 1] = inv_fact[i] * i % p;

  auto comb = [&p, &fact, &inv_fact](int n, int r) {
    if (n < r)
      return 0LL;
    long long tmp = inv_fact[n - r] * inv_fact[r] % p;
    return fact[n] * tmp % p;
  };

  std::vector<int> b(p);
  for (int i = 0; i < p; i++) {
    if (a[i] == 0)
      continue;

    std::vector<int> c(p);
    for (int k = 0; k < p; k++)
      c[k] = pow(-i, p - 1 - k);

    b[0]++;
    for (int k = 0; k < p; k++)
      b[k] = (b[k] + p - comb(p - 1, k) * c[k] % p) % p;
  }

  for (int i = 0; i < p; i++)
    std::cout << b[i] << (i < p - 1 ? " " : "\n");
}

Fixed submission:
#include <iostream>
#include <vector>

int main() {
  int p;
  std::cin >> p;

  std::vector<int> a(p);
  for (int i = 0; i < p; i++)
    std::cin >> a[i];

  auto pow = [&p](long long a, int n) {
    long long res = 1;
    for (; n; n >>= 1) {
      if (n & 1)
        res = res * a % p;
      a = a * a % p;
    }
    return res;
  };

  std::vector<long long> fact(p), inv_fact(p);
  fact[0] = 1;
  for (int i = 1; i < p; i++)
    fact[i] = fact[i - 1] * i % p;
  inv_fact[p - 1] = pow(fact[p - 1], p - 2);
  for (int i = p - 1; i > 0; i--)
    inv_fact[i - 1] = inv_fact[i] * i % p;

  auto comb = [&p, &fact, &inv_fact](int n, int r) {
    if (n < r)
      return 0LL;
    long long tmp = inv_fact[n - r] * inv_fact[r] % p;
    return fact[n] * tmp % p;
  };

  std::vector<int> b(p);
  for (int i = 0; i < p; i++) {
    if (a[i] == 0)
      continue;

    std::vector<int> c(p);
    c[p - 1] = 1;
    for (int k = p - 2; k >= 0; k--)
      c[k] = -i * c[k + 1] % p;

    b[0]++;
    for (int k = 0; k < p; k++)
      b[k] = (b[k] + p - comb(p - 1, k) * c[k] % p) % p;
  }

  for (int i = 0; i < p; i++)
    std::cout << b[i] << (i < p - 1 ? " " : "\n");
}
Problem p02950 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 2e3 + 50;
ll mod = 998244353;
ll qm(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod, b >>= 1;
  }
  return res;
}
ll a[maxn], b[maxn], c[maxn], temp[maxn];
ll x[maxn], y[maxn];
ll n;
void mul(ll *f, int len, ll t) { // len为多项式的次数+1，函数让多项式f变成f*(x+t)
  for (int i = len; i > 0; --i)
    temp[i] = f[i], f[i] = f[i - 1];
  temp[0] = f[0], f[0] = 0;
  for (int i = 0; i <= len; ++i)
    f[i] = (f[i] + t * temp[i] + mod) % mod;
}
void dev(ll *f, ll *r, ll t) { // f是被除多项式的系数，r保存f除以x+t的结果
  for (int i = 0; i <= n; ++i)
    temp[i] = f[i];
  for (int i = n; i > 0; --i) {
    r[i - 1] = temp[i];
    temp[i - 1] = (temp[i - 1] - t * temp[i] + mod) % mod;
  }
  return;
}
void lglr() {
  memset(a, 0, sizeof a);
  b[1] = 1, b[0] = -x[1];
  for (int i = 2; i <= n; ++i) {
    mul(b, i, -x[i]);
  } // 预处理(x-x1)*(x-x2)...*(x-xn)
  for (int i = 1; i <= n; ++i) {
    ll fz = 1;
    for (int j = 1; j <= n; ++j) {
      if (j == i)
        continue;
      fz = (fz * (x[i] - x[j]) + mod) % mod;
    }
    fz = qm(fz, mod - 2);
    fz = ((fz * y[i] % mod) + mod) % mod; // 得到多项式系数
    dev(b, c, -x[i]);                     // 得到多项式，保存在b数组
    for (int j = 0; j < n; ++j)
      a[j] = (a[j] + fz * c[j] + mod) % mod;
  }
}
int main() {
  cin >> n;
  mod = n;
  for (int i = 1; i <= n; ++i) {
    x[i] = i - 1;
    cin >> y[i];
  }

  lglr();
  for (int i = 0; i < n; i++)
    cout << a[i] << " ";
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 3e3 + 50;
ll mod = 998244353;
ll qm(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod, b >>= 1;
  }
  return res;
}
ll a[maxn], b[maxn], c[maxn], temp[maxn];
ll x[maxn], y[maxn];
ll n;
void mul(ll *f, int len, ll t) { // len为多项式的次数+1，函数让多项式f变成f*(x+t)
  for (int i = len; i > 0; --i)
    temp[i] = f[i], f[i] = f[i - 1];
  temp[0] = f[0], f[0] = 0;
  for (int i = 0; i <= len; ++i)
    f[i] = (f[i] + t * temp[i] + mod) % mod;
}
void dev(ll *f, ll *r, ll t) { // f是被除多项式的系数，r保存f除以x+t的结果
  for (int i = 0; i <= n; ++i)
    temp[i] = f[i];
  for (int i = n; i > 0; --i) {
    r[i - 1] = temp[i];
    temp[i - 1] = (temp[i - 1] - t * temp[i] + mod) % mod;
  }
  return;
}
void lglr() {
  memset(a, 0, sizeof a);
  b[1] = 1, b[0] = -x[1];
  for (int i = 2; i <= n; ++i) {
    mul(b, i, -x[i]);
  } // 预处理(x-x1)*(x-x2)...*(x-xn)
  for (int i = 1; i <= n; ++i) {
    ll fz = 1;
    for (int j = 1; j <= n; ++j) {
      if (j == i)
        continue;
      fz = (fz * (x[i] - x[j]) + mod) % mod;
    }
    fz = qm(fz, mod - 2);
    fz = ((fz * y[i] % mod) + mod) % mod; // 得到多项式系数
    dev(b, c, -x[i]);                     // 得到多项式，保存在b数组
    for (int j = 0; j < n; ++j)
      a[j] = (a[j] + fz * c[j] + mod) % mod;
  }
}
int main() {
  cin >> n;
  mod = n;
  for (int i = 1; i <= n; ++i) {
    x[i] = i - 1;
    cin >> y[i];
  }

  lglr();
  for (int i = 0; i < n; i++)
    cout << a[i] << " ";
}
Problem p02950 (Python)

Buggy submission (status: Time Limit Exceeded):
p = int(input())


def div(a, b):
    n = len(a)
    m = len(b)
    res = [0] * (n - m + 1)
    tmp = pow(b[-1], p - 2, p)
    for i in range(n - m, -1, -1):
        t = a[i + m - 1] * tmp % p
        res[i] = t
        for j in range(m):
            a[i + j] = (a[i + j] - t * b[j]) % p
    return res


a = list(map(int, input().split()))
ans = [0] * p
f = [0] * (p + 1)
f[0] = 1
for i in range(p):
    for j in range(p, 0, -1):
        f[j] += f[j - 1]
        f[j - 1] = -f[j - 1] * i
        f[j] %= p
for i in range(p):
    if a[i] == 1:
        fm = div(f[:], [-i, 1])
        t = 1
        r = 0
        for v in fm:
            r += v * t
            t = t * i % p
        t = pow(r, p - 2, p)
        for j in range(len(fm)):
            ans[j] += fm[j] * t % p
for i in range(p):
    ans[i] %= p
print(" ".join(map(str, ans)))


Fixed submission:
p = int(input())


def div(a, b):
    n = len(a)
    m = len(b)
    res = [0] * (n - m + 1)
    tmp = pow(b[-1], p - 2, p)
    for i in range(n - m, -1, -1):
        t = a[i + m - 1] * tmp % p
        res[i] = t
        for j in range(m):
            a[i + j] = (a[i + j] - t * b[j]) % p
    return res


a = list(map(int, input().split()))
ans = [0] * p
f = [0] * (p + 1)
f[0] = 1
for i in range(p):
    for j in range(p, 0, -1):
        f[j] += f[j - 1]
        f[j - 1] = -f[j - 1] * i
        f[j] %= p
for i in range(p):
    if a[i] == 1:
        fm = div(f[:], [-i, 1])
        t = 1
        r = 0
        for v in fm:
            r += v * t
            t = t * i % p
        t = pow(r, p - 2, p)
        for j in range(len(fm)):
            ans[j] += fm[j] * t
for i in range(p):
    ans[i] %= p
print(" ".join(map(str, ans)))
Problem p02950 (Python)

Buggy submission (status: Time Limit Exceeded):
p = int(input())
a = list(map(int, input().split()))

MOD = p
MAX = p + 10
fact = [1] * (MAX + 1)  # i!
finv = [1] * (MAX + 1)  # (i!)^{-1}
iinv = [1] * (MAX + 1)  # i^{-1}
for i in range(2, MAX + 1):
    fact[i] = fact[i - 1] * i % MOD
    iinv[i] = MOD - iinv[MOD % i] * (MOD // i) % MOD
    finv[i] = finv[i - 1] * iinv[i] % MOD


def comb(n: int, k: int) -> int:
    if n < k or n < 0 or k < 0:
        return 0
    return (fact[n] * finv[k] % MOD) * finv[n - k] % MOD


b = [0] * p
for j in range(p):
    if a[j] == 1:
        b[0] += 1
        for k in range(p):
            b[k] += comb(p - 1, k) * pow(j, p - 1 - k, MOD) * (-1) ** (p - k)
            b[k] %= MOD

print(*b)


Fixed submission:
p = int(input())
a = list(map(int, input().split()))

MOD = p
MAX = p + 10
fact = [1] * (MAX + 1)  # i!
finv = [1] * (MAX + 1)  # (i!)^{-1}
iinv = [1] * (MAX + 1)  # i^{-1}
for i in range(2, MAX + 1):
    fact[i] = fact[i - 1] * i % MOD
    iinv[i] = MOD - iinv[MOD % i] * (MOD // i) % MOD
    finv[i] = finv[i - 1] * iinv[i] % MOD


def comb(n: int, k: int) -> int:
    if n < k or n < 0 or k < 0:
        return 0
    return (fact[n] * finv[k] % MOD) * finv[n - k] % MOD


b = [0] * p
for j in range(p):
    if a[j] == 1:
        b[0] += 1
        for k in range(p):
            b[k] += comb(p - 1, k) * pow(j, p - 1 - k, MOD) * (-1) ** ((p - k) % 2)
            b[k] %= MOD

print(*b)