prithivMLmods/Coder-Stat huggingface dataset:
Problem p03023 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define mod 1000000007
#define inf 1000000000000000007
#define eps 0.00000001
#define pi 3.141592653589793
#define pii pair<int, int>
#define pdd pair<ld, ld>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define vii vector<int>
#define vpl vector<pll>
#define vll vector<ll>
#define sti stack<int>
#define stll stack<ll>
#define mseti multiset<ll>
#define msetd multiset<ll, greater<ll>>
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define stp setprecision(20) // use fixed before stp
#define endl '\n'
int main() {
  FAST
#ifndef ONLINE_JUDGE
      freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  ll n;
  cin >> n;
  ll ans = 180 * (n - 2);
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define mod 1000000007
#define inf 1000000000000000007
#define eps 0.00000001
#define pi 3.141592653589793
#define pii pair<int, int>
#define pdd pair<ld, ld>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define vii vector<int>
#define vpl vector<pll>
#define vll vector<ll>
#define sti stack<int>
#define stll stack<ll>
#define mseti multiset<ll>
#define msetd multiset<ll, greater<ll>>
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define stp setprecision(20) // use fixed before stp
#define endl '\n'
int main() {
  FAST ll n;
  cin >> n;
  ll ans = 180 * (n - 2);
  cout << ans;
}
Problem p03023 (C++)

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

int main() {
  int N;
  cout << 180 * (N - 2) / N << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  cout << 180 * (N - 2) << endl;
}
Problem p03023 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define int long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()
#define forn(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i < (n); i++)
const int MOD = 987654319; // 1e9 + 7 ;
int mpow(int a, int b, int p = MOD) {
  a = a % p;
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    a = (a * a) % p;
    b = b >> 1;
  }
  return res % p;
}
const int N = 300002;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  int n;
  cin >> n;
  cout << (n - 2) * 180;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define int long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()
#define forn(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i < (n); i++)
const int MOD = 987654319; // 1e9 + 7 ;
int mpow(int a, int b, int p = MOD) {
  a = a % p;
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    a = (a * a) % p;
    b = b >> 1;
  }
  return res % p;
}
const int N = 300002;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;
  cout << (n - 2) * 180;
}
Problem p03023 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
int main(void) {
  int N;
  cin >> N;
  return (180 * (N) + 180);
}

Fixed submission:
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
int main(void) {
  int N;
  cin >> N;
  cout << (180 + (N - 3) * 180);
  return 0;
}
Problem p03025 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define REP(i, b, e) for (ll i = (ll)b; i < (ll)e; i++)
#define rep0(i, n) REP(i, 0ll, n)
#define rep1(i, n) REP(i, 1ll, n + 1)
const ll longinf = 1ll << 60;
const int inf = (1 << 30) - 1;

// II→factorialを計算するrange
const ll MOD = 1e9 + 7;
const int II = 1e5 + 3;

ll addMod(ll a, ll b) { return ((a % MOD) + (b % MOD)) % MOD; }

ll mulMod(ll a, ll b) { return ((a % MOD) * (b % MOD)) % MOD; }

ll powMod(ll a, ll b) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0) {
    ll ret = powMod(a, b / 2);
    return ((ret % MOD) * (ret % MOD)) % MOD;
  } else
    return ((powMod(a, b - 1) % MOD) * (a % MOD)) % MOD;
}
ll divMod(ll a, ll b) { return ((a % MOD) * (powMod(b, MOD - 2) % MOD)) % MOD; }

ll fact[II + 1];
ll invfact[II + 1];

void calc() { // main関数内にcalc();を入れること(前計算)
  fact[0] = 1;
  invfact[0] = 1;
  rep0(i, II) {
    fact[i + 1] = mulMod(fact[i], i + 1);
    invfact[i + 1] = divMod(1, fact[i + 1]);
  }
}

int main() {
  ll n, a, b, c;
  cin >> n >> a >> b >> c;
  calc();
  ll p = divMod(a, a + b);
  ll q = divMod(b, a + b);
  ll ans = 0;
  for (int m = n; m < 2 * n; m++) {
    ll comb = mulMod(fact[m - 1], invfact[n - 1]);
    comb = mulMod(comb, invfact[m - n]);
    ll A = mulMod(powMod(p, n), powMod(q, m - n));
    ll B = mulMod(powMod(p, m - n), powMod(q, n));
    ll C = mulMod(addMod(A, B), m);
    ans = addMod(ans, mulMod(comb, C));
  }
  ll d = divMod(100ll, addMod(a, b));
  cout << mulMod(ans, d) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define REP(i, b, e) for (ll i = (ll)b; i < (ll)e; i++)
#define rep0(i, n) REP(i, 0ll, n)
#define rep1(i, n) REP(i, 1ll, n + 1)
const ll longinf = 1ll << 60;
const int inf = (1 << 30) - 1;

// II→factorialを計算するrange
const ll MOD = 1e9 + 7;
const ll II = 2e5 + 3;

ll addMod(ll a, ll b) { return ((a % MOD) + (b % MOD)) % MOD; }

ll mulMod(ll a, ll b) { return ((a % MOD) * (b % MOD)) % MOD; }

ll powMod(ll a, ll b) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0) {
    ll ret = powMod(a, b / 2);
    return ((ret % MOD) * (ret % MOD)) % MOD;
  } else
    return ((powMod(a, b - 1) % MOD) * (a % MOD)) % MOD;
}
ll divMod(ll a, ll b) { return ((a % MOD) * (powMod(b, MOD - 2) % MOD)) % MOD; }

ll fact[II + 1];
ll invfact[II + 1];

void calc() { // main関数内にcalc();を入れること(前計算)
  fact[0] = 1;
  invfact[0] = 1;
  rep0(i, II) {
    fact[i + 1] = mulMod(fact[i], i + 1);
    invfact[i + 1] = divMod(1, fact[i + 1]);
  }
}

int main() {
  ll n, a, b, c;
  cin >> n >> a >> b >> c;
  calc();
  ll p = divMod(a, a + b);
  ll q = divMod(b, a + b);
  ll ans = 0;
  for (int m = n; m < 2 * n; m++) {
    ll comb = mulMod(fact[m - 1], invfact[n - 1]);
    comb = mulMod(comb, invfact[m - n]);
    ll A = mulMod(powMod(p, n), powMod(q, m - n));
    ll B = mulMod(powMod(p, m - n), powMod(q, n));
    ll C = mulMod(addMod(A, B), m);
    ans = addMod(ans, mulMod(comb, C));
  }
  ll d = divMod(100ll, addMod(a, b));
  cout << mulMod(ans, d) << endl;
  return 0;
}
Problem p03026 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;

int n;
vector<int> G[1010];
int c[1010], p[1010];
int cur;

void dfs(int v) {
  p[v] = c[cur--];
  for (int nv : G[v])
    if (!p[nv])
      dfs(nv);
}

int main() {
  scanf("%d", &n);
  rep(i, n - 1) {
    int a, b;
    scanf("%d%d", &a, &b);
    G[--a].push_back(--b);
    G[b].push_back(a);
  }
  rep(i, n) scanf("%d", &c[i]);
  sort(c, c + n);
  rep(i, n) p[i] = 0;
  cur = n - 1;
  dfs(0);
  int ans = 0;
  rep(i, n - 1) ans += c[i];
  printf("%d\n%d", ans, p[0]);
  rep(i, n - 1) printf(" %d", p[i + 1]);
  printf("\n");
}

Fixed submission:
#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;

int n;
vector<int> G[10010];
int c[10010], p[10010];
int cur;

void dfs(int v) {
  p[v] = c[cur--];
  for (int nv : G[v])
    if (!p[nv])
      dfs(nv);
}

int main() {
  scanf("%d", &n);
  rep(i, n - 1) {
    int a, b;
    scanf("%d%d", &a, &b);
    G[--a].push_back(--b);
    G[b].push_back(a);
  }
  rep(i, n) scanf("%d", &c[i]);
  sort(c, c + n);
  rep(i, n) p[i] = 0;
  cur = n - 1;
  dfs(0);
  int ans = 0;
  rep(i, n - 1) ans += c[i];
  printf("%d\n%d", ans, p[0]);
  rep(i, n - 1) printf(" %d", p[i + 1]);
  printf("\n");
}
Problem p03026 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

void dfs(int node, vector<vector<int>> edge, vector<int> &val,
         priority_queue<int, vector<int>, greater<int>> &c) {
  if (val[node] != -1 || c.empty())
    return;
  val[node] = 0; // 仮置き
  for (auto tar : edge[node]) {
    if (val[tar] == -1)
      dfs(tar, edge, val, c);
  }
  val[node] = c.top();
  c.pop();
}

int main() {
  int n, a, b, x;
  cin >> n;
  vector<vector<int>> edge(n, vector<int>());
  vector<int> val(n, -1);
  priority_queue<int, vector<int>, greater<int>> c;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    a--;
    b--;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }

  ll ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> x;
    c.push(x);
    ans += x;
  }
  dfs(0, edge, val, c);
  cout << ans - val[0] << endl;
  for (auto node : val) {
    cout << node << " ";
  }
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

void dfs(int node, vector<vector<int>> &edge, vector<int> &val,
         priority_queue<int, vector<int>, greater<int>> &c) {
  if (val[node] != -1 || c.empty())
    return;
  val[node] = 0; // 仮置き
  for (auto tar : edge[node]) {
    if (val[tar] == -1)
      dfs(tar, edge, val, c);
  }
  val[node] = c.top();
  c.pop();
}

int main() {
  int n, a, b, x;
  cin >> n;
  vector<vector<int>> edge(n, vector<int>());
  vector<int> val(n, -1);
  priority_queue<int, vector<int>, greater<int>> c;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    a--;
    b--;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }

  ll ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> x;
    c.push(x);
    ans += x;
  }
  dfs(0, edge, val, c);
  cout << ans - val[0] << endl;
  for (auto node : val) {
    cout << node << " ";
  }
  cout << endl;
}
Problem p03026 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

#define INF ((int)1e9)
#define INFLL ((ll)1e18)
#define MOD (1000000007LL)

int main(int argc, const char *argv[]) {
  ios_base::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<vector<int>> G(N);
  for (int n = 0; n < N - 1; ++n) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  multiset<int> Set;
  int Res = 0;
  for (int n = 0; n < N; ++n) {
    int c;
    cin >> c;
    Set.insert(c);
    Res += c;
  }

  auto itEnd = Set.rbegin();
  Res -= *itEnd;
  cout << Res << endl;

  set<pair<int, int>> Num;
  vector<int> NumNow(N);
  for (int n = 0; n < N; ++n) {
    Num.insert({G[n].size(), n});
    NumNow[n] = G[n].size();
  }
  vector<int> Res2(N, -1);
  for (auto it = Set.begin(); it != Set.end();) {
    auto itV = Num.begin();
    if (Res2[itV->second] == -1) {
      Res2[itV->second] = (*it);
      ++it;
    }
    Num.erase(itV);
    for (int u : G[itV->first]) {
      if (Res2[u] == -1) {
        --NumNow[u];
        Num.insert({NumNow[u], u});
      }
    }
  }

  for (int n = 0; n < N; ++n) {
    cout << Res2[n] << '\n';
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

#define INF ((int)1e9)
#define INFLL ((ll)1e18)
#define MOD (1000000007LL)

int main(int argc, const char *argv[]) {
  ios_base::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<vector<int>> G(N);
  for (int n = 0; n < N - 1; ++n) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  multiset<int> Set;
  int Res = 0;
  for (int n = 0; n < N; ++n) {
    int c;
    cin >> c;
    Set.insert(c);
    Res += c;
  }

  auto itEnd = Set.rbegin();
  Res -= *itEnd;
  cout << Res << endl;

  set<pair<int, int>> Num;
  vector<int> NumNow(N);
  for (int n = 0; n < N; ++n) {
    Num.insert({G[n].size(), n});
    NumNow[n] = G[n].size();
  }
  vector<int> Res2(N, -1);
  for (auto it = Set.begin(); it != Set.end();) {
    auto itV = Num.begin();
    if (Res2[itV->second] == -1) {
      Res2[itV->second] = (*it);
      ++it;
    }
    Num.erase(itV);
    for (int u : G[itV->second]) {
      if (Res2[u] == -1) {
        --NumNow[u];
        Num.insert({NumNow[u], u});
      }
    }
  }

  for (int n = 0; n < N; ++n) {
    cout << Res2[n] << '\n';
  }

  return 0;
}
Problem p03026 (C++)

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

int main() {
  // # of Nodes
  int N;
  cin >> N;
  // Links to connect node a & b
  vector<vector<int>> ab(N + 1, vector<int>(0));
  for (int i = 1; i < N; i++) {
    int a, b;
    cin >> a;
    cin >> b;
    if (a > b)
      swap(a, b);
    ab.at(a).push_back(b);
  }
  //
  vector<int> c(N);
  int M = 0;
  for (int i = 0; i < N; i++) {
    cin >> c.at(i);
    M += c.at(i);
  }
  sort(c.begin(), c.end());
  reverse(c.begin(), c.end());
  //
  M -= c.at(0);
  cout << M << endl;
  //
  vector<bool> Ndone(N + 1, false);
  vector<int> order(0);
  order.push_back(1);
  Ndone.at(1) = true;
  int ix = 0;
  while (true) {
    int NN = order.at(ix);
    int iy = 0;
    while (iy != ab.at(NN).size()) {
      int y = ab.at(NN).at(iy);
      if (!Ndone.at(y)) {
        order.push_back(y);
        Ndone.at(y) = true;
      }
      iy++;
    }
    ix++;
    if (N == ix)
      break;
  }
  vector<int> d(N + 1, 0);
  for (int i = 0; i < N; i++)
    d.at(order.at(i)) = c.at(i);
  for (int i = 1; i <= N; i++)
    cout << d.at(i) << ' ';
  cout << endl;
  return 0;
};


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  // # of Nodes
  int N;
  cin >> N;
  // Links to connect node a & b
  vector<vector<int>> ab(N + 1, vector<int>(0));
  for (int i = 1; i < N; i++) {
    int a, b;
    cin >> a;
    cin >> b;
    ab.at(a).push_back(b);
    swap(a, b);
    ab.at(a).push_back(b);
  }
  //
  vector<int> c(N);
  int M = 0;
  for (int i = 0; i < N; i++) {
    cin >> c.at(i);
    M += c.at(i);
  }
  sort(c.begin(), c.end());
  reverse(c.begin(), c.end());
  //
  M -= c.at(0);
  cout << M << endl;
  //
  vector<bool> Ndone(N + 1, false);
  vector<int> order(0);
  order.push_back(1);
  Ndone.at(1) = true;
  int ix = 0;
  while (true) {
    int NN = order.at(ix);
    int iy = 0;
    while (iy != ab.at(NN).size()) {
      int y = ab.at(NN).at(iy);
      if (!Ndone.at(y)) {
        order.push_back(y);
        Ndone.at(y) = true;
      }
      iy++;
    }
    ix++;
    if (N == ix)
      break;
  }
  vector<int> d(N + 1, 0);
  for (int i = 0; i < N; i++)
    d.at(order.at(i)) = c.at(i);
  for (int i = 1; i <= N; i++)
    cout << d.at(i) << ' ';
  cout << endl;
  return 0;
};
Problem p03026 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long int
using namespace std;
vector<vector<int>> v;
int n;
int vis[1005] = {0};
int ans[1005] = {0};

int cnt;
void dfs(int i, int a[]) {
  vis[i] = 1;
  ans[i] = a[cnt];
  cnt--;
  for (auto j : v[i]) {
    if (vis[j] == 0)
      dfs(j, a);
  }
}

signed main() {
  cin >> n;
  int a[n];
  int sum = 0;
  v.resize(n);

  int x, y;
  for (int i = 0; i < n - 1; ++i) {
    cin >> x >> y;
    x--;
    y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }

  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += a[i];
  }
  sort(a, a + n);

  sum -= a[n - 1];

  cnt = n - 1;
  dfs(0, a);

  cout << sum << endl;
  for (int i = 0; i < n; ++i) {
    cout << ans[i] << " ";
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long int
using namespace std;
vector<vector<int>> v;
int n;
int vis[10005] = {0};
int ans[10005] = {0};

int cnt;
void dfs(int i, int a[]) {
  vis[i] = 1;
  ans[i] = a[cnt];
  cnt--;
  for (auto j : v[i]) {
    if (vis[j] == 0)
      dfs(j, a);
  }
}

signed main() {
  cin >> n;
  int a[n];
  int sum = 0;
  v.resize(n);

  int x, y;
  for (int i = 0; i < n - 1; ++i) {
    cin >> x >> y;
    x--;
    y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }

  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += a[i];
  }
  sort(a, a + n);

  sum -= a[n - 1];

  cnt = n - 1;
  dfs(0, a);

  cout << sum << endl;
  for (int i = 0; i < n; ++i) {
    cout << ans[i] << " ";
  }
}
Problem p03026 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)

using namespace std;

vector<vector<ll>> graph;
vector<ll> depth;
void dfs(ll i, ll v) {
  if (depth[i] != -1)
    return;
  depth[i] = v;
  for (ll e : graph[i]) {
    dfs(e, v + 1);
  }
}

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

  ll N;
  cin >> N;
  graph.assign(N, vector<ll>(N));
  depth.assign(N, -1);
  Rep(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  vector<ll> c(N);
  ll M = 0;
  Rep(i, N) {
    cin >> c[i];

    M += c[i];
  }
  sort(c.begin(), c.end());
  M -= c.back();
  cout << M << "\n";

  dfs(0, 0);
  // Rep (i, N) {
  //     cout << depth[i] << "\n";
  // }

  set<pair<ll, ll>> ans;
  Rep(i, N) { ans.insert(make_pair(-depth[i], i)); }

  vector<ll> output(N);
  ll i = 0;
  for (auto e : ans) {
    output[e.second] = c[i];
    i++;
  }

  for (ll e : output) {
    cout << e << " ";
  }

  cout << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll int
#define Rep(i, n) for (ll i = 0; i < n; i++)

using namespace std;

vector<vector<ll>> graph;
vector<ll> depth;
void dfs(ll i, ll v) {
  if (depth[i] != -1)
    return;
  depth[i] = v;
  for (ll e : graph[i]) {
    dfs(e, v + 1);
  }
}

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

  ll N;
  cin >> N;
  graph.assign(N, vector<ll>(N));
  depth.assign(N, -1);
  Rep(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  vector<ll> c(N);
  ll M = 0;
  Rep(i, N) {
    cin >> c[i];

    M += c[i];
  }
  sort(c.begin(), c.end());
  M -= c.back();
  cout << M << "\n";

  dfs(0, 0);
  // Rep (i, N) {
  //     cout << depth[i] << "\n";
  // }

  set<pair<ll, ll>> ans;
  Rep(i, N) { ans.insert(make_pair(-depth[i], i)); }

  vector<ll> output(N);
  ll i = 0;
  for (auto e : ans) {
    output[e.second] = c[i];
    i++;
  }

  for (ll e : output) {
    cout << e << " ";
  }

  cout << endl;
}
Problem p03027 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;
using vecint = vector<int>;
using ll = int64_t;
constexpr ll MOD = 1000003;

// a^-1 mod p
ll inv(ll a, ll p) { return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p); }

ll powmod(ll x, ll i) {
  if (i == 0)
    return 1;
  if ((i % 2) == 1)
    return (x * powmod(x, i - 1)) % MOD;
  ll h = powmod(x, i / 2);
  return (h * h) % MOD;
}

int main() {
  int q;
  cin >> q;
  vector<ll> fact(MOD);
  vector<ll> factinv(MOD);
  fact[0] = factinv[0] = 1;
  REP(i, MOD - 1) {
    fact[i + 1] = fact[i] * (i + 1);
    fact[i + 1] %= MOD;
    factinv[i + 1] = inv(fact[i + 1], MOD);
  }
  REP(task, q) {
    ll x, d, n;
    cin >> x >> d >> n;
    if (x == 0) {
      cout << 0 << endl;
    } else {
      ll y = (x * inv(d, MOD)) % MOD;
      if (y + n - 1 < MOD) {
        ll res = powmod(d, n);
        res *= fact[y + n - 1];
        res %= MOD;
        res *= factinv[y - 1];
        res %= MOD;
        cout << res << endl;
      } else {
        cout << 0 << endl;
      }
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;
using vecint = vector<int>;
using ll = int64_t;
constexpr ll MOD = 1000003;

// a^-1 mod p
ll inv(ll a, ll p) { return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p); }

ll powmod(ll x, ll i) {
  if (i == 0)
    return 1;
  if ((i % 2) == 1)
    return (x * powmod(x, i - 1)) % MOD;
  ll h = powmod(x, i / 2);
  return (h * h) % MOD;
}

int main() {
  int q;
  cin >> q;
  vector<ll> fact(MOD);
  vector<ll> factinv(MOD);
  fact[0] = factinv[0] = 1;
  REP(i, MOD - 1) {
    fact[i + 1] = fact[i] * (i + 1);
    fact[i + 1] %= MOD;
    factinv[i + 1] = inv(fact[i + 1], MOD);
  }
  REP(task, q) {
    ll x, d, n;
    cin >> x >> d >> n;
    if (x == 0) {
      cout << 0 << endl;
    } else if (d == 0) {
      cout << powmod(x, n) << endl;
    } else {
      ll y = (x * inv(d, MOD)) % MOD;
      if (y + n - 1 < MOD) {
        ll res = powmod(d, n);
        res *= fact[y + n - 1];
        res %= MOD;
        res *= factinv[y - 1];
        res %= MOD;
        cout << res << endl;
      } else {
        cout << 0 << endl;
      }
    }
  }
  return 0;
}
Problem p03027 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define MOD 1000003
using namespace std;
typedef long long ll;

template <typename Tp> inline void getint(Tp &num) {
  register int ch, neg = 0;
  while (!isdigit(ch = getchar()))
    if (ch == '-')
      neg = 1;
  num = ch & 15;
  while (isdigit(ch = getchar()))
    num = num * 10 + (ch & 15);
  if (neg)
    num = -num;
}

inline ll fastpow(ll bas, ll ex) {
  register ll res = 1;
  bas %= MOD;
  for (; ex; ex >>= 1, bas = bas * bas % MOD)
    if (ex & 1)
      res = res * bas % MOD;
  return res;
}

int Q, fac[1000005] = {1, 1}, ifac[1000005] = {1, 1}, inv[1000005] = {1, 1};

int main() {
  getint(Q);
  for (register int i = 2; i < MOD; i++) {
    fac[i] = fac[i - 1] * (ll)i % MOD;
    inv[i] = (MOD - MOD / i) * (ll)inv[MOD % i] % MOD;
    ifac[i] = ifac[i - 1] * (ll)inv[i] % MOD;
  }
  while (Q--) {
    int x, d, n;
    getint(x), getint(d), getint(n);
    x = x * (ll)inv[d] % MOD;
    printf("%lld\n",
           x ? fac[x + n - 1] * (ll)ifac[x - 1] % MOD * fastpow(d, n) % MOD
             : 0);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define MOD 1000003
using namespace std;
typedef long long ll;

template <typename Tp> inline void getint(Tp &num) {
  register int ch, neg = 0;
  while (!isdigit(ch = getchar()))
    if (ch == '-')
      neg = 1;
  num = ch & 15;
  while (isdigit(ch = getchar()))
    num = num * 10 + (ch & 15);
  if (neg)
    num = -num;
}

inline ll fastpow(ll bas, ll ex) {
  register ll res = 1;
  bas %= MOD;
  for (; ex; ex >>= 1, bas = bas * bas % MOD)
    if (ex & 1)
      res = res * bas % MOD;
  return res;
}

int Q, fac[1000005] = {1, 1}, ifac[1000005] = {1, 1}, inv[1000005] = {1, 1};

int main() {
  getint(Q);
  for (register int i = 2; i < MOD; i++) {
    fac[i] = fac[i - 1] * (ll)i % MOD;
    inv[i] = (MOD - MOD / i) * (ll)inv[MOD % i] % MOD;
    ifac[i] = ifac[i - 1] * (ll)inv[i] % MOD;
  }
  while (Q--) {
    int x, d, n;
    getint(x), getint(d), getint(n);
    if (d == 0)
      printf("%lld\n", fastpow(x, n));
    else {
      x = x * (ll)inv[d] % MOD;
      if (x == 0 || x + n - 1 >= MOD)
        puts("0");
      else
        printf("%lld\n",
               fac[x + n - 1] * (ll)ifac[x - 1] % MOD * fastpow(d, n) % MOD);
    }
  }
  return 0;
}
Problem p03027 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int MOD = 1000003;

int pot[MOD + 5], z, x, d, n, licz, mian, r;

// rozwiązuje równanie ax=1(%MOD), gdy MOD pierwsze
int inverse(int a, int n = MOD - 2) {
  int r = 1;
  while (n) {
    if (n % 2)
      r = (r * 1LL * a) % MOD;
    a = (a * 1LL * a) % MOD;
    n >>= 1;
  }
  return r;
}

// (a^b)%c
int poteguj(int a, int b, int c = MOD) {
  int r = 1, m = a;
  while (b) {
    if (b % 2)
      r = (r * 1LL * m) % c;
    m = (m * 1LL * m) % c;
    b /= 2;
  }
  return r;
}

int main() {
  pot[0] = 1;
  for (int i = 1; i < MOD; i++)
    pot[i] = (pot[i - 1] * 1LL * i) % MOD;
  scanf("%d", &z);
  while (z--) {
    scanf("%d %d %d", &x, &d, &n);
    if (x == 0 || d == 0) {
      printf("%d\n", poteguj(x, n));
      continue;
    } else if (d == 1) {
      if (x + n - 1 >= MOD)
        printf("0\n");
      else {
        r = (pot[x + n - 1] * 1LL * inverse(pot[x - 1])) % MOD;
        printf("%d\n", r);
      }
      continue;
    } else {
      licz = (x * inverse(d)) % MOD + n - 1;
      mian = (x * inverse(d)) % MOD - 1;
      if (licz >= MOD)
        printf("0\n");
      else {
        r = (pot[licz] * 1LL * inverse(pot[mian])) % MOD;
        r = (r * 1LL * poteguj(d, n)) % MOD;
        printf("%d\n", r);
      }
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int MOD = 1000003;

int pot[MOD + 5], z, x, d, n, licz, mian, r;

// rozwiązuje równanie ax=1(%MOD), gdy MOD pierwsze
int inverse(int a, int n = MOD - 2) {
  int r = 1;
  while (n) {
    if (n % 2)
      r = (r * 1LL * a) % MOD;
    a = (a * 1LL * a) % MOD;
    n >>= 1;
  }
  return r;
}

// (a^b)%c
int poteguj(int a, int b, int c = MOD) {
  int r = 1, m = a;
  while (b) {
    if (b % 2)
      r = (r * 1LL * m) % c;
    m = (m * 1LL * m) % c;
    b /= 2;
  }
  return r;
}

int main() {
  pot[0] = 1;
  for (int i = 1; i < MOD; i++)
    pot[i] = (pot[i - 1] * 1LL * i) % MOD;
  scanf("%d", &z);
  while (z--) {
    scanf("%d %d %d", &x, &d, &n);
    if (x == 0 || d == 0) {
      printf("%d\n", poteguj(x, n));
      continue;
    } else if (d == 1) {
      if (x + n - 1 >= MOD)
        printf("0\n");
      else {
        r = (pot[x + n - 1] * 1LL * inverse(pot[x - 1])) % MOD;
        printf("%d\n", r);
      }
      continue;
    } else {
      licz = (x * 1LL * inverse(d)) % MOD + n - 1;
      mian = (x * 1LL * inverse(d)) % MOD - 1;
      if (licz >= MOD)
        printf("0\n");
      else {
        r = (pot[licz] * 1LL * inverse(pot[mian])) % MOD;
        r = (r * 1LL * poteguj(d, n)) % MOD;
        printf("%d\n", r);
      }
    }
  }
}
Problem p03027 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;

vector<ll> fct, invfct;

ll mod_pow(ll n, ll p, ll mod) {
  // 繰り返し二乗法で (n^p) % mod を求める
  if (p == 0)
    return 1;
  ll res = mod_pow(n * n % mod, p / 2, mod);
  if (p % 2 == 1)
    res = res * n % mod;
  return res;
}

ll calc(ll x, ll n, ll mod) {
  if (x == 0 || x + n - 1 >= mod) {
    // (2-a) の場合
    return 0;
  } else {
    // (2-b) の場合
    return fct[x + n - 1] * invfct[x - 1] % mod;
  }
}

int main() {
  int Q;
  cin >> Q;
  ll mod = 1000003;

  // mod-1 までの階乗を求める。
  fct = vector<ll>(mod, 1);
  for (int i = 1; i < 2 * mod; i++) {
    fct[i] = fct[i - 1] * i % mod;
  }

  // mod-1 までの階乗の逆数を求める。
  invfct = vector<ll>(mod, 1);
  invfct[mod - 1] = mod_pow(mod - 1, mod - 2, mod);
  for (int i = mod - 2; i >= 1; i--) {
    invfct[i] = invfct[i + 1] * (i + 1) % mod;
  }

  while (Q--) {
    ll x, d, n;
    cin >> x >> d >> n;
    if (d == 0) {
      // (1) の場合。 x^n を計算する。
      cout << mod_pow(x, n, mod) << endl;
    } else if (d == 1) {
      // (2) の場合。
      cout << calc(x, n, mod) << endl;
    } else {
      // (3) の場合。
      // x を x/d の mod で置き換えて(2)の計算をし、最後に d^n を掛ける。
      x = x * mod_pow(d, mod - 2, mod) % mod;
      cout << mod_pow(d, n, mod) * calc(x, n, mod) % mod << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;

vector<ll> fct, invfct;

ll mod_pow(ll n, ll p, ll mod) {
  // 繰り返し二乗法で (n^p) % mod を求める
  if (p == 0)
    return 1;
  ll res = mod_pow(n * n % mod, p / 2, mod);
  if (p % 2 == 1)
    res = res * n % mod;
  return res;
}

ll calc(ll x, ll n, ll mod) {
  if (x == 0 || x + n - 1 >= mod) {
    // (2-a) の場合
    return 0;
  } else {
    // (2-b) の場合
    return fct[x + n - 1] * invfct[x - 1] % mod;
  }
}

int main() {
  int Q;
  cin >> Q;
  ll mod = 1000003;

  // mod-1 までの階乗を求める。
  fct = vector<ll>(mod, 1);
  for (int i = 1; i < mod; i++) {
    fct[i] = fct[i - 1] * i % mod;
  }

  // mod-1 までの階乗の逆数を求める。
  invfct = vector<ll>(mod, 1);
  invfct[mod - 1] = mod_pow(mod - 1, mod - 2, mod);
  for (int i = mod - 2; i >= 1; i--) {
    invfct[i] = invfct[i + 1] * (i + 1) % mod;
  }

  while (Q--) {
    ll x, d, n;
    cin >> x >> d >> n;
    if (d == 0) {
      // (1) の場合。 x^n を計算する。
      cout << mod_pow(x, n, mod) << endl;
    } else if (d == 1) {
      // (2) の場合。
      cout << calc(x, n, mod) << endl;
    } else {
      // (3) の場合。
      // x を x/d の mod で置き換えて(2)の計算をし、最後に d^n を掛ける。
      x = x * mod_pow(d, mod - 2, mod) % mod;
      cout << mod_pow(d, n, mod) * calc(x, n, mod) % mod << endl;
    }
  }
}
Problem p03027 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
typedef long long ll;

ll inf = 1000003;

ll pw(ll x, ll y) {
  if (y == 0)
    return 1;
  else if (y == 1)
    return x % inf;
  else if (y % 2 == 0)
    return pw(x, y / 2) * pw(x, y / 2) % inf;
  else
    return pw(x, y / 2) * pw(x, y / 2) % inf * x % inf;
}

ll dv(ll x, ll y) { return x * pw(y, inf - 2) % inf; }

int main(void) {
  int q;
  ll pile[3000010];
  cin >> q;

  for (int i = 1; i < 2 * inf; i++) {
    if (i == 1)
      pile[i] = 1;
    else
      pile[i] = pile[i - 1] * i % inf;
  }

  for (int i = 0; i < q; i++) {
    ll x, d, n;
    cin >> x >> d >> n;

    if (d == 0)
      cout << pw(x, n) << endl;
    else {
      if (dv(x, d + n - 1) >= inf)
        cout << 0 << endl;
      else
        cout << pw(d, n) * pile[dv(x, d) + n - 1] % inf *
                    dv(1, pile[dv(x, d) - 1]) % inf
             << endl;
    }
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
typedef long long ll;

ll inf = 1000003;

ll pw(ll x, ll y) {
  if (y == 0)
    return 1;
  else if (y == 1)
    return x % inf;
  else if (y % 2 == 0)
    return pw(x, y / 2) * pw(x, y / 2) % inf;
  else
    return pw(x, y / 2) * pw(x, y / 2) % inf * x % inf;
}

ll dv(ll x, ll y) { return x * pw(y, inf - 2) % inf; }

int main(void) {
  int q;
  ll pile[3000010];
  cin >> q;

  for (int i = 1; i < 2 * inf; i++) {
    if (i == 1)
      pile[i] = 1;
    else
      pile[i] = pile[i - 1] * i % inf;
  }

  for (int i = 0; i < q; i++) {
    ll x, d, n;
    cin >> x >> d >> n;

    if (d == 0)
      cout << pw(x, n) << endl;
    else {
      if (dv(x, d) + n - 1 >= inf)
        cout << 0 << endl;
      else
        cout << pw(d, n) * pile[dv(x, d) + n - 1] % inf *
                    dv(1, pile[dv(x, d) - 1]) % inf
             << endl;
    }
  }
}
Problem p03027 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mod 1000003
#define fr first
#define se second
#define ll long long
#define PI 3.1415926535
#define pb push_back
#define mpr make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define senky_bansal ios_base::sync_with_stdio(false);
#define IIIT_ALLAHABAD                                                         \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
ll fac[1000003];
ll ifac[1000003];
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    a %= mod;
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b = b / 2;
  }
  return res % mod;
}
signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  senky_bansal IIIT_ALLAHABAD

      fac[0] = 1;
  fac[1] = 1;
  for (int i = 2; i < 1000003; i++)
    fac[i] = (i * fac[i - 1]) % mod;
  ifac[1000002] = power(fac[1000002], mod - 2);
  for (int i = 1000001; i >= 0; i--) {
    ifac[i] = ((i + 1) * ifac[i + 1]) % mod;
  }
  ll q;
  cin >> q;
  while (q--) {
    ll x, d, n;
    cin >> x >> d >> n;
    if (d == 0) {
      cout << power(x, n) << endl;
    } else {
      //   cout<<ifac[d]<<endl;
      // cout<<power(d,mod-2)<<endl;
      ll p = (x * power(d, mod - 2)) % mod;
      ll r = power(d, n);
      if (p + n - 1 >= mod) {
        cout << "0" << endl;
      } else {
        cout << ((fac[p + n - 1] * power(fac[p - 1], mod - 2)) % mod * r) % mod
             << endl;
      }
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define mod 1000003
#define fr first
#define se second
#define ll long long
#define PI 3.1415926535
#define pb push_back
#define mpr make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define senky_bansal ios_base::sync_with_stdio(false);
#define IIIT_ALLAHABAD                                                         \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
ll fac[1000003];
ll ifac[1000003];
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    a %= mod;
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b = b / 2;
  }
  return res % mod;
}
signed main() {
  senky_bansal IIIT_ALLAHABAD

      fac[0] = 1;
  fac[1] = 1;
  for (int i = 2; i < 1000003; i++)
    fac[i] = (i * fac[i - 1]) % mod;
  ifac[1000002] = power(fac[1000002], mod - 2);
  for (int i = 1000001; i >= 0; i--) {
    ifac[i] = ((i + 1) * ifac[i + 1]) % mod;
  }
  ll q;
  cin >> q;
  while (q--) {
    ll x, d, n;
    cin >> x >> d >> n;
    if (d == 0) {
      cout << power(x, n) << endl;
    } else {
      //   cout<<ifac[d]<<endl;
      // cout<<power(d,mod-2)<<endl;
      ll p = (x * power(d, mod - 2)) % mod;
      ll r = power(d, n);
      if (p + n - 1 >= mod) {
        cout << "0" << endl;
      } else {
        cout << ((fac[p + n - 1] * power(fac[p - 1], mod - 2)) % mod * r) % mod
             << endl;
      }
    }
  }
}
Problem p03027 (Python)

Buggy submission (status: Time Limit Exceeded):
# https://atcoder.jp/contests/m-solutions2019/submissions/5741430
from operator import mul

MOD = 10**6 + 3


def inv(n):
    return pow(n, MOD - 2, MOD)


def mod_mul(x, y):
    return mul(x, y) % MOD


fact = [1]
for n in range(1, MOD):
    fact.append(mod_mul(fact[-1], n))
    # d>1 の数列を d=1 となる数列に変換する
    # MODの倍数を含む数列の積は0になるので,
    # MODの倍数を含まない区間の積が計算できればよい
    # そのような区間の右端はMOD-1であり,fact[MOD-1]が計算できればよい

Q = int(input())
for _ in range(Q):
    x, d, n = map(int, input().split())

    if x == 0:
        print(0)
        continue

    if d == 0:
        print(pow(x, n, MOD))
        continue

    xd = mod_mul(x, inv(d))
    if MOD - xd < n:
        print(0)
        continue

    dn = pow(d, n, MOD)
    print(mod_mul(mod_mul(fact[xd + n - 1], inv(fact[xd - 1])), dn))


Fixed submission:
# https://atcoder.jp/contests/m-solutions2019/submissions/5741430
from operator import mul
import sys

input = sys.stdin.readline

MOD = 10**6 + 3


def inv(n):
    return pow(n, MOD - 2, MOD)


def mod_mul(x, y):
    return mul(x, y) % MOD


fact = [1]
for n in range(1, MOD):
    fact.append(mod_mul(fact[-1], n))
    # d>1 の数列を d=1 となる数列に変換する
    # MODの倍数を含む数列の積は0になるので,
    # MODの倍数を含まない区間の積が計算できればよい
    # そのような区間の右端はMOD-1であり,fact[MOD-1]が計算できればよい

Q = int(input())
for _ in range(Q):
    x, d, n = map(int, input().split())

    if x == 0:
        print(0)
        continue

    if d == 0:
        print(pow(x, n, MOD))
        continue

    xd = mod_mul(x, inv(d))
    if MOD - xd < n:
        print(0)
        continue

    dn = pow(d, n, MOD)
    print(mod_mul(mod_mul(fact[xd + n - 1], inv(fact[xd - 1])), dn))
Problem p03027 (C++)

Buggy submission (status: Runtime Error):
/*  -*- coding: utf-8 -*-
 *
 * e.cc: E - Product of Arithmetic Progression
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MOD = 1000003;
const int MAX_K = MOD * 2;

/* typedef */

typedef long long ll;

/* global variables */

int fracs[MAX_K + 1], invfs[MAX_K + 1];

/* subroutines */

ll powmod(ll a, int n) { // a^n % MOD
  ll pm = 1;
  while (n > 0) {
    if (n & 1)
      pm = (pm * a) % MOD;
    a = (a * a) % MOD;
    n >>= 1;
  }
  return pm;
}

/* main */

int main() {
  fracs[0] = invfs[0] = 1;
  for (int i = 1; i <= MAX_K; i++) {
    fracs[i] = (ll)fracs[i - 1] * i % MOD;
    invfs[i] = powmod(fracs[i], MOD - 2);
  }

  int q;
  scanf("%d", &q);

  while (q--) {
    int x, d, n;
    scanf("%d%d%d", &x, &d, &n);

    int sum = 0;
    if (d == 0)
      sum = powmod(x, n);
    else {
      int y = (ll)x * powmod(d, MOD - 2) % MOD;
      sum = fracs[y + n - 1];
      if (y > 0)
        sum = (ll)sum * invfs[y - 1] % MOD;
      sum = (ll)sum * powmod(d, n) % MOD;
    }

    printf("%d\n", sum);
  }
  return 0;
}


Fixed submission:
/*  -*- coding: utf-8 -*-
 *
 * e.cc: E - Product of Arithmetic Progression
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MOD = 1000003;
const int MAX_K = MOD * 2;

/* typedef */

typedef long long ll;

/* global variables */

int fracs[MAX_K + 1], invfs[MAX_K + 1];

/* subroutines */

ll powmod(ll a, int n) { // a^n % MOD
  ll pm = 1;
  while (n > 0) {
    if (n & 1)
      pm = (pm * a) % MOD;
    a = (a * a) % MOD;
    n >>= 1;
  }
  return pm;
}

/* main */

int main() {
  fracs[0] = invfs[0] = 1;
  for (int i = 1; i <= MAX_K; i++) {
    fracs[i] = (ll)fracs[i - 1] * i % MOD;
    invfs[i] = powmod(fracs[i], MOD - 2);
  }

  int q;
  scanf("%d", &q);

  while (q--) {
    int x, d, n;
    scanf("%d%d%d", &x, &d, &n);

    int sum = 0;
    if (d == 0)
      sum = powmod(x, n);
    else if (n < MOD) {
      int y = (ll)x * powmod(d, MOD - 2) % MOD;
      sum = fracs[y + n - 1];
      if (y > 0)
        sum = (ll)sum * invfs[y - 1] % MOD;
      sum = (ll)sum * powmod(d, n) % MOD;
    }

    printf("%d\n", sum);
  }
  return 0;
}
Problem p03027 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define int long long
#define double long double
#define mod 1000003
#define P pair<long long, long long>
#define all(a) a.begin(), a.end()
#define INF 10000000000000000
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

// power_mod
inline int Pow_mod(int x, int pow) {
  if (pow == 0)
    return 1;
  int a = Pow_mod(x, pow / 2);
  a = a * a % mod;
  if (pow % 2 == 1)
    a *= x;
  return a % mod;
}

int fact[1000010];

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

  fact[0] = 1;
  rep(i, 1000005) fact[i + 1] = fact[i] * (i + 1) % mod;

  int q;
  cin >> q;
  rep(i, q) {
    int x, d, n;
    cin >> x >> d >> n;
    if (d == 0) {
      cout << Pow_mod(x, n) << endl;
      continue;
    }
    int newx = x * Pow_mod(d, mod - 2) % mod;
    if (mod < newx + n or newx == 0)
      cout << 0 << endl;
    cout << fact[newx + n - 1] * Pow_mod(fact[newx - 1], mod - 2) % mod *
                Pow_mod(d, n) % mod
         << endl;
    ;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define int long long
#define double long double
#define mod 1000003
#define P pair<long long, long long>
#define all(a) a.begin(), a.end()
#define INF 10000000000000000
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

// power_mod
inline int Pow_mod(int x, int pow) {
  if (pow == 0)
    return 1;
  int a = Pow_mod(x, pow / 2);
  a = a * a % mod;
  if (pow % 2 == 1)
    a *= x;
  return a % mod;
}

int fact[1000010];

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

  fact[0] = 1;
  rep(i, 1000005) fact[i + 1] = fact[i] * (i + 1) % mod;

  int q;
  cin >> q;
  rep(i, q) {
    int x, d, n;
    cin >> x >> d >> n;
    if (d == 0) {
      cout << Pow_mod(x, n) << endl;
      continue;
    }
    int newx = x * Pow_mod(d, mod - 2) % mod;
    if (mod < newx + n or newx == 0)
      cout << 0 << endl;
    else
      cout << fact[newx + n - 1] * Pow_mod(fact[newx - 1], mod - 2) % mod *
                  Pow_mod(d, n) % mod
           << endl;
  }
}
Problem p03028 (C++)

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

using namespace std;

#define N 5

int n, ans;
bitset<N> a[N], f[N], g[N];
char s[N][N];

int main() {
  scanf("%d", &n);
  for (int i = 2; i <= n; ++i) {
    scanf("%s", s[i] + 1);
    for (int j = 1; j < i; ++j) {
      a[i][j] = s[i][j] - '0';
      a[j][i] = a[i][j] ^ 1;
    }
  }
  for (int i = 1; i <= n; ++i) {
    f[i][i] = g[i][i] = 1;
    for (int j = 1; j < i; ++j)
      if (g[i - 1][j] && a[i][j])
        f[i] |= f[j];
    for (int j = 1; j < i; ++j)
      if (f[i][j + 1] && a[j][i])
        g[i] |= g[j];
  }
  for (int i = 1; i <= n; ++i)
    ans += f[i][1] && g[n][i];
  printf("%d\n", ans);

  return 0;
}


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

using namespace std;

#define N 2005

int n, ans;
bitset<N> a[N], f[N], g[N];
char s[N][N];

int main() {
  scanf("%d", &n);
  for (int i = 2; i <= n; ++i) {
    scanf("%s", s[i] + 1);
    for (int j = 1; j < i; ++j) {
      a[i][j] = s[i][j] - '0';
      a[j][i] = a[i][j] ^ 1;
    }
  }
  for (int i = 1; i <= n; ++i) {
    f[i][i] = g[i][i] = 1;
    for (int j = 1; j < i; ++j)
      if (g[i - 1][j] && a[i][j])
        f[i] |= f[j];
    for (int j = 1; j < i; ++j)
      if (f[i][j + 1] && a[j][i])
        g[i] |= g[j];
  }
  for (int i = 1; i <= n; ++i)
    ans += f[i][1] && g[n][i];
  printf("%d\n", ans);

  return 0;
}
Problem p03028 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
int main() {
  std::cin.tie(0);
  std::cin.sync_with_stdio(false);
  size_t n;
  std::cin >> n;
  constexpr size_t max = 2002;
  auto a = std::vector<std::bitset<max>>(n + 2);
  auto insert = [&](size_t i, size_t j, bool k) {
    a[i][j] = k;
    a[j][i] = !k;
  };
  for (size_t i = 1; i <= n; i++) {
    insert(i, 0, true);
    insert(i, n + 1, true);
  }
  for (size_t i = 2; i <= n; i++) {
    for (size_t j = 1; j < i; j++) {
      char x;
      std::cin >> x;
      insert(i, j, x == '1');
    }
  }
  auto dp = std::vector<std::bitset<max>>(n + 2);
  for (size_t i = 0; i <= n; i++)
    dp[i][i + 1] = dp[i + 1][i] = true;
  for (size_t d = 2; d <= n + 1; d++) {
    for (size_t i = 0, j = d; j <= n + 1; i++, j++) {
      bool tmp = false;
      for (size_t k = i + 1; k < j; k++) {
        tmp |= dp[i][k] & dp[j][k] & (a[i][k] | a[j][k]);
      }
      dp[j][i] = dp[i][j] = tmp;
    }
  }
  int ret = 0;
  for (size_t i = 1; i <= n; i++) {
    if (dp[0][i] & dp[i][n + 1])
      ret++;
  }
  std::cout << ret << std::endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
int main() {
  std::cin.tie(0);
  std::cin.sync_with_stdio(false);
  size_t n;
  std::cin >> n;
  constexpr size_t max = 2002;
  auto a = std::vector<std::bitset<max>>(n + 2);
  auto insert = [&](size_t i, size_t j, bool k) {
    a[i][j] = k;
    a[j][i] = !k;
  };
  for (size_t i = 1; i <= n; i++) {
    insert(i, 0, true);
    insert(i, n + 1, true);
  }
  for (size_t i = 2; i <= n; i++) {
    for (size_t j = 1; j < i; j++) {
      char x;
      std::cin >> x;
      insert(i, j, x == '1');
    }
  }
  auto dp = std::vector<std::bitset<max>>(n + 2);
  for (size_t i = 0; i <= n; i++)
    dp[i][i + 1] = dp[i + 1][i] = true;
  for (size_t d = 2; d <= n + 1; d++) {
    for (size_t i = 0, j = d; j <= n + 1; i++, j++) {
      dp[j][i] = dp[i][j] = (dp[i] & dp[j] & (a[i] | a[j])).any();
    }
  }
  int ret = 0;
  for (size_t i = 1; i <= n; i++) {
    if (dp[0][i] & dp[i][n + 1])
      ret++;
  }
  std::cout << ret << std::endl;
  return 0;
}
Problem p03029 (C++)

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

#define ll long long
#define mod 1000000007
#define pll pair<long long, long long>
#define pdd pair<long double, long double>
#define mp make_pair
#define pb push_back
#define pf push_front
priority_queue<pll, vector<pll>, greater<pll>> pq;
priority_queue<ll, vector<ll>, greater<ll>> p;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll a, p;
  cin >> a >> p;
  a *= 3;
  a += p;
  cout << a / p;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007
#define pll pair<long long, long long>
#define pdd pair<long double, long double>
#define mp make_pair
#define pb push_back
#define pf push_front
priority_queue<pll, vector<pll>, greater<pll>> pq;
priority_queue<ll, vector<ll>, greater<ll>> p;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll a, p;
  cin >> a >> p;
  a *= 3;
  a += p;
  cout << a / 2;
  return 0;
}
Problem p03029 (C++)

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

int main(void) {
  int A, P;
  std::cin >> A >> P;
  P += 3 * A;
  return P / 2;
}

Fixed submission:
#include <iostream>

int main(void) {
  int A, P;
  std::cin >> A >> P;
  P += 3 * A;
  std::cout << P / 2;
  return 0;
}