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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define mod 1000000007
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

using namespace std;
using ll = long long;
using pii = pair<int, int>;

// 最大公約数
ll gcd(ll a, ll b) { return b ? gcd(b, a) : a; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      for (ll k = 1; k <= n; k++) {
        ans += gcd(gcd(i, j), k);
      }
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define mod 1000000007
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

using namespace std;
using ll = long long;
using pii = pair<int, int>;

// 最大公約数
ll gcd(ll a, ll b) { return b ? gcd(b, a %= b) : a; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      for (ll k = 1; k <= n; k++) {
        ans += gcd(gcd(i, j), k);
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p02713 (C++)

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

int main() {
  int K;
  cin >> K;
  int A = 0;

  for (int i = 1; i <= K; i++) {
    for (int j = 1; j <= K; j++) {
      for (int k = 1; k <= K; k++) {
        for (int l = K; l >= 1; l--) {
          if (i % l == 0 && j % l == 0 && k % l == 0) {
            A += l;
            break;
          }
        }
      }
    }
  }
  cout << A << endl;
}


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

int main() {
  int K;
  cin >> K;
  int A = 0;

  for (int i = 1; i <= K; i++) {
    for (int j = 1; j <= K; j++) {
      for (int k = 1; k <= K; k++) {
        int B = min(i, j);
        B = min(B, k);
        for (int l = B; l >= 1; l--) {
          if (i % l == 0 && j % l == 0 && k % l == 0) {
            A += l;
            break;
          }
        }
      }
    }
  }
  cout << A << endl;
}
Problem p02713 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define mp make_pair
#define pb push_back
#define ll long long
#define kl k << 1
#define kr k << 1 | 1
using namespace std;
inline ll read() {
  ll s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
void put1() { puts("YES"); }
void put2() { puts("NO"); }
void put3() { puts("-1"); }
ll qp(ll a, ll b, ll p) {
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = (ans * a) % p;
      --b;
    }
    a = (a * a) % p;
    b >>= 1;
  }
  return ans % p;
}

const int manx = 1e5 + 5;
const int mo = 998244353;

map<pair<ll, ll>, ll> gc;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

int main() {
  ll n = read(), ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        ll x, y;
        if (gc[mp(i, j)])
          x = gc[mp(i, j)];
        else
          x = gc[mp(i, j)] = gcd(i, j);
        if (gc[mp(x, k)])
          y = gc[mp(x, k)];
        else
          y = gc[mp(x, k)] = gcd(x, k);
        ans += y;
      }
    }
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define mp make_pair
#define pb push_back
#define ll long long
#define kl k << 1
#define kr k << 1 | 1
using namespace std;
inline ll read() {
  ll s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
void put1() { puts("YES"); }
void put2() { puts("NO"); }
void put3() { puts("-1"); }
ll qp(ll a, ll b, ll p) {
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = (ans * a) % p;
      --b;
    }
    a = (a * a) % p;
    b >>= 1;
  }
  return ans % p;
}

const int manx = 1e5 + 5;
const int mo = 998244353;

map<pair<ll, ll>, ll> gc;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

int main() {
  ll n = read(), ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        ll x = gcd(i, j);
        ll y = gcd(x, k);
        ans += y;
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p02713 (Python)

Buggy submission (status: Time Limit Exceeded):
from math import gcd
from functools import reduce


def gcd_kai(*numbers):
    return reduce(gcd, numbers)


K = int(input())
ans = 0
for i in range(K):
    for j in range(K):
        for k in range(K):
            ans += gcd_kai(i + 1, j + 1, k + 1)
print(ans)


Fixed submission:
from math import gcd
from functools import reduce


def gcd_kai(*numbers):
    return reduce(gcd, numbers)


K = int(input())
ans = 1
if K == 1:
    next
else:
    ans += (K - 1) * K * 3
    ans_sub = 0
    for i in range(2, K):
        for j in range(i + 1, K + 1):
            for k in range(i, K + 1):
                ans_sub += gcd_kai(i, j, k)
    ans += ans_sub * 3
    for i in range(2, K + 1):
        ans += i
print(ans)
Problem p02713 (Python)

Buggy submission (status: Time Limit Exceeded):
import itertools
import math


def gcd(a: int, b: int, c: int) -> int:
    return math.gcd(math.gcd(a, b), c)


def main() -> None:
    k = int(input())

    s = sum(gcd(a, b, c) for a, b, c in itertools.product(range(1, k + 1), repeat=3))
    print(s)


if __name__ == "__main__":
    main()


Fixed submission:
import itertools
import math


def gcd(a: int, b: int, c: int) -> int:
    return math.gcd(math.gcd(a, b), c)


def main() -> None:
    k = int(input())

    one_to_k = range(1, k + 1)
    s1 = sum(one_to_k)
    s2 = sum(math.gcd(a, b) for a, b in itertools.combinations(one_to_k, 2))
    s3 = sum(gcd(a, b, c) for a, b, c in itertools.combinations(one_to_k, 3))
    print(s1 + 6 * s2 + 6 * s3)


if __name__ == "__main__":
    main()
Problem p02713 (Python)

Buggy submission (status: Runtime Error):
import math

k = int(input())
ans = 0

for a in range(k):
    for b in range(k):
        d = math.gcd(a + 1, b + 1)
        for c in range(k):
            ans += math(d, c + 1)

print(ans)


Fixed submission:
import math

k = int(input())
ans = 0

for a in range(k):
    for b in range(k):
        d = math.gcd(a + 1, b + 1)
        for c in range(k):
            ans += math.gcd(d, c + 1)

print(ans)
Problem p02713 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
typedef long long ll;
#define _GLIBCXX_DEBUG
typedef vector<int> vec;
typedef vector<ll> lvec;
typedef vector<char> cvec;
typedef pair<ll, ll> LP;
typedef tuple<ll, ll, ll> LT;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
int e(int x, int y) {
  if (x < y)
    swap(x, y);
  int r;
  while (true) {
    r = x % y;
    if (r == 0)
      return y;
    x = y;
    y = r;
  }
  return 0;
}
int main() {
  int k;
  cin >> k;
  int sum = 0;
  rep(i, k) {
    rep(j, k) {
      rep(l, k) {
        int a = e(i, j);
        int b = e(a, l);
        sum += b;
      }
    }
  }
  cout << sum << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
typedef long long ll;
#define _GLIBCXX_DEBUG
typedef vector<int> vec;
typedef vector<ll> lvec;
typedef vector<char> cvec;
typedef pair<ll, ll> LP;
typedef tuple<ll, ll, ll> LT;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
int e(int x, int y) {
  if (x < y)
    swap(x, y);
  int r;
  while (true) {
    r = x % y;
    if (r == 0)
      return y;
    x = y;
    y = r;
  }
  return 0;
}
int main() {
  int k;
  cin >> k;
  int sum = 0;
  rep(i, k) {
    rep(j, k) {
      rep(l, k) {
        int a = e(i, j);
        int b = e(a, l);
        sum += b;
      }
    }
  }
  cout << sum << endl;
}
Problem p02713 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b, int c) {
  for (int i = 200; i >= 1; i--) {
    if (a % i == 0 && b % i == 0 && c % i == 0)
      return i;
  }
}
int main() {
  int k;
  cin >> k;
  long long sum = 0;
  for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++)
      for (int k1 = 1; k1 <= k; k1++) {
        sum += gcd(i, j, k1);
      }
  cout << sum;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b, int c) {
  if (a < b)
    swap(a, b);
  int r = 0, ans = 0;
  while (1) {
    r = a % b;
    if (r == 0) {
      ans = b;
      break;
    } else {
      a = b;
      b = r;
    }
  }
  if (ans > c)
    swap(ans, c);
  while (1) {
    r = ans % c;
    if (r == 0) {
      return c;
    } else {
      ans = c;
      c = r;
    }
  }
}
int main() {
  int k;
  cin >> k;
  long long sum = 0;
  for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++)
      for (int k1 = 1; k1 <= k; k1++) {
        sum += gcd(i, j, k1);
      }
  cout << sum;
  return 0;
}
Problem p02713 (C++)

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

#define PI 3.14159265358979323846264338327950L
#define ll long long

using namespace std;

int solve(int i, int j) {
  int result = 1;
  for (int k = 2; k <= min(i, j); k++) {
    if (i % k == 0 && j % k == 0) {
      result = k;
    }
  }
  return result;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int k;
  cin >> k;
  ll sum = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int k = 1; k <= k; k++) {
        sum += solve(solve(i, j), k);
      }
    }
  }
  cout << sum << "\n";
}


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

#define PI 3.14159265358979323846264338327950L
#define ll long long

using namespace std;

int solve(int i, int j) {
  int result = 1;
  for (int k = 2; k <= min(i, j); k++) {
    if (i % k == 0 && j % k == 0) {
      result = k;
    }
  }
  return result;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int k;
  cin >> k;
  ll sum = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int m = 1; m <= k; m++) {
        sum += solve(solve(i, j), m);
      }
    }
  }
  cout << sum << "\n";
}
Problem p02713 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define ll long long int

using namespace std;

ll gcd2(int a, int b) {
  if (a % b == 0)
    return b;
  if (a < b) {
    int tmp = a;
    a = b;
    b = tmp;
  }
  return gcd2(b, a % b);
}

ll gcd3(int a, int b, int c) { return gcd2(gcd2(a, b), c); }

int main(void) {
  ll K, sum = 0;
  cin >> K;
  for (int i = 1; i <= K; i++) {
    for (int j = 1; j <= K; j++) {
      for (int k = 1; k <= K; k++) {
        sum += gcd3(i, j, k);
      }
    }
  }
  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#define ll long long int

using namespace std;

ll gcd2(int a, int b) {
  if (a % b == 0)
    return b;
  return gcd2(b, a % b);
}

ll gcd3(int a, int b, int c) { return gcd2(gcd2(a, b), c); }

int main(void) {
  ll K, sum = 0;
  cin >> K;
  for (int i = 1; i <= K; i++) {
    for (int j = 1; j <= K; j++) {
      for (int k = 1; k <= K; k++) {
        sum += gcd3(i, j, k);
      }
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02713 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define INF 1000000009
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
#define REP(i, a, b) for (int i = a; i <= b; i++)

int main(int argc, char const *argv[]) {
  map<pair<int, pair<int, int>>, int> mp;
  int n;
  cin >> n;
  int gcd = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        if (!mp[{i, {j, k}}]) {
          mp[{i, {j, k}}] = 1;
          // mp[{j,{i,k}}]=1;
          // mp[{k,{i,j}}]=1;
          // cout<<i<<' '<<j<<' '<<k<<endl;
          gcd += __gcd(i, __gcd(j, k));
        }
      }
    }
  }
  cout << gcd << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define INF 1000000009
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
#define REP(i, a, b) for (int i = a; i <= b; i++)

int main(int argc, char const *argv[]) {
  map<pair<int, pair<int, int>>, int> mp;
  int n;
  cin >> n;
  int gcd = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {

        gcd += __gcd(i, __gcd(j, k));
      }
    }
  }
  cout << gcd << endl;
  return 0;
}
Problem p02713 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <map>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int euclid(int i, int j) {
  if (i % j == 0)
    return j;
  euclid(j, i % j);
}

int main() {
  int K;
  cin >> K;
  long long sum = 0;

  for (int a = 1; a <= K; a++) {
    for (int b = 1; b <= K; b++) {
      for (int c = 1; c <= K; c++) {
        int tmp = euclid(max(a, b), min(a, b));
        sum += euclid(max(tmp, c), min(tmp, c));
      }
    }
  }

  cout << sum << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <map>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int euclid(int i, int j) {
  if (i % j == 0)
    return j;
  return euclid(j, i % j);
}

int main() {
  int K;
  cin >> K;
  long long sum = 0;

  for (int a = 1; a <= K; a++) {
    for (int b = 1; b <= K; b++) {
      for (int c = 1; c <= K; c++) {
        int tmp = euclid(max(a, b), min(a, b));
        sum += euclid(max(tmp, c), min(tmp, c));
      }
    }
  }

  cout << sum << endl;

  return 0;
}
Problem p02713 (C++)

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

int gcd(int x, int y) {
  if (x % y == 0)
    return y;
  else
    return gcd(y, x % y);
}
int gcd2(int x, int y, int z) { return gcd(x, gcd(y, z)); }

int main() {
  int n;
  cin >> n;
  long long ans = 0;
  for (int i; i < n; i++)
    for (int j; j < n; j++)
      for (int k; k < n; k++)
        ans += gcd2(i, j, k);
  cout << ans << endl;
}

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

int gcd(int x, int y) {
  if (x % y == 0)
    return y;
  else
    return gcd(y, x % y);
}
int gcd2(int x, int y, int z) { return gcd(x, gcd(y, z)); }

int main() {
  int n;
  cin >> n;
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      for (int k = 1; k <= n; k++)
        ans += gcd2(i, j, k);
  cout << ans << endl;
}
Problem p02713 (C++)

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

ll gcd(ll a, ll b, ll c) {
  ll i = 0;
  ll n = 0;
  n = min({a, b, c});
  rep(i, n) {
    if (a % (n - i) == 0 && b % (n - i) == 0 && c % (n - i) == 0) {
      return n - i;
    }
  }
  return 1;
}
int main() {
  ll n, i, j, k;
  cin >> n;
  ll s1 = 0;
  rep(i, n) rep(j, n) rep(k, n) s1 += gcd(i + 1, j + 1, k + 1);
  cout << s1;

  return 0;
}

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

ll gcd(ll a, ll b, ll c) {
  ll i = 0;
  ll n = 0;
  n = min({a, b, c});
  rep(i, n) {
    if (a % (n - i) == 0 && b % (n - i) == 0 && c % (n - i) == 0) {
      return n - i;
    }
  }
  return 1;
}
int main() {
  ll n, i, j, k;
  cin >> n;
  ll s1 = 0;
  rep(i, n) rep(j, n) rep(k, n) {
    if (i == j && j == k)
      s1 += i + 1;
    if (i > j && j > k)
      s1 += 6 * gcd(i + 1, j + 1, k + 1);
    if (i > j && j == k)
      s1 += 3 * gcd(i + 1, j + 1, k + 1);
    if (i == j && j > k)
      s1 += 3 * gcd(i + 1, j + 1, k + 1);
  }
  cout << s1;

  return 0;
}
Problem p02713 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using vi = vector<int>;
using vii = vector<vi>;
using vpi = vector<pii>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpl = vector<pll>;
using vs = vector<string>;

int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else {
    gcd(b, a % b);
  }
}

int main() {
  int n;
  cin >> n;
  ll sun = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      for (int k = 1; k <= n; k++) {
        sun += gcd(gcd(i, j), k);
      }
  cout << sun << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using vi = vector<int>;
using vii = vector<vi>;
using vpi = vector<pii>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpl = vector<pll>;
using vs = vector<string>;

int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}

int main() {
  int n;
  cin >> n;
  ll sun = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      for (int k = 1; k <= n; k++) {
        sun += gcd(gcd(i, j), k);
      }
  cout << sun << endl;
  return 0;
}
Problem p02713 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

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;
}

using namespace std;

#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e9 + 7;

int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

int main() {
  int k;
  cin >> k;
  ll sum = 0;
  for (int a = 1; a <= k; a++) {
    for (int b = 1; b <= k; k++) {
      // int g = gcd(a,b);
      for (int c = 1; c <= k; c++) {
        sum += gcd(gcd(a, b), c);
      }
    }
  }
  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

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;
}

using namespace std;

#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e9 + 7;

int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}

int main() {
  int k;
  cin >> k;
  ll sum = 0;
  for (int a = 1; a <= k; a++) {
    for (int b = 1; b <= k; b++) {
      // int g = gcd(a,b);
      for (int c = 1; c <= k; c++) {
        sum += gcd(gcd(a, b), c);
      }
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02713 (C++)

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

#define rep(i, N) for (int i = 0; i < N; i++)
#define int long long

#define INF 1e10

using namespace std;

int gcd(int p, int q) {
  if (p % q == 0)
    return q;
  return gcd(q, p % q);
}

signed main(void) {
  int K;
  int ans = 0;

  cin >> K;

  rep(a, K) {
    rep(b, K) {
      rep(c, K) { ans += gcd(gcd(a, b), c); }
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"

#define rep(i, N) for (int i = 0; i < N; i++)
#define int long long

#define INF 1e10

using namespace std;

int gcd(int p, int q) {
  if (p % q == 0)
    return q;
  return gcd(q, p % q);
}

signed main(void) {
  int K;
  int ans = 0;

  cin >> K;

  rep(a, K) {
    rep(b, K) {
      rep(c, K) { ans += gcd(gcd(a + 1, b + 1), c + 1); }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02713 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int gcd(int x, int y) {
  for (int i = max(x, y); i > 0; i--) {
    if (x % i == 0 && y % i == 0)
      return i;
  }
}

int main() {
  int K;
  cin >> K;
  int A[K][K];
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      A[i][j] = gcd(i + 1, j + 1);
    }
  }
  int B[K][K][K];
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      for (int k = 0; k < K; k++) {
        B[i][j][k] = gcd(A[i][j], k + 1);
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      for (int k = 0; k < K; k++) {
        sum += B[i][j][k];
      }
    }
  }
  cout << sum << endl;
}


Fixed submission:
#include <iostream>
using namespace std;

int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }

int main() {
  int K;
  cin >> K;
  int A[K][K];
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      A[i][j] = gcd(i + 1, j + 1);
    }
  }
  int B[K][K][K];
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      for (int k = 0; k < K; k++) {
        B[i][j][k] = gcd(A[i][j], k + 1);
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      for (int k = 0; k < K; k++) {
        sum += B[i][j][k];
      }
    }
  }
  cout << sum << endl;
}
Problem p02713 (C++)

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

using namespace std;
typedef long long ll;

int cal_gcd(int a, int b);
int main() {
  int k;
  ll sum = 0;
  cin >> k;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int l = 1; l <= k; l++) {
        sum += cal_gcd(i, cal_gcd(j, l));
      }
    }
  }
  cout << sum << endl;
  return 0;
}

int cal_gcd(int a, int b) {
  int buf = 0;
  if (a < b) {
    buf = b;
    b = a;
    a = buf;
  }
  if (a % b == 0)
    return b;
  cal_gcd(a % b, b);
}


Fixed submission:
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

int cal_gcd(int a, int b);
int main() {
  int k;
  ll sum = 0;
  cin >> k;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int l = 1; l <= k; l++) {
        sum += cal_gcd(i, cal_gcd(j, l));
      }
    }
  }
  cout << sum << endl;
  return 0;
}

int cal_gcd(int a, int b) {
  int buf = 0;
  if (a < b) {
    buf = b;
    b = a;
    a = buf;
  }
  if (a % b == 0)
    return b;
  return cal_gcd(a % b, b);
}
Problem p02713 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint>       // int64_t, int*_t
#include <cstdio>        // printf
#include <deque>         // deque
#include <iostream>      // cout, endl, cin
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP1(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

int gcd(int a, int b, int c) {
  int ma, ret;
  ma = max(a, max(b, c));
  for (int i = ma; i > 0; i--) {
    if (a % i == 0 && b % i == 0 && c % i == 0) {
      ret = i;
      break;
    }
  }
  return ret;
}

int main(void) {
  int K, i, j, k, g;
  long long int sum = 0;
  cin >> K;
  REP1(i, 1, K) {
    REP1(j, 1, K) {
      REP1(k, 1, K) {
        g = gcd(i, j, k);
        sum += g;
      }
    }
  }
  cout << sum << endl;
}

Fixed submission:
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint>       // int64_t, int*_t
#include <cstdio>        // printf
#include <deque>         // deque
#include <iostream>      // cout, endl, cin
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP1(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

int gcd(int a, int b, int c) {
  int ma, ret;
  ma = min(a, min(b, c));
  for (int i = ma; i > 0; i--) {
    if (a % i == 0 && b % i == 0 && c % i == 0) {
      ret = i;
      break;
    }
  }
  return ret;
}

int main(void) {
  int K, i, j, k, g;
  long long int sum = 0;
  cin >> K;
  REP1(i, 1, K) {
    REP1(j, 1, K) {
      REP1(k, 1, K) {
        g = gcd(i, j, k);
        sum += g;
      }
    }
  }
  cout << sum << endl;
}
Problem p02713 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define PI 3.14159265358979323846264338327950
#define INF 1e18

int gcd(int x, int y) {
  if (x % y == 0) {
    return y;
  }
  int k = x % y;
  gcd(y, k);
}
int main() {
  int k;
  cin >> k;
  ll ans = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int s = 1; s <= k; s++) {
        int f = gcd(i, j);
        int t = gcd(s, f);
        ans += t;
      }
    }
  }
  cout << ans << endl;
}

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

int gcd(int x, int y) {
  if (x % y == 0) {
    return y;
  }
  int k = x % y;
  return gcd(y, k);
}
int main() {
  int k;
  cin >> k;
  ll ans = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int s = 1; s <= k; s++) {
        int f = gcd(i, j);
        int t = gcd(s, f);
        ans += t;
      }
    }
  }
  cout << ans << endl;
}
Problem p02713 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (a < b) {
    int r = b;
    b = a;
    a = r;
  }
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
int gcd2(int a, int b, int c) { return gcd(a, gcd(b, c)); }
int main() {
  long int k, ans = 0;
  cin >> k;
  for (int a = 0; a < k; a++) {
    for (int b = 0; b < k; b++) {
      for (int c = 0; c < k; c++) {
        ans += gcd2(a, b, c);
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (a < b) {
    int r = b;
    b = a;
    a = r;
  }
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
int gcd2(int a, int b, int c) { return gcd(a, gcd(b, c)); }
int main() {
  long int k, ans = 0;
  cin >> k;
  for (int a = 1; a < k + 1; a++) {
    for (int b = 1; b < k + 1; b++) {
      for (int c = 1; c < k + 1; c++) {
        ans += gcd2(a, b, c);
      }
    }
  }
  cout << ans << endl;
}
Problem p02713 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll a, b, c, i, j, k, l, m, n, ans, tmp, hoge, fuga, piyo;
ll x[10010], y[10010];
string s, str;
int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; i++) {
      m = gcd(i, j);
      for (int k = 1; k <= n; i++) {
        ans += gcd(m, k);
      }
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll a, b, c, i, j, k, l, m, n, ans, tmp, hoge, fuga, piyo;
ll x[10010], y[10010];
string s, str;
int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        ans += gcd(i, gcd(j, k));
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p02713 (C++)

Buggy submission (status: Time Limit Exceeded):
/*

Coded and Decoded by : Yash Kapoor

*/
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define mp make_pair
#define pll pair<ll, ll>
#define vl vector<ll>
#define sl set<ll>
#define vll vector<pll>
#define ml map<ll, ll>
#define mll map<pll, ll>
#define all(a) a.begin(), a.end()
#define x first
#define y second
#define sz(x) (ll) x.size()
#define dl '\n'
// #define why         (ll)1000000007
#define why (ll)998244353
#define lp(i, a, b) for (ll i = a; i < b; ++i)
#define lpr(i, a, b) for (ll i = a; i >= b; i--)
#define lpd(i, x) for (auto i : x)
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
ll inf = 1e18;
ld pi = 3.141592653589;
ll mod = why;
ll fast_power(ll base, ll power, ll mod) {
  ll result = 1;
  while (power) {
    if (power % 2)
      result = (result * base) % mod;
    base = (base * base) % mod;
    power /= 2;
  }
  return result;
}

ll inverse(ll base, ll mod) { return fast_power(base, mod - 2, mod); }

ll solve() {
  ll k, sum = 0;
  cin >> k;
  lp(i, 1, k + 1) {
    lp(j, 1, k + 1) {
      lp(l, 1, k + 1) { sum += __gcd(i, __gcd(j, l)); }
    }
  }
  cout << sum;
}

int main() {
  ios ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}

Fixed submission:
/*

Coded and Decoded by : Yash Kapoor

*/
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define mp make_pair
#define pll pair<ll, ll>
#define vl vector<ll>
#define sl set<ll>
#define vll vector<pll>
#define ml map<ll, ll>
#define mll map<pll, ll>
#define all(a) a.begin(), a.end()
#define x first
#define y second
#define sz(x) (ll) x.size()
#define dl '\n'
// #define why         (ll)1000000007
#define why (ll)998244353
#define lp(i, a, b) for (ll i = a; i < b; ++i)
#define lpr(i, a, b) for (ll i = a; i >= b; i--)
#define lpd(i, x) for (auto i : x)
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
ll inf = 1e18;
ld pi = 3.141592653589;
ll mod = why;
ll fast_power(ll base, ll power, ll mod) {
  ll result = 1;
  while (power) {
    if (power % 2)
      result = (result * base) % mod;
    base = (base * base) % mod;
    power /= 2;
  }
  return result;
}

ll inverse(ll base, ll mod) { return fast_power(base, mod - 2, mod); }

ll solve() {
  ll k, sum = 0;
  cin >> k;
  lp(i, 1, k + 1) {
    lp(j, 1, k + 1) {
      lp(l, 1, k + 1) { sum += __gcd(i, __gcd(j, l)); }
    }
  }
  cout << sum;
  return 0;
}

int main() {
  ios ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, a[4001];
char s[4001];
int s1[4001], s2[4001], s3[4001];
long long ans;
int main() {
  scanf("%d", &n);
  cin >> s;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'R')
      a[i + 1] = 1;
    else if (s[i] == 'G')
      a[i + 1] = 2;
    else
      a[i + 1] = 3;
  }
  for (int i = 1; i <= n; ++i) {
    if (a[i] == 1) {
      s1[i] = s1[i - 1] + 1;
      s2[i] = s2[i - 1], s3[i] = s3[i - 1];
    } else if (a[i] == 2) {
      s2[i] = s2[i - 1] + 1;
      s1[i] = s1[i - 1], s3[i] = s3[i - 1];
    } else {
      s1[i] = s1[i - 1];
      s2[i] = s2[i - 1];
      s3[i] = s3[i - 1] + 1;
    }
  }
  for (int i = 1; i <= n - 2; ++i) {
    for (int j = i + 1; j < n; ++j) {
      int N = j - i;
      if (a[j] == a[i])
        continue;
      if (a[i] + a[j] == 3)
        ans += s3[n] - s3[j] - (a[j + N] == 3);
      else if (a[i] + a[j] == 4)
        ans += s2[n] - s2[j] - (a[j + N] == 2);
      else if (a[i] + a[j] == 5)
        ans += s1[n] - s1[j] - (a[j + N] == 1);
    }
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, a[40001];
char s[40001];
int s1[40001], s2[40010], s3[40001];
long long ans;
int main() {
  scanf("%d", &n);
  cin >> s;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'R')
      a[i + 1] = 1;
    else if (s[i] == 'G')
      a[i + 1] = 2;
    else
      a[i + 1] = 3;
  }
  for (int i = 1; i <= n; ++i) {
    if (a[i] == 1) {
      s1[i] = s1[i - 1] + 1;
      s2[i] = s2[i - 1], s3[i] = s3[i - 1];
    } else if (a[i] == 2) {
      s2[i] = s2[i - 1] + 1;
      s1[i] = s1[i - 1], s3[i] = s3[i - 1];
    } else {
      s1[i] = s1[i - 1];
      s2[i] = s2[i - 1];
      s3[i] = s3[i - 1] + 1;
    }
  }
  for (int i = 1; i <= n - 2; ++i) {
    for (int j = i + 1; j < n; ++j) {
      int N = j - i;
      if (a[j] == a[i])
        continue;
      if (a[i] + a[j] == 3)
        ans += s3[n] - s3[j] - (a[j + N] == 3);
      else if (a[i] + a[j] == 4)
        ans += s2[n] - s2[j] - (a[j + N] == 2);
      else if (a[i] + a[j] == 5)
        ans += s1[n] - s1[j] - (a[j + N] == 1);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri, x, n) for (int ri = (int)(n - 1); ri >= (int)(x); ri--)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int fc(const vector<int> &v, int i, int j) {
  int res = v.size() - (lower_bound(ALL(v), j) - v.begin());
  int t = j - i;
  t += j;
  if (*lower_bound(ALL(v), t) == t)
    res--;
  return res;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<vector<int>> v(3, vector<int>());
  rep(i, s.size()) {
    if (s.at(i) == 'R')
      s.at(i) = 0;
    if (s.at(i) == 'G')
      s.at(i) = 1;
    if (s.at(i) == 'B')
      s.at(i) = 2;
  }
  ll ans = 0;
  rep(i, n) v.at(s.at(i)).push_back(i);
  rep(i, n) rep2(j, i + 1, n) {
    if (s.at(i) == s.at(j))
      continue;
    rep(l, 3) {
      if (s.at(i) != l && s.at(j) != l)
        ans += fc(v.at(l), i, j);
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri, x, n) for (int ri = (int)(n - 1); ri >= (int)(x); ri--)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int fc(const vector<int> &v, int i, int j) {
  int res = v.size() - (lower_bound(ALL(v), j) - v.begin());
  int t = j - i;
  t += j;
  auto itr = lower_bound(ALL(v), t);
  if (itr != v.end() && *itr == t)
    res--;
  return res;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<vector<int>> v(3, vector<int>());
  rep(i, s.size()) {
    if (s.at(i) == 'R')
      s.at(i) = 0;
    if (s.at(i) == 'G')
      s.at(i) = 1;
    if (s.at(i) == 'B')
      s.at(i) = 2;
  }
  ll ans = 0;
  rep(i, n) v.at(s.at(i)).push_back(i);
  rep(i, n) rep2(j, i + 1, n) {
    if (s.at(i) == s.at(j))
      continue;
    rep(l, 3) {
      if (s.at(i) != l && s.at(j) != l)
        ans += fc(v.at(l), i, j);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
using namespace std;

int main() {
  int N;
  string S;
  cin >> N;
  cin >> S;
  int sum = 0;
  for (int i = 1; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      if (S[i - 1] != S[j - 1]) {
        for (int k = j + 1; k <= N; k++) {
          if (S[k - 1] != S[i - 1] && S[k - 1] != S[j - 1]) {
            if ((j - i) != (k - j)) {
              sum++;
            }
          }
        }
      }
    }
  }
  cout << sum << endl;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;

int main() {
  int N;
  string S;
  cin >> N >> S;
  int count_r = 0;
  int count_g = 0;
  int count_b = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == 'R') {
      count_r++;
    }
    if (S[i] == 'G') {
      count_g++;
    }
    if (S[i] == 'B') {
      count_b++;
    }
  }
  int64_t sum = count_r * count_g * count_b;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j + (j - i) < N; j++) {
      int k = j + (j - i);
      if (S[i] != S[j] && S[j] != S[k] && S[k] != S[i]) {
        sum--;
      }
    }
  }
  cout << sum << endl;
}
Problem p02714 (C++)

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

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define srt(i) sort(i.begin(), i.end())
#define rvt(i) sort(i.begin(), i.end(), greater<int>())

int main() {
  int n;
  string s;
  cin >> n >> s;

  int ans = 0;
  for (int i = 1; i <= n - 2; i++)
    for (int j = i + 1; j <= n - 1; j++)
      for (int k = j + 1; k <= n; k++) {
        if (j - i == k - j)
          continue;
        if (s.at(i - 1) != s.at(j - 1) && s.at(i - 1) != s.at(k - 1) &&
            s.at(j - 1) != s.at(k - 1))
          ans++;
      }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <limits>
#include <numeric>
#include <type_traits>

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define srt(i) sort(i.begin(), i.end())
#define rvt(i) sort(i.begin(), i.end(), greater<int>())

int main() {
  int n;
  string s;
  cin >> n >> s;

  int64_t r, g, b;
  r = 0;
  g = 0;
  b = 0;
  rep(i, n) {
    if (s.at(i) == 'R')
      r++;
    if (s.at(i) == 'G')
      g++;
    if (s.at(i) == 'B')
      b++;
  }

  int64_t ans = r * g * b;

  for (int i = 0; i < n; i++) {
    for (int t = 1; t <= n; t++) {
      if (i + 2 * t >= n)
        continue;

      char first = s.at(i);
      char second = s.at(i + t);
      char third = s.at(i + 2 * t);

      if (first != second && first != third && second != third)
        ans--;
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <numeric>
#include <stdlib.h>
#include <type_traits>
using namespace std;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int c = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      for (int k = j + 1; k <= n - 1; k++) {
        if (s[i] != s[j] && s[i] != s[k] && s[j] != s[k] &&
            (j - i) != (k - j)) {
          c++;
        }
      }
    }
  }
  cout << c;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <numeric>
#include <stdlib.h>
#include <type_traits>
using namespace std;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  unsigned long long int c = 0;
  long long int cr = 0;
  long long int cg = 0;
  long long int cb = 0;
  for (int i = 0; i < n; i++) {
    switch (s[i]) {
    case 'R':
      cr++;
      break;
    case 'G':
      cg++;
      break;
    case 'B':
      cb++;
      break;
    }
  }
  c = cr * cg * cb;
  for (int j = 0; j < n - 2; j++) {
    for (int k = j + 1; k < n - 1; k++) {
      if (s[j] != s[k] && s[k] != s[2 * k - j] && s[j] != s[2 * k - j] &&
          (2 * k - j) < n) {
        c--;
      }
    }
  }
  cout << c;
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 2e9
#define rep(i, n, m) for (int i = n; i < m; i++)

using namespace std;

int main() {
  int n;
  string s;
  vector<int> R, G, B;
  cin >> n >> s;
  rep(i, 0, n) {
    switch (s[i]) {
    case 'R':
      R.push_back(i + 1);
      break;
    case 'G':
      G.push_back(i + 1);
      break;
    case 'B':
      B.push_back(i + 1);
      break;
    }
  }

  int ans = 0;
  for (int i : R)
    for (int j : G)
      for (int k : B) {
        vector<int> a = {i, j, k};
        sort(a.begin(), a.end());
        if (2 * a[1] != a[0] + a[2])
          ans++;
      }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 2e9
#define rep(i, n, m) for (int i = n; i < m; i++)

using namespace std;

int main() {
  int n;
  string s;
  vector<int> R, G, B;
  cin >> n >> s;
  rep(i, 0, n) {
    switch (s[i]) {
    case 'R':
      R.push_back(i + 1);
      break;
    case 'G':
      G.push_back(i + 1);
      break;
    case 'B':
      B.push_back(i + 1);
      break;
    }
  }

  long long int ans;
  ans = R.size() * G.size() * B.size();
  rep(i, 0, n) rep(j, i, n) {
    int k = 2 * j - i;
    if (j <= k && k < n)
      if (s[i] != s[j] && s[k] != s[j] && s[i] != s[k])
        ans--;
  }
  cout << ans << endl;
  return 0;
}
Problem p02714 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if S[j] != S[i]:
            for k in range(j + 1, N):
                if j - i != k - j and S[k] != S[i] and S[k] != S[j]:
                    ans += 1
print(ans)


Fixed submission:
def main():
    N = int(input())
    S = input()
    ans = S.count("R") * S.count("G") * S.count("B")
    for i in range(N):
        for d in range(N - 1):
            j = i + d
            k = j + d
            if k < N and S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                ans -= 1
    print(ans)


main()
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxx = 4e3 + 7;
int n;
char s[maxx];
int num[maxx];
vector<int> vec[maxx];
LL cnt[maxx];
bool vis[maxx];

LL chk(int a, int b, int c) {
  LL res = 0;
  for (int i = 0; i < vec[a].size(); i++) {
    for (int j = 0; j < vec[b].size(); j++) {
      int id_a = vec[a][i];
      int id_b = vec[b][j];
      if (id_b > id_a) {
        int id_nxt = id_b + (id_b - id_a);
        if (num[id_nxt] == c)
          res++;
      }
    }
  }
  return res;
}

int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'B')
      num[i] = 1;
    else if (s[i] == 'G')
      num[i] = 2;
    else if (s[i] == 'R')
      num[i] = 3;
  }
  for (int i = 1; i <= n; i++) {
    vec[num[i]].push_back(i);
    cnt[num[i]]++;
  }
  LL ans = cnt[1] * cnt[2] * cnt[3];
  ans -= chk(1, 2, 3);
  ans -= chk(1, 3, 2);
  ans -= chk(2, 1, 3);
  ans -= chk(2, 3, 1);
  ans -= chk(3, 1, 2);
  ans -= chk(3, 2, 1);
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxx = 4e3 + 7;
int n;
char s[maxx];
int num[maxx];
vector<int> vec[maxx];
LL cnt[maxx];
bool vis[maxx];

LL chk(int a, int b, int c) {
  LL res = 0;
  for (int i = 0; i < vec[a].size(); i++) {
    for (int j = 1; vec[a][i] + 2 * j <= n; j++) {
      if (num[vec[a][i] + j] == b && num[vec[a][i] + 2 * j] == c)
        res++;
    }
  }
  return res;
}

int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'B')
      num[i] = 1;
    else if (s[i] == 'G')
      num[i] = 2;
    else if (s[i] == 'R')
      num[i] = 3;
  }
  for (int i = 1; i <= n; i++) {
    vec[num[i]].push_back(i);
    cnt[num[i]]++;
  }
  LL ans = cnt[1] * cnt[2] * cnt[3];
  ans -= chk(1, 2, 3);
  ans -= chk(1, 3, 2);
  ans -= chk(2, 1, 3);
  ans -= chk(2, 3, 1);
  ans -= chk(3, 1, 2);
  ans -= chk(3, 2, 1);
  printf("%lld\n", ans);
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-indexed昇順

int main() {
  int N;
  string S;
  cin >> N >> S;
  vector<int> R;
  vector<int> G;
  vector<int> B;
  rep(i, N) {
    switch (S.at(i)) {
    case 'R':
      R.push_back(i);
      break;
    case 'G':
      G.push_back(i);
      break;
    case 'B':
      B.push_back(i);
      break;
    }
  }

  ll sum = 0;
  rep(i, R.size()) {
    rep(j, G.size()) {
      rep(k, B.size()) {
        int r = R.at(i);
        int g = G.at(j);
        int b = B.at(k);
        if (r > g)
          swap(r, g);
        if (g > b)
          swap(g, b);
        if (r > g)
          swap(r, g);
        if (g - r == b - g)
          continue;
        sum++;
      }
    }
  }
  cout << sum << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-indexed昇順

int main() {
  int N;
  string S;
  cin >> N >> S;
  vector<int> R;
  vector<int> G;
  vector<int> B;
  rep(i, N) {
    switch (S.at(i)) {
    case 'R':
      R.push_back(i);
      break;
    case 'G':
      G.push_back(i);
      break;
    case 'B':
      B.push_back(i);
      break;
    }
  }

  ll sum = 0;
  rep(i, R.size()) {
    rep(j, G.size()) {
      int r = R.at(i);
      int b = G.at(j);
      if (b < r)
        swap(b, r);
      int d = b - r;
      int e = B.size();
      if (find(B.begin(), B.end(), b + d) != B.end())
        e--;
      if (find(B.begin(), B.end(), r - d) != B.end())
        e--;
      if ((b + r) % 2 == 0)
        if (find(B.begin(), B.end(), (b + r) / 2) != B.end())
          e--;
      sum += e;
    }
  }
  cout << sum << endl;
}