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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
lint a[100100], s[100100];
map<lint, lint> me;
int main() {
  int n;
  lint ze = 1, out = 0, sum = 0;
  me[0]++;
  cin >> n;
  s[0] = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
    out += me[sum];
    me[sum]++;
  }
  cout << out << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> pint;
#define All(s) s.begin(), s.end()
#define rAll(s) s.rbegin(), s.rend()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
lint a[200100], s[100100];
map<lint, lint> me;
int main() {
  int n;
  lint ze = 1, out = 0, sum = 0;
  me[0]++;
  cin >> n;
  s[0] = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
    out += me[sum];
    me[sum]++;
  }
  cout << out << endl;
}
Problem p03363 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
const int MOD = 1e9 + 7;
typedef long long lint;
const int INF = 1e7;
#define rep(i, n) for (lint i = 0; i < (n); ++i)
#define repi(i, k, n) for (int i = (k); i < (int)(n); ++i)
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define all(x) (x).begin(), (x).end()
#define pb push_back
int main() {
  lint n;
  cin >> n;
  vector<lint> sum(n, 0);
  map<lint, lint> m;
  m[0]++;
  rep(i, n) {
    lint v;
    cin >> v;
    sum[i + 1] = sum[i] + v;
    m[sum[i + 1]]++;
  }
  lint ans = 0;
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    ans += itr->second * (itr->second - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
const int MOD = 1e9 + 7;
typedef long long lint;
const int INF = 1e7;
#define rep(i, n) for (lint i = 0; i < (n); ++i)
#define repi(i, k, n) for (int i = (k); i < (int)(n); ++i)
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define all(x) (x).begin(), (x).end()
#define pb push_back
int main() {
  lint n;
  cin >> n;
  vector<lint> sum(n + 1, 0);
  map<lint, lint> m;
  m[0]++;
  rep(i, n) {
    lint v;
    cin >> v;
    sum[i + 1] = sum[i] + v;
    m[sum[i + 1]]++;
  }
  lint ans = 0;
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    ans += itr->second * (itr->second - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}
Problem p03363 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <stdio.h>
using namespace std;
using ll = long long;

void solve();
int main() {
  solve();
#ifdef DBG
  while (true)
    ;
#endif
}

void solve() {
  ll n, A[200001];
  scanf("%lld", &n);
  A[0] = 0;
  for (ll i = 1; i <= n; i++)
    scanf("%lld", A + i), A[i] += A[i - 1];
  ll count = 0;
  for (ll i = 0; i < n; i++)
    for (ll j = i + 1; j <= n; j++)
      if (A[j] - A[i] == 0)
        count++;
  printf("%lld\n", count);
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
using namespace std;
using ll = long long;

void solve();
int main() {
  solve();
#ifdef DBG
  while (true)
    ;
#endif
}

void solve() {
  ll n, A[200001];
  scanf("%lld", &n);
  A[0] = 0;
  for (ll i = 1; i <= n; i++)
    scanf("%lld", A + i), A[i] += A[i - 1];
  sort(A, A + n + 1);
  ll count = 0, carry = 1;
  for (ll i = 1; i <= n; i++) {
    if (A[i] == A[i - 1])
      carry++;
    else {
      count += carry * (carry - 1) / 2;
      carry = 1;
    }
  }
  count += carry * (carry - 1) / 2;
  printf("%lld\n", count);
}
Problem p03363 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define Fopen                                                                  \
  freopen("_in.txt", "r", stdin);                                              \
  freopen("_out.txt", "w", stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
typedef pair<int, int> pll;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 1e5 + 10;
LL a[N];
LL b[N];
map<LL, LL> mp;
int main() {
  /// Fopen;
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]), b[i] = a[i] + b[i - 1], mp[b[i]]++;
  map<LL, LL>::iterator it = mp.begin();
  LL ans = 0;
  while (it != mp.end()) {
    if (it->first == 0)
      ans += it->second;
    ans += it->se * (it->se - 1) / 2;
    it++;
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define Fopen                                                                  \
  freopen("_in.txt", "r", stdin);                                              \
  freopen("_out.txt", "w", stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
typedef pair<int, int> pll;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 2e5 + 10;
LL a[N];
LL b[N];
map<LL, LL> mp;
int main() {
  /// Fopen;
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]), b[i] = a[i] + b[i - 1], mp[b[i]]++;
  map<LL, LL>::iterator it = mp.begin();
  LL ans = 0;
  while (it != mp.end()) {
    if (it->first == 0)
      ans += it->second;
    ans += it->se * (it->se - 1) / 2;
    it++;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03363 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define inf 2147483647
typedef long long ll;
const ll N = 1e5 + 20;
const ll M = 500000 + 20;
ll a[N], sum[N];
map<ll, ll> mp;
int main() {
  ll n;
  scanf("%lld", &n);
  mp[0] = 1;
  for (ll i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    sum[i] = sum[i - 1] + a[i];
    mp[sum[i]]++;
  }
  ll ans = 0;
  for (auto it = mp.begin(); it != mp.end(); it++) {
    ll a = it->first, b = it->second;
    if (b >= 2)
      ans += b * (b - 1) / 2;
    // cout<<it->first<<" "<<it->second<<endl;
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define inf 2147483647
typedef long long ll;
const ll N = 2e5 + 20;
const ll M = 500000 + 20;
ll a[N], sum[N];
map<ll, ll> mp;
int main() {
  ll n;
  scanf("%lld", &n);
  mp[0] = 1;
  for (ll i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    sum[i] = sum[i - 1] + a[i];
    mp[sum[i]]++;
  }
  ll ans = 0;
  for (auto it = mp.begin(); it != mp.end(); it++) {
    ll a = it->first, b = it->second;
    if (b >= 2)
      ans += b * (b - 1) / 2;
    // cout<<it->first<<" "<<it->second<<endl;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03363 (C++)

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

int main() {
  return 1;
  int n;
  cin >> n;
  map<long long, int> cnt;
  long long sum = 0;
  cnt[0]++;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    sum += a;
    cnt[sum]++;
  }
  long long ans = 0;
  for (auto &p : cnt) {
    ans += 1ll * p.second * (p.second - 1) / 2;
  }
  cout << ans << endl;
}

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

int main() {
  int n;
  cin >> n;
  map<long long, int> cnt;
  long long sum = 0;
  cnt[0]++;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    sum += a;
    cnt[sum]++;
  }
  long long ans = 0;
  for (auto &p : cnt) {
    ans += 1ll * p.second * (p.second - 1) / 2;
  }
  cout << ans << endl;
}
Problem p03363 (C++)

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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}
struct edge {
  ll to, cost;
};

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  ll cnt = 0;
  for (int i = 0; i < N; ++i) {
    ll a = 0;
    for (int k = i; k < N; ++k) {
      a += A[k];
      if (a == 0)
        cnt++;
    }
  }
  cout << cnt << endl;
}

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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}
struct edge {
  ll to, cost;
};

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  ll cnt = 0;
  vector<ll> S(N + 1);
  S[0] = 0;
  for (int i = 0; i < N; ++i)
    S[i + 1] = S[i] + A[i];
  map<ll, ll> m;
  REP(i, S.size()) m[S[i]]++;
  for (auto x : m) {
    cnt += 0.5 * x.second * (x.second - 1);
  }
  cout << cnt << endl;
}
Problem p03363 (C++)

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

// 累積和をとっておいてs[i] == s[j]なる(i, j)（i < j）の組み合わせを数えれば良い

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

  vector<long long> s(N);
  for (int i = 0; i < N; i++) {
    s[i + 1] = s[i] + a[i];
  }

  // s[i]と等しいiが何個あるかをnumsに記録しておく
  map<long long, long long> nums;
  for (int i = 0; i < N + 1; i++) {
    nums[s[i]]++;
  }

  long long res = 0;
  for (auto iter : nums) {
    long long num = iter.second;
    res += num * (num - 1) / 2; // nC2
  }
  cout << res << endl;
  return 0;
}

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

// 累積和をとっておいてs[i] == s[j]なる(i, j)（i < j）の組み合わせを数えれば良い

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

  vector<long long> s(N + 1);
  for (int i = 0; i < N; i++) {
    s[i + 1] = s[i] + a[i];
  }

  // s[i]と等しいiが何個あるかをnumsに記録しておく
  map<long long, long long> nums;
  for (int i = 0; i < N + 1; i++) {
    nums[s[i]]++;
  }

  long long res = 0;
  for (auto iter : nums) {
    long long num = iter.second;
    res += num * (num - 1) / 2; // nC2
  }
  cout << res << endl;
  return 0;
}
Problem p03363 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<ll> a(n), b(n + 1);
  rep(i, n) {
    cin >> a[i];
    b[i + 1] = b[i] + a[i];
  }

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < i; j++) {
      if (b[i] - b[j] == 0)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


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

int main() {
  int n;
  cin >> n;
  vector<ll> a(n), b(n + 1);
  rep(i, n) {
    cin >> a[i];
    b[i + 1] = b[i] + a[i];
  }

  ll ans = 0;
  map<ll, ll> mp;
  rep(i, n + 1) {
    ans += mp[b[i]];
    mp[b[i]]++;
  }
  cout << ans << endl;
  return 0;
}
Problem p03363 (C++)

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

using namespace std;

long a[111111];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%ld", a + i);
    a[i] += a[i - 1];
  }
  sort(a, a + n + 1);
  long r = 0, m = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] != a[i - 1])
      m = 0;
    else
      m++;
    r += m;
  }
  cout << r << endl;
  return 0;
}


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

using namespace std;

long a[222222];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%ld", a + i);
    a[i] += a[i - 1];
  }
  sort(a, a + n + 1);
  long r = 0, m = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] != a[i - 1])
      m = 0;
    else
      m++;
    r += m;
  }
  cout << r << endl;
  return 0;
}
Problem p03363 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
int a[100005];
int n;
int main() {
  while (~scanf("%d", &n)) {
    long long sum = 0;
    map<long long, int> s;
    long long ans = 0;
    s[0] = 1;
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
      ans += a[i];
      sum += s[ans];
      s[ans]++;
    }
    cout << sum << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
int a[1000005];
int n;
int main() {
  while (~scanf("%d", &n)) {
    long long sum = 0;
    map<long long, int> s;
    long long ans = 0;
    s[0] = 1;
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
      ans += a[i];
      sum += s[ans];
      s[ans]++;
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p03363 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
const int maxn = 100005;
using namespace std;
ll a[maxn], n, now, ans;
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", a + i), a[i] += a[i - 1];
  sort(a, a + n + 1), now = 1;
  for (int i = 1; i <= n; i++)
    if (a[i] != a[i - 1])
      now = 1;
    else
      ans += now, now++;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
const int maxn = 200005;
using namespace std;
ll a[maxn], n, now, ans;
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", a + i), a[i] += a[i - 1];
  sort(a, a + n + 1), now = 1;
  for (int i = 1; i <= n; i++)
    if (a[i] != a[i - 1])
      now = 1;
    else
      ans += now, now++;
  cout << ans << endl;
  return 0;
}
Problem p03363 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define nn 65536 * 4
#define ll long long
ll cnt[nn];
ll a[nn];
ll s[nn];
ll n;
int main() {
  ll ans = 0;
  cnt[0] = 1;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]), s[i] = s[i - 1] + a[i], ans += cnt[s[i]]++;
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define nn 65536 * 4
#define ll long long
map<ll, ll> cnt;
ll a[nn];
ll s[nn];
ll n;
int main() {
  ll ans = 0;
  cnt[0] = 1;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]), s[i] = s[i - 1] + a[i], ans += cnt[s[i]]++;
  cout << ans;
  return 0;
}
Problem p03363 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
void File() {
  freopen("A.in", "r", stdin);
  freopen("A.out", "w", stdout);
}
#define REP(i, a, b) for (register int i = a; i <= b; ++i)
#define DREP(i, a, b) for (register int i = a; i >= b; --i)
#define MREP(i, x) for (register int i = beg[x]; i; i = E[i].last)
#define ll long long
#define inf INT_MAX
const int maxn = 1e5 + 10;
int n;
ll a[maxn], sum[maxn], ans;
map<ll, ll> b;
int main() {
  // File();
  scanf("%d", &n);
  REP(i, 1, n) scanf("%lld", &a[i]);
  REP(i, 1, n) sum[i] = sum[i - 1] + a[i];
  b[0] = 1;
  REP(i, 1, n) {
    ans += b[sum[i]];
    ++b[sum[i]];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
void File() {
  freopen("A.in", "r", stdin);
  freopen("A.out", "w", stdout);
}
#define REP(i, a, b) for (register int i = a; i <= b; ++i)
#define DREP(i, a, b) for (register int i = a; i >= b; --i)
#define MREP(i, x) for (register int i = beg[x]; i; i = E[i].last)
#define ll long long
#define inf INT_MAX
const int maxn = 2e5 + 10;
int n;
ll a[maxn], sum[maxn], ans;
map<ll, ll> b;
int main() {
  // File();
  scanf("%d", &n);
  REP(i, 1, n) scanf("%lld", &a[i]);
  REP(i, 1, n) sum[i] = sum[i - 1] + a[i];
  b[0] = 1;
  REP(i, 1, n) {
    ans += b[sum[i]];
    ++b[sum[i]];
  }
  cout << ans << endl;
  return 0;
}
Problem p03363 (C++)

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

using namespace std;

#define pq priority_queue
#define MZ 1000000007

map<long long, int> mp;
long long sum[100005];

int main() {
  int n;
  long long ans = 0;
  scanf("%d", &n);
  mp[0] = 1;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &sum[i]);
    sum[i] += sum[i - 1];
    ans += mp[sum[i]];
    mp[sum[i]]++;
  }
  printf("%lld\n", ans);
  return 0;
}


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

using namespace std;

#define pq priority_queue
#define MZ 1000000007

map<long long, int> mp;
long long sum[200005];

int main() {
  int n;
  long long ans = 0;
  scanf("%d", &n);
  mp[0] = 1;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &sum[i]);
    sum[i] += sum[i - 1];
    ans += mp[sum[i]];
    mp[sum[i]]++;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03363 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) FOR(i, 0, b)
#define INF mugen
#define dump(x) cerr << #x << "=" << x << endl
#define all(a) (a).begin(), (a).end()
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<vii> viii;
typedef pair<int, int> P;
template <class T> void chmin(T &a, T const &b) {
  if (b < a)
    a = b;
}
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
using ll = long long;
const ll mod = LLONG_MAX;

int a[112345];
int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];

  map<ll, int> cnt;
  ll sum = 0;
  ll ans = 0;
  rep(i, n) {
    sum += a[i];
    if (sum == 0)
      ans++;
    ans += cnt[sum];
    cnt[sum]++;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) FOR(i, 0, b)
#define INF mugen
#define dump(x) cerr << #x << "=" << x << endl
#define all(a) (a).begin(), (a).end()
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<vii> viii;
typedef pair<int, int> P;
template <class T> void chmin(T &a, T const &b) {
  if (b < a)
    a = b;
}
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
using ll = long long;
const ll mod = LLONG_MAX;

int a[212345];
int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];

  map<ll, int> cnt;
  ll sum = 0;
  ll ans = 0;
  rep(i, n) {
    sum += a[i];
    if (sum == 0)
      ans++;
    ans += cnt[sum];
    cnt[sum]++;
  }
  cout << ans << endl;
}
Problem p03363 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define fi first
#define se second
#define pr(x) cerr << #x << "=" << (x) << endl
#define pri(x, lo)                                                             \
  {                                                                            \
    cerr << #x << "={";                                                        \
    for (int ol = 0; ol <= lo; ol++)                                           \
      cerr << x[ol] << ",";                                                    \
    cerr << "}" << endl;                                                       \
  }
#define inf 100000000
#define N 1000
map<ll, ll> mp;
ll sum[N], ans, a[N], i, n;
int main() {
  scanf("%lld", &n);
  mp[0]++;
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    sum[i] = sum[i - 1] + a[i];
  }
  for (i = 1; i <= n; i++) {
    ans += mp[sum[i]];
    mp[sum[i]]++;
  }
  printf("%lld\n", ans);
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define fi first
#define se second
#define pr(x) cerr << #x << "=" << (x) << endl
#define pri(x, lo)                                                             \
  {                                                                            \
    cerr << #x << "={";                                                        \
    for (int ol = 0; ol <= lo; ol++)                                           \
      cerr << x[ol] << ",";                                                    \
    cerr << "}" << endl;                                                       \
  }
#define inf 100000000
#define N 1000100
map<ll, ll> mp;
ll sum[N], ans, a[N], i, n;
int main() {
  scanf("%lld", &n);
  mp[0]++;
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    sum[i] = sum[i - 1] + a[i];
  }
  for (i = 1; i <= n; i++) {
    ans += mp[sum[i]];
    mp[sum[i]]++;
  }
  printf("%lld\n", ans);
}
Problem p03363 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<ll> sum(n + 1, 0);
  rep(i, n) {
    ll num = 0;
    cin >> num;
    sum[i + 1] = sum[i] + num;
  }
  vector<ll> cnt(n + 1, 0);
  ll ans = 0;
  rep(i, n + 1) {
    ans += cnt[sum[i]];
    cnt[sum[i]]++;
  }
  cout << ans << endl;
}

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

int main() {
  int n;
  cin >> n;
  vector<ll> sum(n + 1, 0);
  rep(i, n) {
    ll num = 0;
    cin >> num;
    sum[i + 1] = sum[i] + num;
  }
  map<ll, ll> cnt;
  ll ans = 0;
  rep(i, n + 1) {
    ans += cnt[sum[i]];
    cnt[sum[i]]++;
  }
  cout << ans << endl;
}
Problem p03363 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define maxn 100010
#define ll long long
struct arr {
  ll x;
  int y, t;
} b[maxn] = {0};
int a[maxn], cnt[maxn] = {0}, n, i;
ll ans = 0, s = 0;
int cmp1(const void *a, const void *b) {
  if ((*(arr *)a).x > (*(arr *)b).x)
    return 1;
  else
    return -1;
}
int cmp2(const void *a, const void *b) { return (*(arr *)a).t - (*(arr *)b).t; }
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    b[i].x = b[i - 1].x + (ll)a[i];
    b[i].t = i;
  }
  qsort(b + 1, n, sizeof(b[0]), cmp1);
  b[1].y = 1;
  for (i = 2; i <= n; i++)
    if (b[i].x == b[i - 1].x)
      b[i].y = b[i - 1].y;
    else
      b[i].y = b[i - 1].y + 1;
  qsort(b + 1, n, sizeof(b[0]), cmp2);
  for (i = 1; i <= n; i++) {
    ans += (ll)cnt[b[i].y];
    if (b[i].x == 0LL)
      ans++;
    cnt[b[i].y]++;
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define maxn 200010
#define ll long long
struct arr {
  ll x;
  int y, t;
} b[maxn] = {0};
int a[maxn], cnt[maxn] = {0}, n, i;
ll ans = 0, s = 0;
int cmp1(const void *a, const void *b) {
  if ((*(arr *)a).x > (*(arr *)b).x)
    return 1;
  else
    return -1;
}
int cmp2(const void *a, const void *b) { return (*(arr *)a).t - (*(arr *)b).t; }
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    b[i].x = b[i - 1].x + (ll)a[i];
    b[i].t = i;
  }
  qsort(b + 1, n, sizeof(b[0]), cmp1);
  b[1].y = 1;
  for (i = 2; i <= n; i++)
    if (b[i].x == b[i - 1].x)
      b[i].y = b[i - 1].y;
    else
      b[i].y = b[i - 1].y + 1;
  qsort(b + 1, n, sizeof(b[0]), cmp2);
  for (i = 1; i <= n; i++) {
    ans += (ll)cnt[b[i].y];
    if (b[i].x == 0LL)
      ans++;
    cnt[b[i].y]++;
  }
  printf("%lld", ans);
  return 0;
}
Problem p03363 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
#define lop(i, s, t) for (int i = s; i < (t); ++i)
#define rep(i, s, t) for (int i = s; i <= (t); ++i)
#define dec(i, s, t) for (int i = s; i >= (t); --i)
#define cmin(a, b) (a > b ? a = b : a)
#define cmax(a, b) (a < b ? a = b : a)
using namespace std;

#define Pr(f, ...) printf(f, ##__VA_ARGS__), fflush(stdout)

const int N = 1e5 + 50;

typedef long long ll;

int n;
ll ans, a[N];
map<ll, ll> cnt;

int main() {
#ifdef CURIOUSCAT
  freopen("dat.in", "r", stdin);
  // freopen("my.out","w",stdout);
#endif
  scanf("%d", &n);
  rep(i, 1, n) scanf("%lld", a + i);
  ++cnt[0];
  ll sum = 0;
  rep(i, 1, n) {
    sum += a[i];
    ans += cnt[sum];
    ++cnt[sum];
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
#define lop(i, s, t) for (int i = s; i < (t); ++i)
#define rep(i, s, t) for (int i = s; i <= (t); ++i)
#define dec(i, s, t) for (int i = s; i >= (t); --i)
#define cmin(a, b) (a > b ? a = b : a)
#define cmax(a, b) (a < b ? a = b : a)
using namespace std;

#define Pr(f, ...) printf(f, ##__VA_ARGS__), fflush(stdout)

const int N = 2e5 + 50;

typedef long long ll;

int n;
ll ans, a[N];
map<ll, ll> cnt;

int main() {
#ifdef CURIOUSCAT
  freopen("dat.in", "r", stdin);
  // freopen("my.out","w",stdout);
#endif
  scanf("%d", &n);
  rep(i, 1, n) scanf("%lld", a + i);
  ++cnt[0];
  ll sum = 0;
  rep(i, 1, n) {
    sum += a[i];
    ans += cnt[sum];
    ++cnt[sum];
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03363 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int INF = 1 << 28;
const double EPS = 1e-10;
// cout << fixed << std::setprecision(9) << flush;
// memset(a, 0, sizeof(a));
//--------------------------

int n;
ll a[100005];
map<ll, int> mp;

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

  ll ans = 0;
  ll sum = 0;

  for (int i = 0; i < n; i++) {
    sum += a[i];
    if (sum == 0)
      ans++;

    if (mp.find(sum) != mp.end()) {
      ans += mp[sum];
      mp[sum]++;
    } else {
      mp[sum] = 1;
    }
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int INF = 1 << 28;
const double EPS = 1e-10;
// cout << fixed << std::setprecision(9) << flush;
// memset(a, 0, sizeof(a));
//--------------------------

int n;
ll a[200005];
map<ll, int> mp;

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

  ll ans = 0;
  ll sum = 0;

  for (int i = 0; i < n; i++) {
    sum += a[i];
    if (sum == 0)
      ans++;

    if (mp.find(sum) != mp.end()) {
      ans += mp[sum];
      mp[sum]++;
    } else {
      mp[sum] = 1;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p03363 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
map<LL, LL> cnt;
const int maxn = 1e5 + 10;
LL a[maxn], sum[maxn];

int main() {
  int N;
  LL ans = 0;
  scanf("%d", &N);
  cnt[0]++;
  for (int i = 1; i <= N; ++i) {
    scanf("%lld", a + i), sum[i] = sum[i - 1] + a[i];
    ans += cnt[sum[i]];
    cnt[sum[i]]++;
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
map<LL, LL> cnt;
const int maxn = 2e5 + 10;
LL a[maxn], sum[maxn];

int main() {
  int N;
  LL ans = 0;
  scanf("%d", &N);
  cnt[0]++;
  for (int i = 1; i <= N; ++i) {
    scanf("%lld", a + i), sum[i] = sum[i - 1] + a[i];
    ans += cnt[sum[i]];
    cnt[sum[i]]++;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03363 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define scan(arg) cin >> arg
#define prin(arg) cout << arg << "\n"
#define prin2(arg1, arg2) cout << arg1 << " " << arg2 << "\n"
using namespace std;
typedef long long ll;
ll a[100010];
ll s[100010]; // s[i+1]:=a[0]~a[i]までの和
int n;

void acc_sum() { // 累積和
  s[0] = 0;
  rep1(i, n) { s[i] = s[i - 1] + a[i - 1]; }
}
ll sec_sum(int l, int r) { // a[l]~a[r]の区間和
  acc_sum();
  return s[r + 1] - s[l];
}
int main() {
  scan(n);
  rep(i, n) { scan(a[i]); }
  acc_sum();
  map<ll, ll> mi;
  rep(i, n + 1) { mi[s[i]]++; }
  ll sum = 0;
  for (auto ite = mi.begin(); ite != mi.end(); ite++) {
    ll t = ite->second;
    //   prin2(ite->first,t);
    if (t >= 2) {
      sum += t * (t - 1) / 2;
    }
  }
  prin(sum);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define scan(arg) cin >> arg
#define prin(arg) cout << arg << "\n"
#define prin2(arg1, arg2) cout << arg1 << " " << arg2 << "\n"
using namespace std;
typedef long long ll;
ll a[200010];
ll s[200010]; // s[i+1]:=a[0]~a[i]までの和
int n;

void acc_sum() { // 累積和
  s[0] = 0;
  rep1(i, n) { s[i] = s[i - 1] + a[i - 1]; }
}
ll sec_sum(int l, int r) { // a[l]~a[r]の区間和
  acc_sum();
  return s[r + 1] - s[l];
}
int main() {
  scan(n);
  rep(i, n) { scan(a[i]); }
  acc_sum();
  map<ll, ll> mi;
  rep(i, n + 1) { mi[s[i]]++; }
  ll sum = 0;
  for (auto ite = mi.begin(); ite != mi.end(); ite++) {
    ll t = ite->second;
    //   prin2(ite->first,t);
    if (t >= 2) {
      sum += t * (t - 1) / 2;
    }
  }
  prin(sum);

  return 0;
}
Problem p03363 (C++)

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

typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)

/* solve */

int main() {
  int n;
  cin >> n;
  ll a[100010];
  ll s[100010];
  ll tmp = 0;
  map<ll, ll> mp;
  mp[0]++;
  s[0] = 0;
  for (int i = 1; i < n + 1; i++) {
    cin >> a[i];
    s[i] = s[i - 1] + a[i];
    mp[s[i]]++;
  }

  ll ans = 0;
  set<ll> st;
  rep(i, n) { st.insert(s[i]); }
  for (auto itr = st.begin(); itr != st.end(); itr++) {
    tmp = mp[*itr];
    if (tmp > 1)
      ans += tmp * (tmp - 1) / 2;
  }
  cout << ans;
}

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

typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)

/* solve */

int main() {
  int n;
  cin >> n;
  ll a[200010];
  ll s[200010];
  ll tmp = 0;
  map<ll, ll> mp;
  mp[0]++;
  s[0] = 0;
  for (int i = 1; i < n + 1; i++) {
    cin >> a[i];
    s[i] = s[i - 1] + a[i];
    mp[s[i]]++;
  }

  ll ans = 0;
  set<ll> st;
  rep(i, n) { st.insert(s[i]); }
  for (auto itr = st.begin(); itr != st.end(); itr++) {
    tmp = mp[*itr];
    if (tmp > 1)
      ans += tmp * (tmp - 1) / 2;
  }
  cout << ans;
}
Problem p03363 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#define rep(i, s, t) for (i = s; i <= t; ++i)
#define dep(i, s, t) for (i = s; i >= t; --i)

using namespace std;

typedef long long LL;
const int N = 1e5 + 10;
int n;
LL a[N], ans;

int main() {
  LL i, t = 0;
  scanf("%d", &n);
  rep(i, 1, n) {
    scanf("%lld", &a[i]);
    a[i] += a[i - 1];
  }
  sort(a, a + n + 1);

  rep(i, 0, n) if (!i || a[i] != a[i - 1]) {
    ans += (t - 1) * t / 2;
    t = 1;
  }
  else {
    ++t;
  }
  if (t)
    ans += (t - 1) * t / 2;
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define rep(i, s, t) for (i = s; i <= t; ++i)
#define dep(i, s, t) for (i = s; i >= t; --i)

using namespace std;

typedef long long LL;
const int N = 2e5 + 10;
int n;
LL a[N], ans;

int main() {
  LL i, t = 0;
  scanf("%d", &n);
  rep(i, 1, n) {
    scanf("%lld", &a[i]);
    a[i] += a[i - 1];
  }
  sort(a, a + n + 1);

  rep(i, 0, n) if (!i || a[i] != a[i - 1]) {
    ans += (t - 1) * t / 2;
    t = 1;
  }
  else {
    ++t;
  }
  if (t)
    ans += (t - 1) * t / 2;
  printf("%lld\n", ans);
  return 0;
}
Problem p03363 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long a, s[100005];
  map<long, long> mp;
  // mp[0]=1;
  for (int i = 1; i <= n; i++) {
    cin >> a;
    s[i] = s[i - 1] + a;
  }
  for (int i = 0; i <= n; i++)
    ++mp[s[i]];
  long long ans = 0;
  for (auto t : mp) {
    ans += (t.second * (t.second - 1)) / 2;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
// #include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long a, s[300005];
  map<long, long> mp;
  // mp[0]=1;
  for (int i = 1; i <= n; i++) {
    cin >> a;
    s[i] = s[i - 1] + a;
  }
  for (int i = 0; i <= n; i++)
    ++mp[s[i]];
  long long ans = 0;
  for (auto t : mp) {
    ans += (t.second * (t.second - 1)) / 2;
  }
  cout << ans << endl;
}
Problem p03364 (C++)

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

int n, ans;
char a[N][N];
long long h[N][N], v[N][N], p[N];

long long H(int i, int j, int k) {
  return h[i + k][j + n - 1] - h[i + k][j + k] * p[n - k - 1];
}

long long V(int i, int j, int k) {
  return v[i + n - 1][j + k] - v[i + k][j + k] * p[n - k - 1];
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  p[0] = 1;
  for (int i = 1; i < N; i++) {
    p[i] = p[i - 1] * 42073;
  }
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
      a[i][j + n] = a[i + n][j] = a[i + n][j + n] = a[i][j];
    }
  }
  for (int i = 1; i <= n * 2; i++) {
    for (int j = 1; j <= n * 2; j++) {
      h[i][j] = h[i][j - 1] * 42073 + a[i][j];
    }
  }
  for (int i = 1; i <= n * 2; i++) {
    for (int j = 1; j <= n * 2; j++) {
      v[j][i] = v[j - 1][i] * 42073 + a[j][i];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      int ok = 1;
      for (int k = 0; k < n; k++) {
        ok &= H(i, j, k) == V(i, j, k);
      }
      ans += ok;
    }
  }
  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 605;

int n, ans;
char a[N][N];
long long h[N][N], v[N][N], p[N];

long long H(int i, int j, int k) {
  return h[i + k][j + n - 1] - h[i + k][j + k] * p[n - k - 1];
}

long long V(int i, int j, int k) {
  return v[i + n - 1][j + k] - v[i + k][j + k] * p[n - k - 1];
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  p[0] = 1;
  for (int i = 1; i < N; i++) {
    p[i] = p[i - 1] * 42073;
  }
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
      a[i][j + n] = a[i + n][j] = a[i + n][j + n] = a[i][j];
    }
  }
  for (int i = 1; i <= n * 2; i++) {
    for (int j = 1; j <= n * 2; j++) {
      h[i][j] = h[i][j - 1] * 42073 + a[i][j];
    }
  }
  for (int i = 1; i <= n * 2; i++) {
    for (int j = 1; j <= n * 2; j++) {
      v[j][i] = v[j - 1][i] * 42073 + a[j][i];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      int ok = 1;
      for (int k = 0; k < n; k++) {
        ok &= H(i, j, k) == V(i, j, k);
      }
      ans += ok;
    }
  }
  cout << ans << "\n";
}
Problem p03364 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
char a[200][200];
int f[200][200], n, ans;
void ck(int x, int y) {
  bool flag = 0;
  int xx = x - 1, yy = y - 1;
  for (int i = 1; i <= n; ++i) {
    if (flag)
      break;
    for (int j = i + 1; j <= n; ++j) {
      int x1 = xx + i;
      int y1 = j + yy;
      int x2 = xx + j;
      int y2 = yy + i;
      if (x1 > n)
        x1 -= n;
      if (y2 > n)
        y2 -= n;
      if (y1 > n)
        y1 -= n;
      if (x2 > n)
        x2 -= n;
      if (a[x1][y1] != a[x2][y2]) {
        flag = 1;
        break;
      }
    }
  }
  if (!flag)
    f[x][y] = 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%s", a[i] + 1);
  for (int i = 1; i <= n; ++i)
    ck(1, i), ck(i, 1);
  for (int i = 2; i <= n; ++i)
    for (int j = 2; j <= n; ++j)
      f[i][j] = f[i - 1][j - 1];
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      ans += f[i + 1][j + 1];
  cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
char a[400][400];
int f[400][400], n, ans;
void ck(int x, int y) {
  bool flag = 0;
  int xx = x - 1, yy = y - 1;
  for (int i = 1; i <= n; ++i) {
    if (flag)
      break;
    for (int j = i + 1; j <= n; ++j) {
      int x1 = xx + i;
      int y1 = j + yy;
      int x2 = xx + j;
      int y2 = yy + i;
      if (x1 > n)
        x1 -= n;
      if (y2 > n)
        y2 -= n;
      if (y1 > n)
        y1 -= n;
      if (x2 > n)
        x2 -= n;
      if (a[x1][y1] != a[x2][y2]) {
        flag = 1;
        break;
      }
    }
  }
  if (!flag)
    f[x][y] = 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%s", a[i] + 1);
  for (int i = 1; i <= n; ++i)
    ck(1, i), ck(i, 1);
  for (int i = 2; i <= n; ++i)
    for (int j = 2; j <= n; ++j)
      f[i][j] = f[i - 1][j - 1];
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      ans += f[i + 1][j + 1];
  cout << ans;
  return 0;
}
Problem p03364 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define INT long long int
#define MOD 1000000007
#define oo (1e18)
using namespace std;
INT n, ans;
string s[330], a[660], b[660];
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  for (int i = 0; i < 2 * n; i++)
    b[i] = a[i] = s[i % n] + s[i % n];
  for (int i = 0; i < 2 * n; i++)
    for (int j = 0; j < 2 * n; j++)
      b[j][i] = a[i][j];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      bool same = true;
      for (int k = 0; same and k < n; k++)
        for (int ii = 0; same and ii < n - k; ii++)
          if (a[i + k][j + k + ii] != b[j + k][i + k + ii])
            same = false;
      if (same)
        ans++;
    }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define INT long long int
#define MOD 1000000007
#define oo (1e18)
using namespace std;
INT n, ans;
string s[330], a[660], b[660];
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  for (int i = 0; i < 2 * n; i++)
    b[i] = a[i] = s[i % n] + s[i % n];
  for (int i = 0; i < 2 * n; i++)
    for (int j = 0; j < 2 * n; j++)
      b[j][i] = a[i][j];
  for (int j = 0; j < n; j++) {
    bool same = true;
    for (int k = 0; same and k < n; k++)
      if (a[k].substr(j + k, n - k) != b[j + k].substr(k, n - k))
        same = false;
    if (same)
      ans += n;
  }
  cout << ans << endl;
  return 0;
}
Problem p03364 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (int(i) = (s); (i) <= (e); (i)++)

int N;

vector<string> S;

int res(int A) {
  vector<string> fie;
  fie.resize(N);

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      fie[i][j] = S[(i + A) % N][j];
    }
  }

  bool ok = true;
  rep(i, 0, N - 1) {
    rep(j, 0, N - 1) { ok &= fie[i][j] == fie[j][i]; }
  }

  if (ok) {
    return N;
  }
  return 0;
}

int main() {
  cin >> N;
  S.resize(N);
  rep(i, 0, N - 1) { cin >> S[i]; }

  int ans = 0;
  rep(i, 0, N - 1) { ans += res(i); }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (int(i) = (s); (i) <= (e); (i)++)

int N;

vector<string> S;

int res(int A) {
  vector<vector<char>> fie;
  fie.resize(N, vector<char>(N));

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      fie[i][j] = S[(i + A) % N][j];
    }
  }

  bool ok = true;
  rep(i, 0, N - 1) {
    rep(j, 0, N - 1) { ok &= fie[i][j] == fie[j][i]; }
  }

  if (ok) {
    return N;
  }
  return 0;
}

int main() {
  cin >> N;
  S.resize(N);
  rep(i, 0, N - 1) { cin >> S[i]; }

  int ans = 0;
  rep(i, 0, N - 1) { ans += res(i); }

  cout << ans << endl;
}
Problem p03364 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define _ << " " <<
#define all(X) (X).begin(), (X).end()
#define len(X) (X).size()
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define Tiii tuple<int, int, int>
#define Tlll tuple<ll, ll, ll>

int n;
char s[305][305];

bool check(int p, int q) {
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (s[(p + i) % n][(q + j) % n] != s[(p + j) % n][(q + i) % n])
        return false;
    }
  }
  return true;
}

int main() {
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> s[i];

  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      ans += check(i, j);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define _ << " " <<
#define all(X) (X).begin(), (X).end()
#define len(X) (X).size()
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define Tiii tuple<int, int, int>
#define Tlll tuple<ll, ll, ll>

int n;
char s[305][305];

bool check(int p, int q) {
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (s[(p + i) % n][(q + j) % n] != s[(p + j) % n][(q + i) % n])
        return false;
    }
  }
  return true;
}

int main() {
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> s[i];

  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += check(i, 0) * n;
  }
  cout << ans << endl;
}
Problem p03364 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mp make_pair
#define pii pair<int, int>
typedef long long ll;
using namespace std;
int n;
long long cnt;
string s1[305], s2[305];
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s1[i];
  }
  for (int l = 0; l < n; l++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        s2[(i + l) % n][j] = s1[i][j];
      }
    }
    bool ig = 1;
    for (int i = 0; i < n && ig; i++) {
      for (int j = i; j < n && ig; j++) {
        if (s2[i][j] != s2[j][i])
          ig = 0;
      }
    }
    if (ig) {
      cnt += n - l;
    }
  }
  for (int l = 1; l < n; l++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        s2[i][(j + l) % n] = s1[i][j];
      }
    }
    bool ig = 1;
    for (int i = 0; i < n && ig; i++) {
      for (int j = i; j < n && ig; j++) {
        if (s2[i][j] != s2[j][i])
          ig = 0;
      }
    }
    if (ig) {
      cnt += n - l;
    }
  }
  cout << cnt;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mp make_pair
#define pii pair<int, int>
typedef long long ll;
using namespace std;
int n;
long long cnt;
string s1[305], s2[305];
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s1[i];
    s2[i] = s1[i];
  }
  for (int l = 0; l < n; l++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        s2[(i + l) % n][j] = s1[i][j];
      }
    }
    bool ig = 1;
    for (int i = 0; i < n && ig; i++) {
      for (int j = i; j < n && ig; j++) {
        if (s2[i][j] != s2[j][i])
          ig = 0;
      }
    }
    if (ig) {
      cnt += n - l;
    }
  }
  for (int l = 1; l < n; l++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        s2[i][(j + l) % n] = s1[i][j];
      }
    }
    bool ig = 1;
    for (int i = 0; i < n && ig; i++) {
      for (int j = i; j < n && ig; j++) {
        if (s2[i][j] != s2[j][i])
          ig = 0;
      }
    }
    if (ig) {
      cnt += n - l;
    }
  }
  cout << cnt;
  return 0;
}
Problem p03364 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define maxn 309
using namespace std;
char s[maxn * 2][maxn * 2];
int f[maxn * 2][maxn * 2];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
    for (int j = n + 1; j < 2 * n; j++)
      s[i][j] = s[i][j - n];
  }
  for (int i = n + 1; i < 2 * n; i++)
    for (int j = 1; j < 2 * n; j++)
      s[i][j] = s[i - n][j];
  for (int i = 1; i < 2 * n; i++)
    for (int j = 1; j < 2 * n; j++) {
      bool bg = 1;
      for (int k = 0; k < n; k++) {
        if (s[i + k][j] != s[i][j + k])
          bg = 0;
        if (bg)
          f[i][j] = k;
      }
    }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      bool bg = 1;
      for (int k = 0; k < n; k++)
        if (f[i + k][j + k] < n - k - 1)
          bg = 0;
      ans += bg;
    }
  printf("%d", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define maxn 609
using namespace std;
char s[maxn * 2][maxn * 2];
int f[maxn * 2][maxn * 2];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
    for (int j = n + 1; j < 2 * n; j++)
      s[i][j] = s[i][j - n];
  }
  for (int i = n + 1; i < 2 * n; i++)
    for (int j = 1; j < 2 * n; j++)
      s[i][j] = s[i - n][j];
  for (int i = 1; i < 2 * n; i++)
    for (int j = 1; j < 2 * n; j++) {
      bool bg = 1;
      for (int k = 0; k < n; k++) {
        if (s[i + k][j] != s[i][j + k])
          bg = 0;
        if (bg)
          f[i][j] = k;
      }
    }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      bool bg = 1;
      for (int k = 0; k < n; k++)
        if (f[i + k][j + k] < n - k - 1)
          bg = 0;
      ans += bg;
    }
  printf("%d", ans);
  return 0;
}