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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define X first
#define Y second

ll inp[10001];
int main() {
  ll out = 0;
  int N;
  ll x;
  cin >> N >> x;
  for (int i = 0; i < N; i++) {
    cin >> inp[i];
  }
  /*
  if (inp[0] + inp[1] > x){
          out += x - (inp[0] + inp[1]);
          inp[1] = x - inp[0];
          if (inp[1] < 0) {
                  inp[0] -= inp[1];
                  inp[1] = 0;
          }
  }
  */
  for (int i = 1; i < N; i++) {
    if (inp[i - 1] + inp[i] > x) {
      out += (inp[i - 1] + inp[i]) - x;
      inp[i] = x - inp[i - 1];
      if (inp[i] < 0) {
        inp[i - 1] -= inp[i];
        inp[i] = 0;
      }
    }
  }

  cout << out << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define X first
#define Y second

ll inp[100001];
int main() {
  ll out = 0;
  int N;
  ll x;
  cin >> N >> x;
  for (int i = 0; i < N; i++) {
    cin >> inp[i];
  }
  /*
  if (inp[0] + inp[1] > x){
          out += x - (inp[0] + inp[1]);
          inp[1] = x - inp[0];
          if (inp[1] < 0) {
                  inp[0] -= inp[1];
                  inp[1] = 0;
          }
  }
  */
  for (int i = 1; i < N; i++) {
    if (inp[i - 1] + inp[i] > x) {
      out += (inp[i - 1] + inp[i]) - x;
      inp[i] = x - inp[i - 1];
      if (inp[i] < 0) {
        inp[i - 1] -= inp[i];
        inp[i] = 0;
      }
    }
  }

  cout << out << endl;

  return 0;
}
Problem p03864 (C++)

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

using namespace std;

long long calc(vector<int> boxes, long long x) {
  int n = boxes.size();
  long long last = boxes[n - 1] + boxes[n - 2], ldiff = last - x,
            lsub = max(0ll, ldiff);
  boxes[n - 2] -= lsub;
  if (boxes[n - 2] < 0) {
    boxes[n - 1] += boxes[n - 2];
    boxes[n - 2] = 0;
  }
  long long result = lsub;
  for (int i = 0; i <= n - 1; i++) {
    long long sum = boxes[i] + boxes[i + 1], diff = sum - x,
              sub = max(0ll, diff);
    boxes[i + 1] -= sub;
    if (boxes[i + 1] < 0)
      boxes[i + 1] = 0;
    result += sub;
  }
  return result;
}

signed main() {
  long long n, x;
  cin >> n >> x;
  vector<int> fwd(n), bck;
  for (auto &i : fwd) {
    cin >> i;
    bck.push_back(i);
  }
  reverse(begin(bck), end(bck));
  auto left = calc(move(fwd), x), right = calc(move(bck), x);
  cout << min(left, right) << endl;
}


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

using namespace std;

long long calc(vector<int> boxes, long long x) {
  int n = boxes.size();
  long long last = boxes[n - 1] + boxes[n - 2], ldiff = last - x,
            lsub = max(0ll, ldiff);
  boxes[n - 2] -= lsub;
  if (boxes[n - 2] < 0) {
    boxes[n - 1] += boxes[n - 2];
    boxes[n - 2] = 0;
  }
  long long result = lsub;
  for (int i = 0; i < n - 1; i++) {
    long long sum = boxes[i] + boxes[i + 1], diff = sum - x,
              sub = max(0ll, diff);
    boxes[i + 1] -= sub;
    if (boxes[i + 1] < 0)
      boxes[i + 1] = 0;
    result += sub;
  }
  return result;
}

signed main() {
  long long n, x;
  cin >> n >> x;
  vector<int> fwd(n), bck;
  for (auto &i : fwd) {
    cin >> i;
    bck.push_back(i);
  }
  reverse(begin(bck), end(bck));
  auto left = calc(move(fwd), x), right = calc(move(bck), x);
  cout << min(left, right) << endl;
}
Problem p03864 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define INF 1000000010
const ll MOD = 1000000007;

int main() {
  int n, x;
  cin >> n >> x;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;
  rep(i, n - 1) {
    while (a[i] + a[i + 1] > x) {
      ans++;
      if (a[i + 1])
        a[i + 1]--;
      else
        a[i]--;
    }
  }
  cout << ans << endl;
}

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

int main() {
  int n, x;
  cin >> n >> x;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;
  rep(i, n - 1) {
    if (a[i] + a[i + 1] > x) {
      int diff = a[i] + a[i + 1] - x;
      ans += diff;
      a[i + 1] -= min(diff, a[i + 1]);
    }
  }
  cout << ans << endl;
}
Problem p03865 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

map<list<char>, int> m;

int dfs(list<char> ls) {
  if (m.count(ls))
    m[ls];

  set<int> ret;
  auto end = --ls.end();
  for (auto it = ++ls.begin(); it != end; it++) {
    if (*prev(it, 1) != *it && *prev(it, 1) != *next(it, 1) &&
        *it != *next(it, 1)) {
      // for(auto i:ls){ cout << i << ' '; } cout << endl;
      char foo = *it;
      it = ls.erase(it);
      ret.emplace(dfs(ls));
      ls.insert(it, foo);
      // it++;
      --it;
    }
  }

  rep(i, 1e8) if (not ret.count(i)) return m[ls] = i;
  return 1e8;
}

int main() {
  string s;
  cin >> s;
  list<char> ls(all(s));
  cout << (dfs(ls) % 2 ? "First" : "Second") << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

map<list<char>, int> m;

int dfs(list<char> ls) {
  if (m.count(ls))
    m[ls];

  set<int> ret;
  auto end = --ls.end();
  for (auto it = ++ls.begin(); it != end; it++) {
    if (*prev(it, 1) != *it && *prev(it, 1) != *next(it, 1) &&
        *it != *next(it, 1)) {
      // for(auto i:ls){ cout << i << ' '; } cout << endl;
      char foo = *it;
      it = ls.erase(it);
      ret.emplace(dfs(ls));
      ls.insert(it, foo);
      // it++;
      --it;
    }
  }

  rep(i, 1e8) if (not ret.count(i)) return m[ls] = i;
  return 1e8;
}

int main() {
  string s;
  cin >> s;
  bool f = (s.front() != s.back()) xor (s.size() % 2 == 0);
  cout << (f ? "First" : "Second") << endl;
}
Problem p03865 (C++)

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

using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#define DBG(fmt, ...) printf(fmt, __VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

#define MOD 1000000007
#define INF 1000000000
#define MAX_N 100010

// 取れる場所ならどこ取っても結果変わらない説

string S;

// s : 現在の文字列
// player : 取る番の人
// 0 ... first 1 ... second
int dfs(string s, int player) {
  int next = (player == 0) ? 1 : 0;
  if (s.length() <= 2) {
    return next;
  }
  for (int i = 1; i < s.length() - 1; ++i) {
    if (s[i - 1] != s[i + 1]) {
      s.erase(s.begin() + i);
      return dfs(s, next);
    }
  }
  return next;
}

int main() {
  cin >> S;
  if (dfs(S, 0) == 0) {
    printf("First\n");
  } else {
    printf("Second\n");
  }

  return 0;
}


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

using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#define DBG(fmt, ...) printf(fmt, __VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

#define MOD 1000000007
#define INF 1000000000
#define MAX_N 100010

// 取れる場所ならどこ取っても結果変わらない説

string S;

// s : 現在の文字列
// player : 取る番の人
// 0 ... first 1 ... second
int dfs(string s, int player) {
  int next = (player == 0) ? 1 : 0;
  if (s.length() <= 2) {
    return next;
  }
  for (int i = 1; i < s.length() - 1; ++i) {
    if (s[i - 1] != s[i + 1]) {
      s.erase(s.begin() + i);
      return dfs(s, next);
    }
  }
  return next;
}

int main() {
  cin >> S;
  // if(dfs(S, 0) == 0) { printf("First\n"); }
  // else { printf("Second\n"); }
  size_t getable = 0;
  if (S[0] != S[S.length() - 1]) {
    // 両端が違う文字 → 残り 2 文字までいける
    getable = S.length() - 2;
  } else {
    // 両端が同じ文字
    set<char> alph;
    for (int i = 0; i < S.length(); ++i) {
      alph.insert(S[i]);
    }
    if (alph.size() >= 3) {
      // 3 種類以上あるなら n-1 取れる
      getable = S.length() - 3;
    } else {
      getable = 0;
    }
  }
  if (getable % 2 != 0) {
    printf("First\n");
  } else {
    printf("Second\n");
  }

  return 0;
}
Problem p03865 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PI 4 * atan(1.0)
typedef long long ll;
typedef long double ld;
using namespace std;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};

int main() {
  char s[10001];
  cin >> s;
  if (strlen(s) % 2) {
    if (s[0] == s[strlen(s) - 1])
      cout << "Second" << endl;
    else
      cout << "First" << endl;
  } else {
    if (s[0] != s[strlen(s) - 1])
      cout << "Second" << endl;
    else
      cout << "First" << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PI 4 * atan(1.0)
typedef long long ll;
typedef long double ld;
using namespace std;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};

int main() {
  char s[100001];
  cin >> s;
  if (strlen(s) % 2) {
    if (s[0] == s[strlen(s) - 1])
      cout << "Second" << endl;
    else
      cout << "First" << endl;
  } else {
    if (s[0] != s[strlen(s) - 1])
      cout << "Second" << endl;
    else
      cout << "First" << endl;
  }
  return 0;
}
Problem p03865 (Python)

Buggy submission (status: Runtime Error):
#!/usr/bin/env python
# -*- coding:utf-8 -*-

s = input()
if len(s) % 2 == 0 ^ s[0] == s[-1]:
    print("Second")
else:
    print("First")


Fixed submission:
#!/usr/bin/env python
# -*- coding:utf-8 -*-

s = input()
if (len(s) % 2 == 0) and (s[0] == s[-1]):
    print("First")
elif (len(s) % 2 == 0) or (s[0] == s[-1]):
    print("Second")
else:
    print("First")
Problem p03866 (Python)

Buggy submission (status: Time Limit Exceeded):
from heapq import heappush, heappop

sX, sY, tX, tY = map(int, input().split())
N = int(input())

XYR = [tuple(map(int, input().split())) for _ in range(N)] + [(sX, sY, 0), (tX, tY, 0)]
S, T = N, N + 1
N += 2

edges = [[] for _ in range(N)]
for i, (x, y, r) in enumerate(XYR):
    for j, (u, v, w) in enumerate(XYR[i + 1 :], start=i + 1):
        dist = max(0, ((x - u) ** 2 + (y - v) ** 2) ** 0.5 - (r + w))
        edges[i].append((j, dist))
        edges[j].append((i, dist))

minDist = [10**18] * N
minDist[S] = 0
que = [(0, S)]
while que:
    dist, now = heappop(que)
    for to, cost in edges[now]:
        d = dist + cost
        if minDist[to] > d:
            minDist[to] = d
            heappush(que, (d, to))
print(minDist[T])


Fixed submission:
from heapq import heappush, heappop

sX, sY, tX, tY = map(int, input().split())
N = int(input())

XYR = [tuple(map(int, input().split())) for _ in range(N)] + [(sX, sY, 0), (tX, tY, 0)]
S, T = N, N + 1
N += 2

edges = [[] for _ in range(N)]
for i, (x, y, r) in enumerate(XYR):
    for j, (u, v, w) in enumerate(XYR[i + 1 :], start=i + 1):
        dist = max(0, ((x - u) ** 2 + (y - v) ** 2) ** 0.5 - (r + w))
        edges[i].append((j, dist))
        edges[j].append((i, dist))

minDist = [10**18] * N
minDist[S] = 0
que = [(0, S)]
while que:
    dist, now = heappop(que)
    if minDist[now] < dist:
        continue
    for to, cost in edges[now]:
        d = dist + cost
        if minDist[to] > d:
            minDist[to] = d
            heappush(que, (d, to))
print(minDist[T])
Problem p03866 (Python)

Buggy submission (status: Runtime Error):
#!/usr/bin/env pypy3

import math
import itertools


INVALID = -1
INF = 10**12


def dijkstra_dense(num_vs, adj_matrix, source, dest):
    dist = [INF for _ in range(num_vs)]
    visited = [False for _ in range(num_vs)]
    dist[source] = 0
    while True:
        u = min(
            (j for j in range(num_vs) if not visited[j]),
            key=lambda i: dist[i],
            default=INVALID,
        )
        if u == INVALID or dist[u] == INF:
            break
        visited[u] = True
        for v in range(num_vs):
            w = adj_matrix[u][v]
            if u != v and w < INF:
                new_len = dist[u] + w
                if new_len < dist[v]:
                    dist[v] = new_len
    return dist[dest]


def main():
    # 始点を頂点0、終点を頂点N + 1とする
    x0, y0, xf, yf = (int(x) for x in input().split())
    n = int(input())
    xs = [None for _ in range(n + 2)]
    ys = [None for _ in range(n + 2)]
    rs = [None for _ in range(n + 2)]
    xs[0], ys[0], rs[0] = x0, y0, 0
    xs[n + 1], ys[n + 1], rs[n + 1] = xf, yf, 0
    for i in range(1, n + 1):
        xs[i], ys[i], rs[i] = (int(z) for z in input().split())
    adj_matrix = [[INF for _ in range(n + 2)] for _ in range(n + 2)]
    for i, j in itertools.product(range(n + 2), repeat=2):
        adj_matrix[i][j] = max(
            0, math.hypot(xs[i] - xs[j], ys[i] - ys[j]) - (rs[i] + rs[j])
        )
    res = dijkstra_dense(n + 2, adj_matrix, 0, n + 1)
    print("{:.12f}".format(res))


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3

import math
import itertools


INVALID = -1
INF = 10**12


def dijkstra_dense(num_vs, adj_matrix, source, dest):
    dist = [INF for _ in range(num_vs)]
    visited = [False for _ in range(num_vs)]
    dist[source] = 0
    while True:
        u = min(
            (j for j in range(num_vs) if not visited[j]),
            key=lambda i: dist[i],
            default=INVALID,
        )
        if u == INVALID or dist[u] == INF:
            break
        visited[u] = True
        for v in range(num_vs):
            w = adj_matrix[u][v]
            if u != v and w < INF:
                new_len = dist[u] + w
                if new_len < dist[v]:
                    dist[v] = new_len
    return dist[dest]


def main():
    # 始点を頂点0、終点を頂点N + 1とする
    x0, y0, xf, yf = (int(x) for x in input().split())
    n = int(input())
    xs = [None for _ in range(n + 2)]
    ys = [None for _ in range(n + 2)]
    rs = [None for _ in range(n + 2)]
    xs[0], ys[0], rs[0] = x0, y0, 0
    xs[n + 1], ys[n + 1], rs[n + 1] = xf, yf, 0
    for i in range(1, n + 1):
        xs[i], ys[i], rs[i] = (int(z) for z in input().split())
    adj_matrix = [[INF for _ in range(n + 2)] for _ in range(n + 2)]
    for i, j in itertools.product(range(n + 2), repeat=2):
        adj_matrix[i][j] = max(
            0, math.hypot(xs[i] - xs[j], ys[i] - ys[j]) - (rs[i] + rs[j])
        )
    res = dijkstra_dense(n + 2, adj_matrix, 0, n + 1)
    print("{:.12f}".format(res))


if __name__ == "__main__":
    main()
Problem p03866 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define x first
#define y second
#define sqr(x) ((x) * (x))
#define mp(x, y) make_pair((x), (y))
using namespace std;
double m[1005][1005];
int main() {
  pair<double, double> p[1005];
  scanf("%lf %lf", &p[0].x, &p[0].y);
  scanf("%lf %lf", &p[1].x, &p[1].y);
  int n;
  double r[1005];
  r[0] = 0, r[1] = 0;
  scanf("%d", &n);
  for (int i = 2; i <= n + 1; i++)
    scanf("%lf %lf %lf", &p[i].x, &p[i].y, &r[i]);
  for (int i = 0; i < n + 2; i++)
    for (int j = 0; j < n + 2; j++) {
      m[i][j] = max(
          sqrt(sqr(p[i].x - p[j].x) + sqr(p[i].y - p[j].y)) - r[i] - r[j], 0.0);
    }
  priority_queue<pair<double, int>, vector<pair<double, int>>,
                 greater<pair<double, int>>>
      pq;
  pq.push(mp(0, 0));
  int visit[1005];
  memset(visit, 0, sizeof(visit));
  while (!pq.empty()) {
    pair<double, int> p = pq.top();
    pq.pop();
    visit[p.y] = 1;
    if (p.y == 1) {
      printf("%.10lf\n", p.x);
      return 0;
    }
    for (int i = 0; i < n + 2; i++) {
      if (!visit[i])
        pq.push(mp(p.x + m[p.y][i], i));
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define x first
#define y second
#define sqr(x) ((x) * (x))
#define mp(x, y) make_pair((x), (y))
using namespace std;
double m[1005][1005];
int main() {
  pair<double, double> p[1005];
  scanf("%lf %lf", &p[0].x, &p[0].y);
  scanf("%lf %lf", &p[1].x, &p[1].y);
  int n;
  double r[1005];
  r[0] = 0, r[1] = 0;
  scanf("%d", &n);
  for (int i = 2; i <= n + 1; i++)
    scanf("%lf %lf %lf", &p[i].x, &p[i].y, &r[i]);
  for (int i = 0; i < n + 2; i++)
    for (int j = 0; j < n + 2; j++) {
      m[i][j] = max(
          sqrt(sqr(p[i].x - p[j].x) + sqr(p[i].y - p[j].y)) - r[i] - r[j], 0.0);
    }
  priority_queue<pair<double, int>, vector<pair<double, int>>,
                 greater<pair<double, int>>>
      pq;
  pq.push(mp(0, 0));
  int visit[1005];
  memset(visit, 0, sizeof(visit));
  while (!pq.empty()) {
    pair<double, int> p = pq.top();
    pq.pop();
    if (visit[p.y])
      continue;
    visit[p.y] = 1;
    if (p.y == 1) {
      printf("%.10lf\n", p.x);
      return 0;
    }
    for (int i = 0; i < n + 2; i++) {
      if (!visit[i])
        pq.push(mp(p.x + m[p.y][i], i));
    }
  }
}
Problem p03866 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;

std::vector<std::vector<std::pair<int, double>>> g;
int n;

double solve() {
  std::vector<double> min_cost(n, 1e18);
  std::priority_queue<std::pair<double, int>> qu;
  qu.push({0, 0});
  min_cost[0] = 0;

  while (!qu.empty()) {
    auto p = qu.top();
    qu.pop();

    for (auto e : g[p.second]) {
      if (p.first + e.second >= min_cost[e.first])
        continue;
      min_cost[e.first] = p.first + e.second;
      qu.push({min_cost[e.first], e.first});
    }
  }

  return min_cost[1];
}

int main() {
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  cin >> n;
  n += 2;
  g.resize(n);
  std::vector<double> a(n), b(n), c(n);
  for (int i = 2; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];
  a[0] = sx, b[0] = sy, c[0] = 0;
  a[1] = gx, b[1] = gy, c[1] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
      double dist =
          sqrt((a[i] - a[j]) * (a[i] - a[j]) + (b[i] - b[j]) * (b[i] - b[j]));
      double circle = c[i] + c[j];
      dist = std::max(0.0, dist - circle);

      g[i].push_back({j, dist});
      g[j].push_back({i, dist});
    }
  }

  cout << std::fixed << std::setprecision(10);
  cout << solve() << endl;
  return 0;
}


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

std::vector<std::vector<std::pair<int, double>>> g;
int n;

double solve() {
  std::vector<double> min_cost(n, 1e18);
  std::priority_queue<std::pair<double, int>,
                      std::vector<std::pair<double, int>>,
                      std::greater<std::pair<double, int>>>
      qu;
  qu.push({0, 0});
  min_cost[0] = 0;

  while (!qu.empty()) {
    auto p = qu.top();
    qu.pop();

    for (auto e : g[p.second]) {
      if (p.first + e.second >= min_cost[e.first])
        continue;
      min_cost[e.first] = p.first + e.second;
      qu.push({min_cost[e.first], e.first});
    }
  }

  return min_cost[1];
}

int main() {
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  cin >> n;
  n += 2;
  g.resize(n);
  std::vector<double> a(n), b(n), c(n);
  for (int i = 2; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];
  a[0] = sx, b[0] = sy, c[0] = 0;
  a[1] = gx, b[1] = gy, c[1] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
      double dist =
          sqrt((a[i] - a[j]) * (a[i] - a[j]) + (b[i] - b[j]) * (b[i] - b[j]));
      double circle = c[i] + c[j];
      dist = std::max(0.0, dist - circle);

      g[i].push_back({j, dist});
      g[j].push_back({i, dist});
    }
  }

  cout << std::fixed << std::setprecision(10);
  cout << solve() << endl;
  return 0;
}
Problem p03866 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <queue>
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define all(a) (a).begin(), (a).end()

constexpr int MAX_N = 1000;
constexpr double INF = 1e12;

using P = std::pair<double, int>;

int xs, ys, xt, yt;
int N;
int x[MAX_N], y[MAX_N], r[MAX_N];
double cost[MAX_N][MAX_N];
std::priority_queue<P, std::vector<P>, std::greater<P>> pque;
double d[MAX_N];

double sqr(double x) { return x * x; }

int main() {
  scanf("%d%d%d%d%d", &xs, &ys, &xt, &yt, &N);
  rep(i, N) scanf("%d%d%d", x + i + 1, y + i + 1, r + i + 1);

  ++N;

  x[0] = xs;
  y[0] = ys;
  x[N] = xt;
  y[N] = yt;

  ++N;

  rep(i, N) rep(j, i) cost[i][j] = cost[j][i] = std::max<double>(
      0, sqrt(sqr(x[j] - x[i]) + sqr(y[j] - y[i])) - r[i] - r[j]);

  std::fill(d, d + N, INF);

  pque.push(P(0, 0));
  d[0] = 0;

  while (!pque.empty()) {
    P p = pque.top();
    pque.pop();

    int v = p.second;
    double dist = p.first;

    if (d[v] < dist)
      continue;

    rep(i, N) if (i != v) {
      if (d[i] > d[v] + cost[v][i]) {
        d[i] = d[v] + cost[v][i];
        pque.push(P(d[i], i));
      }
    }
  }

  printf("%.10f\n", d[N - 1]);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <queue>
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define all(a) (a).begin(), (a).end()

constexpr int MAX_N = 1010;
constexpr double INF = 1e12;

using P = std::pair<double, int>;

int xs, ys, xt, yt;
int N;
int x[MAX_N], y[MAX_N], r[MAX_N];
double cost[MAX_N][MAX_N];
std::priority_queue<P, std::vector<P>, std::greater<P>> pque;
double d[MAX_N];

double sqr(double x) { return x * x; }

int main() {
  scanf("%d%d%d%d%d", &xs, &ys, &xt, &yt, &N);
  rep(i, N) scanf("%d%d%d", x + i + 1, y + i + 1, r + i + 1);

  ++N;

  x[0] = xs;
  y[0] = ys;
  x[N] = xt;
  y[N] = yt;

  ++N;

  rep(i, N) rep(j, i) cost[i][j] = cost[j][i] = std::max<double>(
      0, sqrt(sqr(x[j] - x[i]) + sqr(y[j] - y[i])) - r[i] - r[j]);

  std::fill(d, d + N, INF);

  pque.push(P(0, 0));
  d[0] = 0;

  while (!pque.empty()) {
    P p = pque.top();
    pque.pop();

    int v = p.second;
    double dist = p.first;

    if (d[v] < dist)
      continue;

    rep(i, N) if (i != v) {
      if (d[i] > d[v] + cost[v][i]) {
        d[i] = d[v] + cost[v][i];
        pque.push(P(d[i], i));
      }
    }
  }

  printf("%.10f\n", d[N - 1]);

  return 0;
}
Problem p03866 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <vector>
#define ll long long
#define fr first
#define sc second
#define ii pair<double, int>

using namespace std;
vector<vector<pair<double, int>>> v;
vector<double> d(103);
int bfs(int i) {
  for (int j = 0; j < d.size(); j++)
    d[j] = 1e15;
  d[i] = 0;
  priority_queue<ii, vector<ii>, greater<ii>> q;
  q.push(ii(0, i));
  while (!q.empty()) {
    ii R = q.top();
    q.pop();
    double L = R.fr;
    int u = R.sc;
    if (L > d[u])
      continue;
    for (int j = 0; j < v[u].size(); j++) {
      ii node = v[u][j];
      if (d[node.sc] > d[u] + node.fr) {
        d[node.sc] = d[u] + node.fr;
        q.push(ii(d[node.sc], node.sc));
      }
    }
  }
}
struct point {
  int x, y;
};
struct circle {
  point p;
  int r;
} a[1005];

double distanc(circle b, circle c) {
  double t = hypot(b.p.x - c.p.x, b.p.y - c.p.y);
  if (t > b.r + c.r)
    return t - (b.r + c.r);
  else
    return 0;
}
int main() {
  int x1, y1, x2, y2, n;
  v.resize(1005);
  cin >> x1 >> y1 >> x2 >> y2 >> n;
  a[0].p.x = x1;
  a[0].p.y = y1;
  a[0].r = 0;
  a[n + 1].p.x = x2;
  a[n + 1].p.y = y2;
  a[n + 1].r = 0;
  for (int i = 1; i <= n; i++) {
    int x, y, r;
    cin >> x >> y >> r;
    a[i].p.x = x;
    a[i].p.y = y;
    a[i].r = r;
  }
  for (int i = 0; i <= n + 1; i++)
    for (int j = i + 1; j <= n + 1; j++) {
      double di = distanc(a[i], a[j]);
      v[i].push_back(ii(di, j));
      v[j].push_back(ii(di, i));
    }
  bfs(0);
  cout << fixed << setprecision(10) << d[n + 1];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <vector>
#define ll long long
#define fr first
#define sc second
#define ii pair<double, int>

using namespace std;
vector<vector<pair<double, int>>> v;
vector<double> d(1005);
int bfs(int i) {
  for (int j = 0; j < d.size(); j++)
    d[j] = 1e15;
  d[i] = 0;
  priority_queue<ii, vector<ii>, greater<ii>> q;
  q.push(ii(0, i));
  while (!q.empty()) {
    ii R = q.top();
    q.pop();
    double L = R.fr;
    int u = R.sc;
    if (L > d[u])
      continue;
    for (int j = 0; j < v[u].size(); j++) {
      ii node = v[u][j];
      if (d[node.sc] > d[u] + node.fr) {
        d[node.sc] = d[u] + node.fr;
        q.push(ii(d[node.sc], node.sc));
      }
    }
  }
}
struct point {
  int x, y;
};
struct circle {
  point p;
  int r;
} a[1005];

double distanc(circle b, circle c) {
  double t = hypot(b.p.x - c.p.x, b.p.y - c.p.y);
  if (t > b.r + c.r)
    return t - (b.r + c.r);
  else
    return 0;
}
int main() {
  int x1, y1, x2, y2, n;
  v.resize(1005);
  cin >> x1 >> y1 >> x2 >> y2 >> n;
  a[0].p.x = x1;
  a[0].p.y = y1;
  a[0].r = 0;
  a[n + 1].p.x = x2;
  a[n + 1].p.y = y2;
  a[n + 1].r = 0;
  for (int i = 1; i <= n; i++) {
    int x, y, r;
    cin >> x >> y >> r;
    a[i].p.x = x;
    a[i].p.y = y;
    a[i].r = r;
  }
  for (int i = 0; i <= n + 1; i++)
    for (int j = i + 1; j <= n + 1; j++) {
      double di = distanc(a[i], a[j]);
      v[i].push_back(ii(di, j));
      v[j].push_back(ii(di, i));
    }
  bfs(0);
  cout << fixed << setprecision(10) << d[n + 1];
  return 0;
}
Problem p03866 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int Nmax = 1000;
int N;
ll Xs, Ys, Xt, Yt, X[Nmax + 2], Y[Nmax + 2], R[Nmax + 2];
double dist[Nmax + 2];

int main() {
  cin >> Xs >> Ys >> Xt >> Yt;
  cin >> N;
  for (int i = 1; i < N + 1; i++) {
    cin >> X[i] >> Y[i] >> R[i];
  }
  X[0] = Xs;
  Y[0] = Ys;
  R[0] = 0;
  X[N + 1] = Xt;
  Y[N + 1] = Yt;
  R[N + 1] = 0;

  for (int i = 0; i < N + 2; i++) {
    dist[i] = 1e18;
  }
  dist[0] = 0;

  priority_queue<pair<double, int>, vector<pair<double, int>>,
                 greater<pair<double, int>>>
      que;
  que.push(make_pair(0, 0));

  while (!que.empty()) {
    double d = que.top().first;
    int n = que.top().second;
    que.pop();
    if (n == N + 1)
      break;
    for (int i = 0; i < N + 2; i++) {
      if (n == i)
        continue;
      double dx = X[n] - X[i];
      double dy = Y[n] - Y[i];
      double di = max(0.0, sqrt(dx * dx + dy * dy) - R[n] - R[i]);
      if (d + di < dist[i]) {
        dist[i] = d + di;
        que.push(make_pair(dist[i], i));
      }
    }
  }

  cout << setprecision(10) << dist[N + 1] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int Nmax = 1000;
int N;
ll Xs, Ys, Xt, Yt, X[Nmax + 2], Y[Nmax + 2], R[Nmax + 2];
double dist[Nmax + 2];

int main() {
  cin >> Xs >> Ys >> Xt >> Yt;
  cin >> N;
  for (int i = 1; i < N + 1; i++) {
    cin >> X[i] >> Y[i] >> R[i];
  }
  X[0] = Xs;
  Y[0] = Ys;
  R[0] = 0;
  X[N + 1] = Xt;
  Y[N + 1] = Yt;
  R[N + 1] = 0;

  for (int i = 0; i < N + 2; i++) {
    dist[i] = 1e18;
  }
  dist[0] = 0;

  priority_queue<pair<double, int>, vector<pair<double, int>>,
                 greater<pair<double, int>>>
      que;
  que.push(make_pair(0, 0));

  while (!que.empty()) {
    double d = que.top().first;
    int n = que.top().second;
    que.pop();
    if (n == N + 1)
      break;
    if (dist[n] < d)
      continue;
    for (int i = 0; i < N + 2; i++) {
      if (n == i)
        continue;
      double dx = X[n] - X[i];
      double dy = Y[n] - Y[i];
      double di = max(0.0, sqrt(dx * dx + dy * dy) - R[n] - R[i]);
      if (d + di < dist[i]) {
        dist[i] = d + di;
        que.push(make_pair(dist[i], i));
      }
    }
  }

  cout << setprecision(10) << dist[N + 1] << endl;

  return 0;
}
Problem p03866 (C++)

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

using namespace std;

int main() {
  int x[1002];
  int y[1002];
  int r[1002];
  r[0] = r[1] = 0;

  scanf("%d %d %d %d", &x[0], &y[0], &x[1], &y[1]);

  int n;
  scanf("%d", &n);
  n += 2;
  for (int i = 2; i < n; i++) {
    scanf("%d %d %d", x + i, y + i, r + i);
  }

  priority_queue<pair<double, int>> pq;

  vector<int> trace;

  double d[1002];
  for (int i = 0; i < 1002; i++)
    d[i] = DBL_MAX;
  pq.push(make_pair(0.0, 0));
  while (!pq.empty()) {
    double di = pq.top().first;
    int j = pq.top().second;
    pq.pop();
    // printf("%f %d\n",di,j);
    // if(find(trace.begin(),trace.end(),j) != trace.end()) continue;
    trace.emplace_back(j);
    if (d[j] > di) {
      d[j] = di;
    } else {
      continue;
    }
    d[j] = min(d[j], di);
    for (int i = 1; i < n; i++) {
      // cout << x[j] << " " << x[i] << " " << y[j] << " " << y[i] << " " <<
      // r[i] << " " << r[j] << endl;
      double distance =
          di + max(hypot(x[j] - x[i], y[j] - y[i]) - r[i] - r[j], 0.0);
      // cout << j << " - " << i << " " << distance << endl;
      pq.push(make_pair(distance, i));
    }
  }
  printf("%.10f\n", d[1]);
  return 0;
}

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

using namespace std;

int main() {
  int x[1002];
  int y[1002];
  int r[1002];
  r[0] = r[1] = 0;

  scanf("%d %d %d %d", &x[0], &y[0], &x[1], &y[1]);

  int n;
  scanf("%d", &n);
  n += 2;
  for (int i = 2; i < n; i++) {
    scanf("%d %d %d", x + i, y + i, r + i);
  }

  priority_queue<pair<double, int>, vector<pair<double, int>>,
                 greater<pair<double, int>>>
      pq;

  vector<int> trace;

  double d[1002];
  for (int i = 0; i < 1002; i++)
    d[i] = DBL_MAX;
  pq.push(make_pair(0.0, 0));
  while (!pq.empty()) {
    double di = pq.top().first;
    int j = pq.top().second;
    pq.pop();
    // printf("%f %d\n",di,j);
    // if(find(trace.begin(),trace.end(),j) != trace.end()) continue;
    trace.emplace_back(j);
    if (d[j] > di) {
      d[j] = di;
    } else {
      continue;
    }
    d[j] = min(d[j], di);
    for (int i = 1; i < n; i++) {
      // cout << x[j] << " " << x[i] << " " << y[j] << " " << y[i] << " " <<
      // r[i] << " " << r[j] << endl;
      double distance =
          di + max(hypot(x[j] - x[i], y[j] - y[i]) - r[i] - r[j], 0.0);
      // cout << j << " - " << i << " " << distance << endl;
      pq.push(make_pair(distance, i));
    }
  }
  printf("%.10f\n", d[1]);
  return 0;
}
Problem p03866 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <cstdio>
#include <limits>
#include <queue>

using namespace std;

int x[1002], y[1002], r[1002];
double cost[1002][1002];
double d[1002];

double norm(double x1, double x2) {
  double d = x1 - x2;
  return d * d;
}

int main() {
  scanf("%d%d%d%d", &x[0], &y[0], &x[1], &y[1]);
  r[0] = r[1] = 0;

  int N;
  scanf("%d", &N);
  N += 2;
  for (int i = 2; i < N; i++) {
    scanf("%d%d%d", &x[i], &y[i], &r[i]);
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cost[i][j] =
          max(sqrt(norm(x[i], x[j]) + norm(y[i], y[j])) - r[i] - r[j], 0.0);
    }
  }

  d[0] = 0;
  for (int i = 1; i < N; i++)
    d[i] = numeric_limits<double>::max();

  priority_queue<pair<double, int>> pq;
  pq.push(make_pair(d[0], 0));

  while (!pq.empty()) {
    auto p = pq.top();
    pq.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < N; i++) {
      if (d[i] > d[v] + cost[v][i]) {
        d[i] = d[v] + cost[v][i];
        pq.push(make_pair(d[i], i));
      }
    }
  }

  printf("%.10f\n", d[1]);
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <limits>
#include <queue>

using namespace std;

int x[1002], y[1002], r[1002];
double cost[1002][1002];
double d[1002];

double norm(double x1, double x2) {
  double d = x1 - x2;
  return d * d;
}

int main() {
  scanf("%d%d%d%d", &x[0], &y[0], &x[1], &y[1]);
  r[0] = r[1] = 0;

  int N;
  scanf("%d", &N);
  N += 2;
  for (int i = 2; i < N; i++) {
    scanf("%d%d%d", &x[i], &y[i], &r[i]);
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cost[i][j] =
          max(sqrt(norm(x[i], x[j]) + norm(y[i], y[j])) - r[i] - r[j], 0.0);
    }
  }

  d[0] = 0;
  for (int i = 1; i < N; i++)
    d[i] = numeric_limits<double>::max();

  priority_queue<pair<double, int>, vector<pair<double, int>>,
                 greater<pair<double, int>>>
      pq;
  pq.push(make_pair(d[0], 0));

  while (!pq.empty()) {
    auto p = pq.top();
    pq.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < N; i++) {
      if (d[i] > d[v] + cost[v][i]) {
        d[i] = d[v] + cost[v][i];
        pq.push(make_pair(d[i], i));
      }
    }
  }

  printf("%.10f\n", d[1]);
}
Problem p03867 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

typedef long long LL;

const int N = 1005;
const int MOD = 1000000007;

int n, k, tot, a[N], f[N];

int ksm(int x, int y) {
  int ans = 1;
  while (y) {
    if (y & 1)
      ans = (LL)ans * x % MOD;
    x = (LL)x * x % MOD;
    y >>= 1;
  }
  return ans;
}

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i * i <= n; i++)
    if (n % i == 0) {
      a[++tot] = i;
      if (i * i < n)
        a[++tot] = n / i;
    }
  sort(a + 1, a + tot + 1);
  int ny2 = ksm(2, MOD - 2), ans = 0;
  for (int i = 1; i <= tot; i++) {
    f[i] = ksm(k, (a[i] + 1) / 2);
    for (int j = 1; j < i; j++)
      if (a[i] % a[j] == 0)
        (f[i] += MOD - f[j]) %= MOD;
    if (a[i] & 1)
      (ans += (LL)f[i] * a[i] % MOD) %= MOD;
    else
      (ans += (LL)f[i] * a[i] % MOD * ny2 % MOD) %= MOD;
  }
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

typedef long long LL;

const int N = 3005;
const int MOD = 1000000007;

int n, k, tot, a[N], f[N];

int ksm(int x, int y) {
  int ans = 1;
  while (y) {
    if (y & 1)
      ans = (LL)ans * x % MOD;
    x = (LL)x * x % MOD;
    y >>= 1;
  }
  return ans;
}

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i * i <= n; i++)
    if (n % i == 0) {
      a[++tot] = i;
      if (i * i < n)
        a[++tot] = n / i;
    }
  sort(a + 1, a + tot + 1);
  int ny2 = ksm(2, MOD - 2), ans = 0;
  for (int i = 1; i <= tot; i++) {
    f[i] = ksm(k, (a[i] + 1) / 2);
    for (int j = 1; j < i; j++)
      if (a[i] % a[j] == 0)
        (f[i] += MOD - f[j]) %= MOD;
    if (a[i] & 1)
      (ans += (LL)f[i] * a[i] % MOD) %= MOD;
    else
      (ans += (LL)f[i] * a[i] % MOD * ny2 % MOD) %= MOD;
  }
  printf("%d", ans);
  return 0;
}
Problem p03867 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long lint;
#define cout cerr
#define ni (next_num<int>())
template <class T> inline T next_num() {
  T i = 0;
  char c;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  bool flag = c == '-';
  flag ? (c = getchar()) : 0;
  while (i = i * 10 - '0' + c, isdigit(c = getchar()))
    ;
  return flag ? -i : i;
}
const int dN = 1010, O = 1000000007;
inline int fpow(int x, int n) {
  lint a = 1;
  for (; n; n >>= 1, x = (lint)x * x % O) {
    if (n & 1) {
      a = (lint)a * x % O;
    }
  }
  return a;
}
inline int inv(int x) { return fpow(x, O - 2); }
int fact[dN], fs = 0;
lint f[dN];
inline void addfact(int x, int e) {
  for (int i = 0, j = 0; i < e; i++) {
    for (int k = fs; j < k; j++) {
      fact[fs++] = fact[j] * x;
    }
  }
}
int main() {
  int n = ni, k = ni;
  fact[fs++] = 1;
  for (int x = 2, e; x * x <= n; x++) {
    for (e = 0; n % x == 0; n /= x, e++)
      ;
    addfact(x, e);
  }
  if (n != 1) {
    addfact(n, 1);
  }
  lint ans = 0;
  for (int i = 0; i < fs; i++) {
    int d = fact[i];
    f[i] = fpow(k, (d + 1) >> 1);
    for (int j = 0; j < i; j++) {
      if (d % fact[j] == 0) {
        f[i] -= f[j];
      }
    }
    f[i] = (f[i] % O + O) % O;
    static int inv2 = inv(2);
    ans += d & 1 ? f[i] * d % O : (f[i] * d % O * inv2 % O);
  }
  printf("%lld\n", ans % O);
  return 0;
}

Fixed submission:
#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long lint;
#define cout cerr
#define ni (next_num<int>())
template <class T> inline T next_num() {
  T i = 0;
  char c;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  bool flag = c == '-';
  flag ? (c = getchar()) : 0;
  while (i = i * 10 - '0' + c, isdigit(c = getchar()))
    ;
  return flag ? -i : i;
}
const int dN = 5010, O = 1000000007;
inline int fpow(int x, int n) {
  lint a = 1;
  for (; n; n >>= 1, x = (lint)x * x % O) {
    if (n & 1) {
      a = (lint)a * x % O;
    }
  }
  return a;
}
inline int inv(int x) { return fpow(x, O - 2); }
int fact[dN], fs = 0;
lint f[dN];
inline void addfact(int x, int e) {
  for (int i = 0, j = 0; i < e; i++) {
    for (int k = fs; j < k; j++) {
      fact[fs++] = fact[j] * x;
    }
  }
}
int main() {
  int n = ni, k = ni;
  fact[fs++] = 1;
  for (int x = 2, e; x * x <= n; x++) {
    for (e = 0; n % x == 0; n /= x, e++)
      ;
    addfact(x, e);
  }
  if (n != 1) {
    addfact(n, 1);
  }
  lint ans = 0;
  for (int i = 0; i < fs; i++) {
    int d = fact[i];
    f[i] = fpow(k, (d + 1) >> 1);
    for (int j = 0; j < i; j++) {
      if (d % fact[j] == 0) {
        f[i] -= f[j];
      }
    }
    f[i] = (f[i] % O + O) % O;
    static int inv2 = inv(2);
    ans += d & 1 ? f[i] * d % O : (f[i] * d % O * inv2 % O);
  }
  printf("%lld\n", ans % O);
  return 0;
}
Problem p03867 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
const long long MOD = 1000000007;
int N, K, d[240], w[240], L;
long long O;
long long POWER(long long a, long long b) {
  long long r = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      r = r * a % MOD;
    a = a * a % MOD;
  }
  return r;
}
int main() {
  scanf("%d%d", &N, &K);
  for (int i = 1; i * i <= N; i++)
    if (N % i == 0) {
      d[L++] = i;
      if (i * i < N)
        d[L++] = N / i;
    }
  std::sort(d, d + L);
  for (int i = 0; i < L; i++)
    w[i] = d[i];
  for (int i = L - 1; i >= 0; i--)
    for (int j = i + 1; j < L; j++)
      if (d[j] % d[i] == 0)
        w[i] -= w[j];
  for (int i = 0; i < L; i++)
    O = (O + (w[i] + MOD) * POWER(K, N / d[i] & 1 ? d[i] + 1 >> 1 : d[i])) %
        MOD;
  printf("%lld\n", O);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
const long long MOD = 1000000007;
int N, K, d[10000], w[10000], L;
long long O;
long long POWER(long long a, long long b) {
  long long r = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      r = r * a % MOD;
    a = a * a % MOD;
  }
  return r;
}
int main() {
  scanf("%d%d", &N, &K);
  for (int i = 1; i * i <= N; i++)
    if (N % i == 0) {
      d[L++] = i;
      if (i * i < N)
        d[L++] = N / i;
    }
  std::sort(d, d + L);
  for (int i = 0; i < L; i++)
    w[i] = d[i];
  for (int i = L - 1; i >= 0; i--)
    for (int j = i + 1; j < L; j++)
      if (d[j] % d[i] == 0)
        w[i] -= w[j];
  for (int i = 0; i < L; i++)
    O = (O + (w[i] + MOD) * POWER(K, N / d[i] & 1 ? d[i] + 1 >> 1 : d[i])) %
        MOD;
  printf("%lld\n", O);
  return 0;
}
Problem p03868 (C++)

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

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

template <typename T> inline void chkmax(T &x, T y) {
  if (x < y)
    x = y;
}

template <typename T> inline void chkmin(T &x, T y) {
  if (x > y)
    x = y;
}

const int maxn = 1e5 + 10, P = 1e9 + 7;
int n;

struct point {
  int op, x;
} a[maxn];

int main() {
  scanf("%d", &n);
  rep(i, 1, n * 2) {
    scanf("%d", &a[i].x);
    a[i].op = i <= n;
  }
  sort(a + 1, a + n * 2 + 1, [](point a, point b) { return a.x < b.x; });
  int res = 1;
  int now = 0, sz = 0;
  rep(i, 1, n * 2) {
    if (now == a[i].op) {
      sz++;
    } else {
      if (sz)
        res = 1ll * res * sz % P;
      if (--sz < 0)
        now ^= 1, sz = 1;
    }
  }
  printf("%d", res);
  return 0;
}

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

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

template <typename T> inline void chkmax(T &x, T y) {
  if (x < y)
    x = y;
}

template <typename T> inline void chkmin(T &x, T y) {
  if (x > y)
    x = y;
}

const int maxn = 1e5 + 10, P = 1e9 + 7;
int n;

struct point {
  int op, x;
} a[maxn * 2];

int main() {
  scanf("%d", &n);
  rep(i, 1, n * 2) {
    scanf("%d", &a[i].x);
    a[i].op = i <= n;
  }
  sort(a + 1, a + n * 2 + 1, [](point a, point b) { return a.x < b.x; });
  int res = 1;
  int now = 0, sz = 0;
  rep(i, 1, n * 2) {
    if (now == a[i].op) {
      sz++;
    } else {
      if (sz)
        res = 1ll * res * sz % P;
      if (--sz < 0)
        now ^= 1, sz = 1;
    }
  }
  printf("%d", res);
  return 0;
}
Problem p03868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
// #define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template <class T> void read(T &res) {
  res = 0;
  T f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}
const int MOD = 1000000007;
int N;
int a[MAXN], b[MAXN], tot;
int sta[MAXN], top;
pii p[MAXN * 2];
int inc(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; }
int mul(int a, int b) { return 1LL * a * b % MOD; }
void Solve() {
  read(N);
  for (int i = 1; i <= N; ++i) {
    read(a[i]);
    p[++tot] = mp(a[i], 0);
  }
  for (int i = 1; i <= N; ++i) {
    read(b[i]);
    p[++tot] = mp(b[i], 1);
  }
  sort(p + 1, p + tot + 1);
  int ans = 1;
  for (int i = 1; i <= tot; ++i) {
    if (top && sta[top] != p[i].se) {
      ans = mul(ans, top);
      --top;
    } else
      sta[++top] = p[i].se;
  }
  out(ans);
  enter;
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#else
  freopen("prophesy.in", "r", stdin);
  freopen("prophesy.out", "w", stdout);
#endif
  Solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
// #define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template <class T> void read(T &res) {
  res = 0;
  T f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}
const int MOD = 1000000007;
int N;
int a[MAXN], b[MAXN], tot;
int sta[MAXN], top;
pii p[MAXN * 2];
int inc(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; }
int mul(int a, int b) { return 1LL * a * b % MOD; }
void Solve() {
  read(N);
  for (int i = 1; i <= N; ++i) {
    read(a[i]);
    p[++tot] = mp(a[i], 0);
  }
  for (int i = 1; i <= N; ++i) {
    read(b[i]);
    p[++tot] = mp(b[i], 1);
  }
  sort(p + 1, p + tot + 1);
  int ans = 1;
  for (int i = 1; i <= tot; ++i) {
    if (top && sta[top] != p[i].se) {
      ans = mul(ans, top);
      --top;
    } else
      sta[++top] = p[i].se;
  }
  out(ans);
  enter;
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  Solve();
  return 0;
}
Problem p03868 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define show(x) cout << #x << " = " << (x) << endl;
using namespace std;
using ll = long long;
using pii = pair<int, int>;
ll mod = 1e9 + 7;
pii a[20005];
int main() {
  int n;
  scanf("%d", &n);
  rep(i, 2 * n) {
    scanf("%d", &a[i].first);
    a[i].second = i;
  }
  sort(a, a + 2 * n);
  ll ans = 1;
  int hoge = 0;
  rep(i, 2 * n) {
    if (a[i].second < n) {
      if (hoge < 0)
        ans = ans * (-hoge) % mod;
      hoge++;
    } else {
      if (hoge > 0)
        ans = ans * hoge % mod;
      hoge--;
    }
  }
  printf("%lld\n", ans);
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define show(x) cout << #x << " = " << (x) << endl;
using namespace std;
using ll = long long;
using pii = pair<int, int>;
ll mod = 1e9 + 7;
pii a[200005];
int main() {
  int n;
  scanf("%d", &n);
  rep(i, 2 * n) {
    scanf("%d", &a[i].first);
    a[i].second = i;
  }
  sort(a, a + 2 * n);
  ll ans = 1;
  int hoge = 0;
  rep(i, 2 * n) {
    if (a[i].second < n) {
      if (hoge < 0)
        ans = ans * (-hoge) % mod;
      hoge++;
    } else {
      if (hoge > 0)
        ans = ans * hoge % mod;
      hoge--;
    }
  }
  printf("%lld\n", ans);
}