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

Buggy submission (status: Runtime Error):
import numpy as np
import sys
from scipy.sparse.csgraph import floyd_warshall, csgraph_from_dense

N = int(input())
S = np.frombuffer(sys.stdin.buffer.read(), dtype="S1").reshape(N, -1)[:, :N].astype(int)
G = csgraph_from_dense(S)
F = floyd_warshall(G).astype(int)
F2 = F % 2
I0 = np.where(F2[0] == 0)
I1 = np.where(F2[0] == 1)
if (S[I0][:, I0] == 0).all() and (S[I1][:, I1] == 0).all():
    print(F.max() + 1)
    exit()
print(-1)


Fixed submission:
import numpy as np
import sys
from scipy.sparse.csgraph import floyd_warshall, csgraph_from_dense

N = int(sys.stdin.buffer.readline())
S = np.frombuffer(sys.stdin.buffer.read(), dtype="S1").reshape(N, -1)[:, :N].astype(int)
G = csgraph_from_dense(S)
F = floyd_warshall(G).astype(int)
F2 = F % 2
I0 = np.where(F2[0] == 0)
I1 = np.where(F2[0] == 1)
if (S[I0][:, I0] == 0).all() and (S[I1][:, I1] == 0).all():
    print(F.max() + 1)
    exit()
print(-1)
Problem p02892 (Python)

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

n = int(input())
s = [input() for i in range(n)]
q = deque([0])
d = [float("inf")] * n
d[0] = 0
while q:
    p = q.popleft()
    for i, c in enumerate(s[p]):
        if c == "1" and d[i] == float("inf"):
            d[i] = d[p] + 1
            q.append(i)
m = 0
for i in range(n):
    if d[m] < d[i]:
        m = i
q = deque([m])
d = [float("inf")] * n
d[m] = 0
while q:
    p = q.popleft()
    for i, c in enumerate(s[p]):
        if c == "1":
            if d[i] == float("inf"):
                d[i] = d[p] + 1
                q.append(i)
            elif d[i] != d[p] + 1 and d[i] != d[p] - 1:
                print(-1)
                exit(1)
m = 0
for i in range(n):
    m = max(m, d[i])
print(m + 1)


Fixed submission:
from collections import deque

n = int(input())
s = [input() for i in range(n)]
ans = -2
for k in range(n):
    q = deque([k])
    d = [float("inf")] * n
    d[k] = 0
    while q:
        flg = 0
        p = q.popleft()
        for i, c in enumerate(s[p]):
            if c == "1":
                if d[i] == float("inf"):
                    d[i] = d[p] + 1
                    q.append(i)
                elif d[i] != d[p] + 1 and d[i] != d[p] - 1:
                    flg = 1
                    break
        if flg:
            break
    if flg:
        continue
    for i in range(n):
        ans = max(ans, d[i])
print(ans + 1)
Problem p02892 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
char s[N][N];
int stg[N];
int n;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
  }
  int ret = -1;
  for (int S = 1; S <= n; S++) {
    queue<int> q;
    q.push(S);
    for (int i = 1; i <= n; i++)
      stg[i] = 0;
    stg[S] = 1;
    int ans = 1;
    while (q.size()) {
      int x = q.front();
      q.pop();
      ans = max(ans, stg[x]);
      for (int j = 1; j <= n; j++)
        if (s[x][j] == '1') {
          if (!stg[j])
            stg[j] = stg[x] + 1, q.push(j);
        }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++)
        if (s[i][j] == '1') {
          if (abs(stg[i] - stg[j]) != 1)
            ans = -1;
        }
    }
    ret = max(ret, ans);
  }
  cout << ret << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 210;
char s[N][N];
int stg[N];
int n;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
  }
  int ret = -1;
  for (int S = 1; S <= n; S++) {
    queue<int> q;
    q.push(S);
    for (int i = 1; i <= n; i++)
      stg[i] = 0;
    stg[S] = 1;
    int ans = 1;
    while (q.size()) {
      int x = q.front();
      q.pop();
      ans = max(ans, stg[x]);
      for (int j = 1; j <= n; j++)
        if (s[x][j] == '1') {
          if (!stg[j])
            stg[j] = stg[x] + 1, q.push(j);
        }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++)
        if (s[i][j] == '1') {
          if (abs(stg[i] - stg[j]) != 1)
            ans = -1;
        }
    }
    ret = max(ret, ans);
  }
  cout << ret << endl;
}
Problem p02892 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int n, i, ii, j, x, nr, p, u, ok;
int v[105], c[105];
char a[105][105];
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i] + 1;
  }
  nr = -1;
  for (ii = 1; ii <= n; ii++) {
    for (i = 1; i <= n; i++) {
      v[i] = 0;
    }
    v[ii] = p = u = 1;
    c[1] = ii;
    ok = 1;
    while (p <= u) {
      x = c[p];
      for (i = 1; i <= n; i++) {
        if (a[x][i] == '1') {
          if (v[i] == 0) {
            v[i] = 1 + v[x];
            c[++u] = i;
          } else {
            if (v[i] != v[x] - 1 && v[i] != v[x] + 1) {
              ok = 0;
            }
          }
        }
      }
      p++;
    }
    if (ok == 1) {
      for (i = 1; i <= n; i++) {
        nr = max(nr, v[i]);
      }
    }
  }
  cout << nr;
}


Fixed submission:
#include <iostream>
using namespace std;
int n, i, ii, j, x, nr, p, u, ok;
int v[205], c[205];
char a[205][205];
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i] + 1;
  }
  nr = -1;
  for (ii = 1; ii <= n; ii++) {
    for (i = 1; i <= n; i++) {
      v[i] = 0;
    }
    v[ii] = p = u = 1;
    c[1] = ii;
    ok = 1;
    while (p <= u) {
      x = c[p];
      for (i = 1; i <= n; i++) {
        if (a[x][i] == '1') {
          if (v[i] == 0) {
            v[i] = 1 + v[x];
            c[++u] = i;
          } else {
            if (v[i] != v[x] - 1 && v[i] != v[x] + 1) {
              ok = 0;
            }
          }
        }
      }
      p++;
    }
    if (ok == 1) {
      for (i = 1; i <= n; i++) {
        nr = max(nr, v[i]);
      }
    }
  }
  cout << nr;
}
Problem p02892 (C++)

Buggy submission (status: Runtime Error):
/**/
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

typedef long long LL;
using namespace std;

int n, col[205], tot, head[205];
int dp[205][205];
char s[205][205];

struct node {
  int v, next;
} a[55 * 55 * 2];

bool dfs(int x, int p) {
  col[x] = p;
  for (int i = head[x]; i != -1; i = a[i].next) {
    int v = a[i].v;
    if (col[v] == p)
      return true;
    if (!col[v] && dfs(v, p ^ 1))
      return true;
  }
  return false;
}

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);

  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    head[i] = -1;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      dp[i][j] = 0x3f3f3f3f;
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
    for (int j = 1; j <= n; j++) {
      if (s[i][j] == '1')
        a[tot] = node{j, head[i]}, head[i] = tot++, dp[i][j] = 1;
    }
  }
  for (int i = 1; i <= n; i++)
    dp[i][i] = 0;
  if (dfs(1, 2)) {
    printf("-1\n");
    return 0;
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      ans = max(ans, dp[i][j]);
  printf("%d\n", ans + 1);

  return 0;
}
/**/

Fixed submission:
/**/
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

typedef long long LL;
using namespace std;

int n, col[205], tot, head[205];
int dp[205][205];
char s[205][205];

struct node {
  int v, next;
} a[205 * 205];

bool dfs(int x, int p) {
  col[x] = p;
  for (int i = head[x]; i != -1; i = a[i].next) {
    int v = a[i].v;
    if (col[v] == p)
      return true;
    if (!col[v] && dfs(v, p ^ 1))
      return true;
  }
  return false;
}

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);

  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    head[i] = -1;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      dp[i][j] = 0x3f3f3f3f;
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
    for (int j = 1; j <= n; j++) {
      if (s[i][j] == '1')
        a[tot] = node{j, head[i]}, head[i] = tot++, dp[i][j] = 1;
    }
  }
  for (int i = 1; i <= n; i++)
    dp[i][i] = 0;
  if (dfs(1, 2)) {
    printf("-1\n");
    return 0;
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      ans = max(ans, dp[i][j]);
  printf("%d\n", ans + 1);

  return 0;
}
/**/
Problem p02892 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define mk make_pair
#define eb emplace_back
#define eps 1e-8
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
// #define int long long
using namespace std;

typedef long double ld;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vii;
typedef vector<long double> vd;
const int inf = 1e9;
const int M = 1e9 + 7;
//__int128

const int maxn = 202;
string s[maxn];
int vis[maxn];
int n;
int bfs(int u) {
  queue<int> q;
  q.push(u);
  int res = 0;
  vis[u] = 1;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    res = max(res, vis[x]);
    for (int i = 0; i < n; ++i) {
      if (s[x][i] == '1') {
        if (!vis[i]) {
          q.push(i);
          vis[i] = vis[x] + 1;
        } else if (vis[i] == vis[x]) {
          return -1;
        }
      }
    }
  }
  return res;
}

signed main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  std::vector<pii> d(n);
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
  }
  int res = -1;
  for (int i = 0; i < n; ++i) {
    memset(vis, 0, sizeof(vis));
    res = max(res, bfs(i));
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define mk make_pair
#define eb emplace_back
#define eps 1e-8
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
// #define int long long
using namespace std;

typedef long double ld;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vii;
typedef vector<long double> vd;
const int inf = 1e9;
const int M = 1e9 + 7;
//__int128

const int maxn = 202;
string s[maxn];
int vis[maxn];
int n;
int bfs(int u) {
  queue<int> q;
  q.push(u);
  int res = 0;
  vis[u] = 1;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    res = max(res, vis[x]);
    for (int i = 0; i < n; ++i) {
      if (s[x][i] == '1') {
        if (!vis[i]) {
          q.push(i);
          vis[i] = vis[x] + 1;
        } else if (vis[i] == vis[x]) {
          return -1;
        }
      }
    }
  }
  return res;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  std::vector<pii> d(n);
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
  }
  int res = -1;
  for (int i = 0; i < n; ++i) {
    memset(vis, 0, sizeof(vis));
    res = max(res, bfs(i));
  }
  cout << res << endl;
  return 0;
}
Problem p02892 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int dis[105][105], e[105][105];
signed main() {
  int n;
  char ch;
  cin >> n;
  memset(dis, 0x3f, sizeof(dis));
  for (int i = 1; i <= n; ++i)
    dis[i][i] = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) {
      cin >> ch;
      if (ch == '1')
        e[i][j] = dis[i][j] = 1;
    }
  for (int k = 1; k <= n; ++k)
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j)
        dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
  int ans = -1;
  for (int u = 1; u <= n; ++u) {
    int fl = 0;
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j)
        if (e[i][j] && abs(dis[u][i] - dis[u][j]) != 1)
          fl = 1;
    if (fl)
      break;
    for (int i = 1; i <= n; ++i)
      ans = max(ans, dis[u][i] + 1);
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dis[205][205], e[205][205];
signed main() {
  int n;
  char ch;
  cin >> n;
  memset(dis, 0x3f, sizeof(dis));
  for (int i = 1; i <= n; ++i)
    dis[i][i] = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) {
      cin >> ch;
      if (ch == '1')
        e[i][j] = dis[i][j] = 1;
    }
  for (int k = 1; k <= n; ++k)
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j)
        dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
  int ans = -1;
  for (int u = 1; u <= n; ++u) {
    int fl = 0;
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j)
        if (e[i][j] && abs(dis[u][i] - dis[u][j]) != 1)
          fl = 1;
    if (fl)
      break;
    for (int i = 1; i <= n; ++i)
      ans = max(ans, dis[u][i] + 1);
  }
  cout << ans;
  return 0;
}
Problem p02892 (C++)

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

const int N = 105;
typedef long long LL;

char ch;
int a[N][N], n, clr[N], ok, dis[N][N], ans;

void Dfs(int u, int col) {
  clr[u] = col;
  for (int i = 1; i <= n; i++)
    if (a[u][i] == 1) {
      if (clr[i]) {
        if (clr[i] == col)
          ok = 1;
        continue;
      }
      Dfs(i, 3 - col);
    }
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      cin >> ch;
      if (ch == '1')
        a[i][j] = 1;
      else {
        if (i == j)
          a[i][j] = 0;
        else
          a[i][j] = 1e9;
      }
    }
  Dfs(1, 1);
  if (ok) {
    cout << -1 << endl;
    return 0;
  }
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (a[i][k] + a[k][j] < a[i][j])
          a[i][j] = a[i][k] + a[k][j];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      ans = max(ans, a[i][j]);
  cout << ans + 1 << endl;
  return 0;
}

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

const int N = 205;
typedef long long LL;

char ch;
int a[N][N], n, clr[N], ok, dis[N][N], ans;

void Dfs(int u, int col) {
  clr[u] = col;
  for (int i = 1; i <= n; i++)
    if (a[u][i] == 1) {
      if (clr[i]) {
        if (clr[i] == col)
          ok = 1;
        continue;
      }
      Dfs(i, 3 - col);
    }
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      cin >> ch;
      if (ch == '1')
        a[i][j] = 1;
      else {
        if (i == j)
          a[i][j] = 0;
        else
          a[i][j] = 1e9;
      }
    }
  Dfs(1, 1);
  if (ok) {
    cout << -1 << endl;
    return 0;
  }
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (a[i][k] + a[k][j] < a[i][j])
          a[i][j] = a[i][k] + a[k][j];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      ans = max(ans, a[i][j]);
  cout << ans + 1 << endl;
  return 0;
}
Problem p02892 (C++)

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

using namespace std;

const int MAX_N = 1e3 + 10;

int N, fl, ans;

int col[MAX_N];

int cap;
int head[MAX_N], to[MAX_N], nxt[MAX_N];

queue<int> que;

inline int Abs(int x) { return x < 0 ? -x : x; }

void BFS(int x) {
  memset(col, 0, sizeof col);
  col[x] = 1;

  que.push(x);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    ans = max(ans, col[u]);
    for (int i = head[u]; i; i = nxt[i]) {
      if (!col[to[i]]) {
        col[to[i]] = col[u] + 1;
        que.push(to[i]);
      } else if (Abs(col[to[i]] - col[u]) != 1)
        fl = 1;
    }
    if (fl)
      break;
  }
}

inline void add(int x, int y) {
  to[++cap] = y, nxt[cap] = head[x], head[x] = cap;
}

int main() {

  scanf("%d", &N);

  int x;
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j <= N; ++j) {
      scanf("%1d", &x);
      if (x)
        add(i, j), add(j, i);
    }
  }

  for (int i = 1; i <= N; ++i) {
    BFS(i);
    if (fl)
      return puts("-1"), 0;
  }

  printf("%d\n", ans);

  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

const int MAX_N = 1e5 + 10;

int N, fl, ans;

int col[MAX_N];

int cap;
int head[MAX_N], to[MAX_N], nxt[MAX_N];

queue<int> que;

inline int Abs(int x) { return x < 0 ? -x : x; }

void BFS(int x) {
  memset(col, 0, sizeof col);
  col[x] = 1;

  que.push(x);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    ans = max(ans, col[u]);
    for (int i = head[u]; i; i = nxt[i]) {
      if (!col[to[i]]) {
        col[to[i]] = col[u] + 1;
        que.push(to[i]);
      } else if (Abs(col[to[i]] - col[u]) != 1)
        fl = 1;
    }
    if (fl)
      break;
  }
}

inline void add(int x, int y) {
  to[++cap] = y, nxt[cap] = head[x], head[x] = cap;
}

int main() {

  scanf("%d", &N);

  int x;
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j <= N; ++j) {
      scanf("%1d", &x);
      if (x)
        add(i, j), add(j, i);
    }
  }

  for (int i = 1; i <= N; ++i) {
    BFS(i);
    if (fl)
      return puts("-1"), 0;
  }

  printf("%d\n", ans);

  return 0;
}
Problem p02892 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long int ll;
typedef pair<int, int> Pii;

const ll mod = 1000000007;

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

  int N;
  cin >> N;
  vector<string> S(N);
  for (auto &x : S)
    cin >> x;

  int best = 0;
  for (int i = 0; i < N; i++) {
    queue<Pii> que;
    que.emplace(i, 1);
    vector<int> depth(N, 0);
    vector<bool> visited(N, false);
    while (!que.empty()) {
      auto now = que.front();
      que.pop();
      visited[now.first] = true;
      depth[now.first] = now.second;
      for (int j = 0; j < N; j++) {
        if (S[now.first][j] == '0')
          continue;
        else if (visited[j]) {
          if (depth[now.first] - 1 != depth[j]) {
            cout << -1 << endl; // impossible
            return 0;
          }
        } else {
          que.emplace(j, now.second + 1);
        }
      }
    }
    for (int j = 0; j < N; j++) {
      if (depth[j] > best) {
        best = depth[j];
      }
    }
  }

  cout << best << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long int ll;
typedef pair<int, int> Pii;

const ll mod = 1000000007;

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

  int N;
  cin >> N;
  vector<string> S(N);
  for (auto &x : S)
    cin >> x;

  int best = 0;
  for (int i = 0; i < N; i++) {
    queue<Pii> que;
    que.emplace(i, 1);
    vector<int> depth(N, 0);
    vector<bool> visited(N, false);
    while (!que.empty()) {
      auto now = que.front();
      que.pop();
      if (visited[now.first])
        continue;
      visited[now.first] = true;
      depth[now.first] = now.second;
      for (int j = 0; j < N; j++) {
        if (S[now.first][j] == '0')
          continue;
        else if (visited[j]) {
          if (depth[now.first] - 1 != depth[j]) {
            cout << -1 << endl; // impossible
            return 0;
          }
        } else {
          que.emplace(j, now.second + 1);
        }
      }
    }
    for (int j = 0; j < N; j++) {
      if (depth[j] > best) {
        best = depth[j];
      }
    }
  }

  cout << best << endl;

  return 0;
}
Problem p02892 (C++)

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

using namespace std;

const int N = 202;

vector<int> g[N];
int col[N];

bool dfs(int v, int c, vector<int> &comp) {
  comp.push_back(v);
  col[v] = c;
  for (int u : g[v]) {
    if (col[u] == -1) {
      if (!dfs(u, c ^ 1, comp))
        return false;
    } else {
      if (col[u] == col[v])
        return false;
    }
  }
  return true;
}

bool used[N];
int d[N];

int bfs(int s, vector<int> &vert) {
  for (int v : vert) {
    used[v] = false;
    d[v] = N;
  }
  d[s] = 0;
  used[s] = true;
  queue<int> Q;
  Q.push(s);
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    for (int u : g[v]) {
      if (!used[u]) {
        d[u] = d[v] + 1;
        used[u] = true;
        Q.push(u);
      }
    }
  }

  int ans = 0;
  for (int v : vert)
    ans = max(ans, d[v] + 1);
  return ans;
}

char s[N];

int main() {
  freopen("input.txt", "r", stdin);

  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%s", s);
    for (int j = 0; j < n; ++j) {
      if (s[j] == '1')
        g[i].push_back(j);
    }
  }

  for (int v = 0; v < n; ++v)
    col[v] = -1;

  int ans = 0;
  vector<int> comp;
  for (int v = 0; v < n; ++v) {
    if (col[v] == -1) {
      comp.clear();
      if (!dfs(v, 0, comp)) {
        printf("-1");
        return 0;
      }

      int cur = 0;
      for (int u : comp)
        cur = max(cur, bfs(u, comp));

      ans += cur;
    }
  }

  printf("%d", ans);
}


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

using namespace std;

const int N = 202;

vector<int> g[N];
int col[N];

bool dfs(int v, int c, vector<int> &comp) {
  comp.push_back(v);
  col[v] = c;
  for (int u : g[v]) {
    if (col[u] == -1) {
      if (!dfs(u, c ^ 1, comp))
        return false;
    } else {
      if (col[u] == col[v])
        return false;
    }
  }
  return true;
}

bool used[N];
int d[N];

int bfs(int s, vector<int> &vert) {
  for (int v : vert) {
    used[v] = false;
    d[v] = N;
  }
  d[s] = 0;
  used[s] = true;
  queue<int> Q;
  Q.push(s);
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    for (int u : g[v]) {
      if (!used[u]) {
        d[u] = d[v] + 1;
        used[u] = true;
        Q.push(u);
      }
    }
  }

  int ans = 0;
  for (int v : vert)
    ans = max(ans, d[v] + 1);
  return ans;
}

char s[N];

int main() {
  // freopen("input.txt", "r", stdin);

  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%s", s);
    for (int j = 0; j < n; ++j) {
      if (s[j] == '1')
        g[i].push_back(j);
    }
  }

  for (int v = 0; v < n; ++v)
    col[v] = -1;

  int ans = 0;
  vector<int> comp;
  for (int v = 0; v < n; ++v) {
    if (col[v] == -1) {
      comp.clear();
      if (!dfs(v, 0, comp)) {
        printf("-1");
        return 0;
      }

      int cur = 0;
      for (int u : comp)
        cur = max(cur, bfs(u, comp));

      ans += cur;
    }
  }

  printf("%d", ans);
}
Problem p02892 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#define N 9
using namespace std;

int n, ans, d[N][N];
int main() {
  int i, j, k, t;
  cin >> n;
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++) {
      scanf("%1d", &t);
      d[i][j] = (t == 1 || i == j ? t : 1e9);
    }
  for (k = 0; k < n; k++)
    for (i = 0; i < n; i++)
      for (j = 0; j < n; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++) {
      ans = max(ans, d[i][j]);
      for (k = 0; k < n; k++) {
        if ((d[i][k] + d[k][j] + d[i][j]) % 2)
          return puts("-1") * 0;
      }
    }
  cout << ans + 1;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#define N 205
using namespace std;

int n, ans, d[N][N];
int main() {
  int i, j, k, t;
  cin >> n;
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++) {
      scanf("%1d", &t);
      d[i][j] = (t == 1 || i == j ? t : 1e9);
    }
  for (k = 0; k < n; k++)
    for (i = 0; i < n; i++)
      for (j = 0; j < n; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++) {
      ans = max(ans, d[i][j]);
      for (k = 0; k < n; k++) {
        if ((d[i][k] + d[k][j] + d[i][j]) % 2)
          return puts("-1") * 0;
      }
    }
  cout << ans + 1;
  return 0;
}
Problem p02893 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
a = input()
inva = "".join(map(lambda x: str(int(x) ^ 1), a))

mod = 998244353
ans = 0
p = 1
num = [0] * -~n
for i in range(n):
    if i:
        num[i] += (num[i - 1] * 2) % mod
    if a[i] == "1":
        num[i] += 1


def get_div(x):
    y = []
    for i in range(1, n):
        if x % i == 0 and (x // i) % 2:
            y.append(i)
    return y


div = get_div(n)
d = [0] * -~n
tot = 0
for i in div:
    now = n // i
    tt = num[i - 1]
    for j in get_div(i):
        tt -= d[j]
    k = (a[:i] + inva[:i]) * (n // (2 * i)) + a[:i]
    d[i] = tt + (int(a, 2) >= int(k, 2))
    tot += d[i]
d[n] = int(a, 2) + 1 - tot

for i in range(n + 1):
    ans = (ans + d[i] * i * 2) % mod

print(ans % mod)


Fixed submission:
n = int(input())
a = input()
inva = "".join(map(lambda x: str(int(x) ^ 1), a))

mod = 998244353
ans = 0
p = 1
num = [0] * -~n
for i in range(n):
    if i:
        num[i] += (num[i - 1] * 2) % mod
    if a[i] == "1":
        num[i] += 1


def get_div(x):
    y = []
    for i in range(1, n):
        if x % i == 0 and (x // i) % 2:
            y.append(i)
    return y


div = get_div(n)
d = [0] * -~n
tot = 0
for i in div:
    now = n // i
    tt = num[i - 1]
    for j in get_div(i):
        tt -= d[j]
    k = (a[:i] + inva[:i]) * (n // (2 * i)) + a[:i]
    d[i] = tt + (a >= k)
    tot += d[i]
d[n] = int(a, 2) + 1 - tot

for i in range(n + 1):
    ans = (ans + d[i] * i * 2) % mod

print(ans % mod)
Problem p02893 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int mod = 998244353;

int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}

int n, f[N], ans = 0, bin[N];
char s[N];

int main() {
  cin >> n;
  scanf("%s", s + 1);
  reverse(s + 1, s + n + 1);
  bin[0] = 1;
  for (int i = 1; i <= n; i++)
    bin[i] = (bin[i - 1] << 1) % mod;
  for (int i = 1; i <= n; i++)
    if (n % i == 0 && ((n / i) & 1)) {
      int sum = 0;
      for (int j = n; j >= n - i + 1; j--)
        if (s[j] == '1')
          sum = (sum + bin[j - (n - i) - 1]) % mod;
      int fl = 0, fl2 = 0;
      for (int j = n - i; j && !fl2; j -= i) {
        fl ^= 1;
        for (int k = j, l = n; k >= j - i + 1; k--, l--) {
          int b = (s[l] - '0') ^ fl;
          if (b < s[k] - '0') {
            sum = (sum + 1) % mod;
            fl2 = 1;
            break;
          } else if (b > s[k] - '0') {
            fl2 = 1;
            break;
          }
        }
      }
      if (!fl2)
        sum = (sum + 1) % mod;
      f[i] = sum;
    }
  for (int i = 1; i <= n; i++)
    if (n % i == 0 && ((n / i) & 1))
      for (int j = i + i; j <= n; j += i)
        if (n % j == 0 && ((n / i) & 1))
          f[j] = (f[j] - f[i] + mod) % mod;
  for (int i = 1; i <= n; i++)
    if (n % i == 0 && ((n / i) & 1))
      ans = (ans + 2ll * f[i] * i) % mod;
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int mod = 998244353;

int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}

int n, f[N], ans = 0, bin[N];
char s[N];

int main() {
  cin >> n;
  scanf("%s", s + 1);
  reverse(s + 1, s + n + 1);
  bin[0] = 1;
  for (int i = 1; i <= n; i++)
    bin[i] = (bin[i - 1] << 1) % mod;
  for (int i = 1; i <= n; i++)
    if (n % i == 0 && ((n / i) & 1)) {
      int sum = 0;
      for (int j = n; j >= n - i + 1; j--)
        if (s[j] == '1')
          sum = (sum + bin[j - (n - i) - 1]) % mod;
      int fl = 0, fl2 = 0;
      for (int j = n - i; j && !fl2; j -= i) {
        fl ^= 1;
        for (int k = j, l = n; k >= j - i + 1; k--, l--) {
          int b = (s[l] - '0') ^ fl;
          if (b < s[k] - '0') {
            sum = (sum + 1) % mod;
            fl2 = 1;
            break;
          } else if (b > s[k] - '0') {
            fl2 = 1;
            break;
          }
        }
      }
      if (!fl2)
        sum = (sum + 1) % mod;
      f[i] = sum;
    }
  for (int i = 1; i <= n; i++)
    if (n % i == 0 && ((n / i) & 1))
      for (int j = i + i; j <= n; j += i)
        if (n % j == 0 && ((n / i) & 1))
          f[j] = (f[j] - f[i] + mod) % mod;
  for (int i = 1; i <= n; i++)
    if (n % i == 0 && ((n / i) & 1))
      ans = (ans + 2ll * f[i] * i) % mod;
  cout << ans;
  return 0;
}
Problem p02893 (C++)

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

using namespace std;

typedef long long ll;

const int N = 2e5 + 100, MOD = 998244353;

int add(ll a, int b) { return (a + b + 2LL * MOD) % MOD; }

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

void sadd(int &a, int b) { a = add(a, b); }

void smul(int &a, int b) { a = mul(a, b); }

vector<int> divs[N];

int cnt[N];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  string x;
  cin >> n >> x;
  n *= 2;
  string t = x;
  for (auto &c : t)
    c = (c == '0' ? '1' : '0');
  x.insert(x.end(), t.begin(), t.end());
  for (int i = 1; i <= n; i++)
    for (int j = i; j <= n; j += i)
      divs[j].push_back(i);
  vector<int> ds;
  for (int i = 1; i <= n; i++)
    if (n % i == 0 && n % (2 * i) != 0)
      ds.push_back(i);

  int ans = 0;
  for (auto g : ds) {
    int cur = 0;
    string cx;
    for (int i = 0; i < g / 2; i++) {
      smul(cur, 2);
      if (x[i] == '1')
        sadd(cur, 1);
      cx.push_back(x[i]);
    }
    sadd(cnt[g], cur);
    for (int i = 0; i < g / 2; i++)
      cx.push_back((cx[i] == '0' ? '1' : '0'));
    for (int i = g; i < n; i++)
      cx.push_back(cx[i % g]);
    if (cx <= x)
      sadd(cnt[g], 1);
    for (auto d : divs[g])
      if (d != g)
        sadd(cnt[g], -cnt[d]);
    sadd(ans, mul(g, cnt[g]));
  }
  cout << ans << "\n";
}


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

using namespace std;

typedef long long ll;

const int N = 5e5 + 100, MOD = 998244353;

int add(ll a, int b) { return (a + b + 2LL * MOD) % MOD; }

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

void sadd(int &a, int b) { a = add(a, b); }

void smul(int &a, int b) { a = mul(a, b); }

vector<int> divs[N];

int cnt[N];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  string x;
  cin >> n >> x;
  n *= 2;
  string t = x;
  for (auto &c : t)
    c = (c == '0' ? '1' : '0');
  x.insert(x.end(), t.begin(), t.end());
  for (int i = 1; i <= n; i++)
    for (int j = i; j <= n; j += i)
      divs[j].push_back(i);
  vector<int> ds;
  for (int i = 1; i <= n; i++)
    if (n % i == 0 && n % (2 * i) != 0)
      ds.push_back(i);

  int ans = 0;
  for (auto g : ds) {
    int cur = 0;
    string cx;
    for (int i = 0; i < g / 2; i++) {
      smul(cur, 2);
      if (x[i] == '1')
        sadd(cur, 1);
      cx.push_back(x[i]);
    }
    sadd(cnt[g], cur);
    for (int i = 0; i < g / 2; i++)
      cx.push_back((cx[i] == '0' ? '1' : '0'));
    for (int i = g; i < n; i++)
      cx.push_back(cx[i % g]);
    if (cx <= x)
      sadd(cnt[g], 1);
    for (auto d : divs[g])
      if (d != g)
        sadd(cnt[g], -cnt[d]);
    sadd(ans, mul(g, cnt[g]));
  }
  cout << ans << "\n";
}
Problem p02894 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2005;
typedef long double db;
typedef long long ll;
const db pi = acos(-1.0);
int n, L, a[maxn];
ll w;
db x, y, X, Y;
int main() {
  cin >> n >> L;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a, a + n);
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++) {
      x = cos(pi * (a[i] + a[j]) / L);
      y = sin(pi * (a[i] + a[j]) / L);
      int r = ((n - 2) - (j - i - 1)) - (j - i - 1);
      X += x * r;
      Y += y * r;
    }
  w = 1ll * n * (n - 1) * (n - 2) / 6;
  printf("%.15Lf %.15Lf\n", X / w, Y / w);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3005;
typedef long double db;
typedef long long ll;
const db pi = acos(-1.0);
int n, L, a[maxn];
ll w;
db x, y, X, Y;
int main() {
  cin >> n >> L;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a, a + n);
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++) {
      x = cos(pi * (a[i] + a[j]) / L);
      y = sin(pi * (a[i] + a[j]) / L);
      int r = ((n - 2) - (j - i - 1)) - (j - i - 1);
      X += x * r;
      Y += y * r;
    }
  w = 1ll * n * (n - 1) * (n - 2) / 6;
  printf("%.15Lf %.15Lf\n", X / w, Y / w);
  return 0;
}
Problem p02894 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 2005
const double PI = acos(-1.0);
typedef long long ll;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
int n, L, t[N];
double X, Y;
int main() {
  n = read(), L = read();
  for (int i = 1; i <= n; ++i) {
    t[i] = read();
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      X += (n - 2 * (j - i)) * cos(PI * (t[i] + t[j]) / L);
      Y += (n - 2 * (j - i)) * sin(PI * (t[i] + t[j]) / L);
    }
  }
  ll all = 1LL * n * (n - 1) * (n - 2) / 6;
  printf("%.12lf %.12lf\n", X / all, Y / all);
  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 3005
const double PI = acos(-1.0);
typedef long long ll;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
int n, L, t[N];
double X, Y;
int main() {
  n = read(), L = read();
  for (int i = 1; i <= n; ++i) {
    t[i] = read();
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      X += (n - 2 * (j - i)) * cos(PI * (t[i] + t[j]) / L);
      Y += (n - 2 * (j - i)) * sin(PI * (t[i] + t[j]) / L);
    }
  }
  ll all = 1LL * n * (n - 1) * (n - 2) / 6;
  printf("%.12lf %.12lf\n", X / all, Y / all);
  return 0;
}
Problem p02894 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (register int i = a; i <= b; i++)
#define per(i, a, b) for (register int i = a; i >= b; i--)
using namespace std;

typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef long long ll;

template <typename _T> inline void read(_T &f) {
  f = 0;
  _T fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + (c & 15);
    c = getchar();
  }
  f *= fu;
}

template <typename T> void print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x < 10)
    putchar(x + 48);
  else
    print(x / 10), putchar(x % 10 + 48);
}

template <typename T> void print(T x, char t) {
  print(x);
  putchar(t);
}

const double PI = acos(-1.0);
const int N = 1005;

int a[N];
double sx, sy;
int n, L;

int main() {
  read(n);
  read(L);
  for (register int i = 1; i <= n; i++)
    read(a[i]);
  for (register int i = 1; i <= n; i++) {
    for (register int j = i + 1; j <= n; j++) {
      double a1 = (double)(a[i] + a[j]) / 2 / L, a2 = a1 + 0.5;
      if (a2 >= 1)
        a2 -= 1;
      sx += cos(a1 * 2 * PI) * (n - (j - i + 1));
      sy += sin(a1 * 2 * PI) * (n - (j - i + 1));
      sx += cos(a2 * 2 * PI) * (j - i - 1);
      sy += sin(a2 * 2 * PI) * (j - i - 1);
    }
  }
  ll cnt = 1ll * n * (n - 1) * (n - 2) / 6;
  printf("%.9lf %.9lf\n", sx / cnt, sy / cnt);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (register int i = a; i <= b; i++)
#define per(i, a, b) for (register int i = a; i >= b; i--)
using namespace std;

typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef long long ll;

template <typename _T> inline void read(_T &f) {
  f = 0;
  _T fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + (c & 15);
    c = getchar();
  }
  f *= fu;
}

template <typename T> void print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x < 10)
    putchar(x + 48);
  else
    print(x / 10), putchar(x % 10 + 48);
}

template <typename T> void print(T x, char t) {
  print(x);
  putchar(t);
}

const double PI = acos(-1.0);
const int N = 3005;

int a[N];
double sx, sy;
int n, L;

int main() {
  read(n);
  read(L);
  for (register int i = 1; i <= n; i++)
    read(a[i]);
  for (register int i = 1; i <= n; i++) {
    for (register int j = i + 1; j <= n; j++) {
      double a1 = (double)(a[i] + a[j]) / 2 / L, a2 = a1 + 0.5;
      if (a2 >= 1)
        a2 -= 1;
      sx += cos(a1 * 2 * PI) * (n - (j - i + 1));
      sy += sin(a1 * 2 * PI) * (n - (j - i + 1));
      sx += cos(a2 * 2 * PI) * (j - i - 1);
      sy += sin(a2 * 2 * PI) * (j - i - 1);
    }
  }
  ll cnt = 1ll * n * (n - 1) * (n - 2) / 6;
  printf("%.9lf %.9lf\n", sx / cnt, sy / cnt);
  return 0;
}
Problem p02894 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define per(i, r, l) for (int i = (r); i >= (l); --i)
using namespace std;
typedef double db;
const db pi = acos(-1.0);
int n, L, a[2010];
db A, B;
int main() {
  scanf("%d%d", &n, &L);
  rep(i, 1, n) scanf("%d", a + i);
  sort(a + 1, a + n + 1);
  rep(i, 1, n) rep(j, i + 1, n) {
    db x = cos(db(a[i] + a[j]) / L * pi), y = sin(db(a[i] + a[j]) / L * pi);
    int k1 = j - i - 1, k2 = n - 2 - k1;
    A += (k2 - k1) * x, B += (k2 - k1) * y;
  }
  A = A / n / (n - 1) / (n - 2) * 6;
  B = B / n / (n - 1) / (n - 2) * 6;
  printf("%.15lf %.15lf", A, B);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define per(i, r, l) for (int i = (r); i >= (l); --i)
using namespace std;
typedef double db;
const db pi = acos(-1.0);
int n, L, a[3010];
db A, B;
int main() {
  scanf("%d%d", &n, &L);
  rep(i, 1, n) scanf("%d", a + i);
  sort(a + 1, a + n + 1);
  rep(i, 1, n) rep(j, i + 1, n) {
    db x = cos(db(a[i] + a[j]) / L * pi), y = sin(db(a[i] + a[j]) / L * pi);
    int k1 = j - i - 1, k2 = n - 2 - k1;
    A += (k2 - k1) * x, B += (k2 - k1) * y;
  }
  A = A / n / (n - 1) / (n - 2) * 6;
  B = B / n / (n - 1) / (n - 2) * 6;
  printf("%.15lf %.15lf", A, B);
  return 0;
}
Problem p02894 (C++)

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

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template <class T> void debug(T itr1, T itr2) {
  auto now = itr1;
  while (now < itr2) {
    cout << *now << " ";
    now++;
  }
  cout << endl;
}
#define repr(i, from, to) for (int i = (int)from; i < (int)to; i++)
#define rep(i, N) repr(i, 0, N)
#define per(i, N) for (int i = (int)N - 1; i >= 0; i--)

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;

int main() {
  ll N;
  cin >> N;
  double L;
  cin >> L;
  double T[(int)L];
  rep(i, N) cin >> T[i];

  double rx = 0, ry = 0;
  rep(i, N) repr(j, i + 1, N) {
    double theta1 = ((double)(T[i] + T[j]) / 2) * M_PI * 2 / L;
    double theta2 = (theta1 < M_PI) ? theta1 + M_PI : theta1 - M_PI;
    // cout << theta1 << "->" << (N+i)-j-1 << " ";
    // cout << theta2 << "->" << j-i-1 << endl;
    rx += cos(theta1) * ((N + i) - j - 1) + cos(theta2) * (j - i - 1);
    ry += sin(theta1) * ((N + i) - j - 1) + sin(theta2) * (j - i - 1);
  }
  rx /= N * (N - 1) * (N - 2) / 6;
  ry /= N * (N - 1) * (N - 2) / 6;
  cout << fixed << setprecision(10) << rx << " " << ry << endl;
  // cout << result << endl;
  return 0;
}

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

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template <class T> void debug(T itr1, T itr2) {
  auto now = itr1;
  while (now < itr2) {
    cout << *now << " ";
    now++;
  }
  cout << endl;
}
#define repr(i, from, to) for (int i = (int)from; i < (int)to; i++)
#define rep(i, N) repr(i, 0, N)
#define per(i, N) for (int i = (int)N - 1; i >= 0; i--)

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;

int main() {
  ll N;
  cin >> N;
  double L;
  cin >> L;
  double T[N];
  rep(i, N) cin >> T[i];

  double rx = 0, ry = 0;
  rep(i, N) repr(j, i + 1, N) {
    double theta1 = ((double)(T[i] + T[j]) / 2) * M_PI * 2 / L;
    double theta2 = (theta1 < M_PI) ? theta1 + M_PI : theta1 - M_PI;
    // cout << theta1 << "->" << (N+i)-j-1 << " ";
    // cout << theta2 << "->" << j-i-1 << endl;
    rx += cos(theta1) * ((N + i) - j - 1) + cos(theta2) * (j - i - 1);
    ry += sin(theta1) * ((N + i) - j - 1) + sin(theta2) * (j - i - 1);
  }
  rx /= N * (N - 1) * (N - 2) / 6;
  ry /= N * (N - 1) * (N - 2) / 6;
  cout << fixed << setprecision(10) << rx << " " << ry << endl;
  // cout << result << endl;
  return 0;
}
Problem p02895 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, s, t) for (int i = s; i < t; i++)
typedef long long ll;
ll dp[22][22][22], isused[22][22][22];
char isok[22][22];
ll solve(int l, int mid, int r) {
  if (r - l <= 0)
    return 1;
  if (mid <= l || mid >= r)
    return 0;
  if (isused[l][mid][r])
    return dp[l][mid][r];
  isused[l][mid][r] = 1;
  rep(i, l, mid) rep(j, mid + 1, r + 1) if (isok[i][j] == '1') rep(k1, i, mid)
      rep(k2, mid, j) dp[l][mid][r] +=
      solve(l, i, k1) * solve(k1 + 1, mid, k2) * solve(k2 + 1, j, r);
  return dp[l][mid][r];
}
int main() {
  int n;
  scanf("%d", &n);
  rep(i, 1, n * 2 + 1) scanf("%s", isok[i] + 1);
  ll res = 0;
  rep(i, 2, n * 2 + 1) if (isok[1][i] == '1') res += solve(2, i, n * 2);
  printf("%lld\n", res);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, s, t) for (int i = s; i < t; i++)
typedef long long ll;
ll dp[44][44][44], isused[44][44][44];
char isok[44][44];
ll solve(int l, int mid, int r) {
  if (r - l <= 0)
    return 1;
  if (mid <= l || mid >= r)
    return 0;
  if (isused[l][mid][r])
    return dp[l][mid][r];
  isused[l][mid][r] = 1;
  rep(i, l, mid) rep(j, mid + 1, r + 1) if (isok[i][j] == '1') rep(k1, i, mid)
      rep(k2, mid, j) dp[l][mid][r] +=
      solve(l, i, k1) * solve(k1 + 1, mid, k2) * solve(k2 + 1, j, r);
  return dp[l][mid][r];
}
int main() {
  int n;
  scanf("%d", &n);
  rep(i, 1, n * 2 + 1) scanf("%s", isok[i] + 1);
  ll res = 0;
  rep(i, 2, n * 2 + 1) if (isok[1][i] == '1') res += solve(2, i, n * 2);
  printf("%lld\n", res);
}
Problem p02895 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ri register int
typedef long long ll;
#define rll register ll
const int N = 45;
char e[N][N];
int n;
ll f[N][N][N], g[N][N][N];
inline void modify(ri i, ri j, ri k) {
  for (ri p = j + 1; p < n; ++p)
    if (e[p][k])
      g[i][j][p] += f[i][j][k];
}
int main() {
  ri l, i, j, k, p, q, y;
  rll res, ans;
  scanf("%d", &n);
  n <<= 1;
  for (i = 1; i <= n; ++i) {
    scanf("%s", e[i] + 1);
    for (j = 1; j <= n; ++j)
      e[i][j] &= 15;
  }
  if (n == 2)
    return printf("%d\n", e[1][2]);
  for (i = 1; i <= n; ++i)
    f[i][i][i] = 1, modify(i, i, i);
  for (i = 1; i + 2 <= n; ++i)
    if (e[i][i + 2])
      f[i][i + 2][i + 1] = 1, modify(i, i + 2, i + 1);
  for (l = 4; l < n; ++l)
    for (i = 1; (j = i + l - 1) <= n; ++i) {
      for (p = i; p <= j - 2; ++p)
        for (q = p + 2; q <= j; ++q) {
          res = 0;
          for (y = (q != j); y <= j - (q != j); ++y)
            res += g[i][p][y] * f[q][j][y];
          for (k = p + 1; k < q; ++k)
            f[i][j][k] += res * f[p + 1][q - 1][k];
        }
      for (k = i + 1; k < j; ++k)
        modify(i, j, k);
    }
  ans = 0;
  for (i = 1; i < n; ++i)
    if (e[n][i])
      ans += f[1][n - 1][i];
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ri register int
typedef long long ll;
#define rll register ll
const int N = 45;
char e[N][N];
int n;
ll f[N][N][N], g[N][N][N];
inline void modify(ri i, ri j, ri k) {
  for (ri p = j + 1; p < n; ++p)
    if (e[p][k])
      g[i][j][p] += f[i][j][k];
}
int main() {
  ri l, i, j, k, p, q, y;
  rll res, ans;
  scanf("%d", &n);
  n <<= 1;
  for (i = 1; i <= n; ++i) {
    scanf("%s", e[i] + 1);
    for (j = 1; j <= n; ++j)
      e[i][j] &= 15;
  }
  if (n == 2)
    return printf("%d\n", e[1][2]), 0;
  for (i = 1; i <= n; ++i)
    f[i][i][i] = 1, modify(i, i, i);
  for (i = 1; i + 2 <= n; ++i)
    if (e[i][i + 2])
      f[i][i + 2][i + 1] = 1, modify(i, i + 2, i + 1);
  for (l = 4; l < n; ++l)
    for (i = 1; (j = i + l - 1) <= n; ++i) {
      for (p = i; p <= j - 2; ++p)
        for (q = p + 2; q <= j; ++q) {
          res = 0;
          for (y = (q != j); y <= j - (q != j); ++y)
            res += g[i][p][y] * f[q][j][y];
          for (k = p + 1; k < q; ++k)
            f[i][j][k] += res * f[p + 1][q - 1][k];
        }
      for (k = i + 1; k < j; ++k)
        modify(i, j, k);
    }
  ans = 0;
  for (i = 1; i < n; ++i)
    if (e[n][i])
      ans += f[1][n - 1][i];
  printf("%lld\n", ans);
  return 0;
}
Problem p02895 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 22;
int n;
string s[N];
typedef long long ll;
ll ans, f[N][N][N], g[N][N][N][N];
ll F(int l, int r, int p);
ll G(int a, int b, int c, int d) {
  if (~g[a][b][c][d])
    return g[a][b][c][d];
  ll ret = 0;
  for (int i = a; i <= b; i++) {
    for (int j = c; j <= d; j++)
      if (s[i][j] == '1') {
        ret += F(a, b, i) * F(c, d, j);
      }
  }
  return g[a][b][c][d] = ret;
}
ll F(int l, int r, int p) {
  if (l == r)
    return 1;
  if (~f[l][r][p])
    return f[l][r][p];
  ll ret = 0;
  for (int i = l + 1; i <= p; i++) {
    for (int j = p; j < r; j++) {
      ret += G(l, i - 1, j + 1, r) * F(i, j, p);
    }
  }
  return f[l][r][p] = ret;
}
int main() {
  memset(f, -1, sizeof f);
  memset(g, -1, sizeof g);
  cin >> n;
  n <<= 1;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int p = 1; p < n; p++)
    if (s[0][p] == '1') {
      ans += F(1, n - 1, p);
    }
  printf("%lld\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 42;
int n;
string s[N];
typedef long long ll;
ll ans, f[N][N][N], g[N][N][N][N];
ll F(int l, int r, int p);
ll G(int a, int b, int c, int d) {
  if (~g[a][b][c][d])
    return g[a][b][c][d];
  ll ret = 0;
  for (int i = a; i <= b; i++) {
    for (int j = c; j <= d; j++)
      if (s[i][j] == '1') {
        ret += F(a, b, i) * F(c, d, j);
      }
  }
  return g[a][b][c][d] = ret;
}
ll F(int l, int r, int p) {
  if (l == r)
    return 1;
  if (~f[l][r][p])
    return f[l][r][p];
  ll ret = 0;
  for (int i = l + 1; i <= p; i++) {
    for (int j = p; j < r; j++) {
      ret += G(l, i - 1, j + 1, r) * F(i, j, p);
    }
  }
  return f[l][r][p] = ret;
}
int main() {
  memset(f, -1, sizeof f);
  memset(g, -1, sizeof g);
  cin >> n;
  n <<= 1;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int p = 1; p < n; p++)
    if (s[0][p] == '1') {
      ans += F(1, n - 1, p);
    }
  printf("%lld\n", ans);
}