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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rep2(i, n) for (int i = 0; i <= n; i++)
#define repr(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define P pair<long long, long long>
#define double long double
#define INF 1e10
#define MOD 1e9 + 7
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;
int fg(int k) {
  int sum = 1;
  for (int i = 1; i <= k; ++i) {
    sum *= i;
  }
  return sum;
}
signed main() {
  int a;
  string b;
  cin >> a >> b;
  int x = 0;
  int y = 0;
  int z = 0;
  rep(i, a) {
    if (b.at(i) == 'R') {
      x++;
    }
    if (b.at(i) == 'G') {
      y++;
    }
    if (b.at(i) == 'B') {
      z++;
    }
  }
  int sum = x * y * z;
  rep1(j, (a - 3) / 2) {
    for (int k = 0; k <= a - j * 2; k++) {
      if (b.at(k) != b.at(k + j) && b.at(k) != b.at(k + j * 2) &&
          b.at(k + j) != b.at(k + j * 2)) {
        sum--;
      }
    }
  }
  cout << sum;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rep2(i, n) for (int i = 0; i <= n; i++)
#define repr(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define P pair<long long, long long>
#define double long double
#define INF 1e10
#define MOD 1e9 + 7
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;
int fg(int k) {
  int sum = 1;
  for (int i = 1; i <= k; ++i) {
    sum *= i;
  }
  return sum;
}
signed main() {
  int a;
  string b;
  cin >> a >> b;
  int x = 0;
  int y = 0;
  int z = 0;
  rep(i, a) {
    if (b.at(i) == 'R') {
      x++;
    }
    if (b.at(i) == 'G') {
      y++;
    }
    if (b.at(i) == 'B') {
      z++;
    }
  }
  int sum = x * y * z;
  rep1(j, (a - 3) / 2 + 1) {
    for (int k = 0; k < a - j * 2; k++) {
      if (b.at(k) != b.at(k + j) && b.at(k) != b.at(k + j * 2) &&
          b.at(k + j) != b.at(k + j * 2)) {
        sum--;
      }
    }
  }
  cout << sum;
}
Problem p02714 (C++)

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

using namespace std;
using ll = long long;
using vec = vector<int>;

#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define rrep(i, n) for (int(i) = (n); (i) >= 0; --(i))
#define Sort(a) sort(a.begin(), a.end())
#define gSort(a) sort(a.begin(), a.end(), greater<int>())
#define pout(n, a) cout << fixed << setprecision(n) << (a)
#define Cast(dig, n) static_cast<std::bitset<(dig)>>(n)

const int INF = 2147483647;
const ll MOD = 1000000007;

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

  ll ans = 0;
  for (int i = 0; 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[i] != s[j]) && (s[i] != s[k]) && (s[j] != s[k]))
          ++ans;
      }
    }
  }

  cout << ans;
}

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

using namespace std;
using ll = long long;
using vec = vector<int>;

#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define rrep(i, n) for (int(i) = (n); (i) >= 0; --(i))
#define Sort(a) sort(a.begin(), a.end())
#define gSort(a) sort(a.begin(), a.end(), greater<int>())
#define pout(n, a) cout << fixed << setprecision(n) << (a)
#define Cast(dig, n) static_cast<std::bitset<(dig)>>(n)

const int INF = 2147483647;
const ll MOD = 1000000007;

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

  ll r = 0, g = 0, b = 0;
  for (auto i : s) {
    if (i == 'R')
      r++;
    else if (i == 'G')
      g++;
    else
      b++;
  }

  // 全パターンを計上
  ll ans = r * g * b;

  for (int i = 0; i < n; ++i) {
    // 同間隔で条件を満たすなら除く
    for (int d = 0; d < n; ++d) {
      int j = i + d;
      int k = j + d;

      if (k >= n)
        break;

      if ((s[i] != s[j]) && (s[j] != s[k]) && (s[i] != s[k]))
        --ans;
    }
  }

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

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> R, B, G;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R') {
      R.push_back(i + 1);
    } else if (s[i] == 'B') {
      B.push_back(i + 1);
    } else {
      G.push_back(i + 1);
    }
  }

  int cnt = 0;
  for (int i = 0; i < R.size(); i++) {
    for (int k = 0; k < B.size(); k++) {
      for (int t = 0; t < G.size(); t++) {
        int I, J, K;
        I = min({R[i], B[k], G[t]});
        K = max({R[i], B[k], G[t]});
        J = R[i] + B[k] + G[t] - (I + K);
        if (J - I != K - J) {
          cnt++;
        }
      }
    }
  }
  cout << cnt << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> R, B, G;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R') {
      R.push_back(i + 1);
    } else if (s[i] == 'B') {
      B.push_back(i + 1);
    } else {
      G.push_back(i + 1);
    }
  }
  long long cnt = R.size() * B.size() * G.size();
  ;
  for (int i = 0; i < n; i++) {
    for (int k = 1; k < n; k++) {
      int I, J, K;
      long long a = i;
      long long b = i + k;
      long long c = b + k;
      if (c < n) {
        if (s[a] != s[b] && s[a] != s[c] && s[b] != s[c]) {
          cnt--;
        }
      }
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  long long R = 0;
  long long G = 0;
  long long B = 0;
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    if (S.at(i) == 'R') {
      R = R + 1;
    } else if (S.at(i) == 'G') {
      G = G + 1;
    } else {
      B = B + 1;
    }
  }
  sum = R * G * B;
  long long count = 0;
  if (N < 3) {
    cout << 0 << endl;
  } else {
    for (int i = 0; i < N - 2; i++) {
      for (int j = i + 1; j < N - 1; j++) {
        int m = 2 * j - i;
        if ((S.at(i) != S.at(j)) && (S.at(i) != S.at(m)) &&
            (S.at(m) != S.at(j))) {
          count = count + 1;
        }
      }
    }
    cout << sum - count << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  long long R = 0;
  long long G = 0;
  long long B = 0;
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    if (S.at(i) == 'R') {
      R = R + 1;
    } else if (S.at(i) == 'G') {
      G = G + 1;
    } else {
      B = B + 1;
    }
  }
  sum = R * G * B;
  long long count = 0;
  if (N < 3) {
    cout << 0 << endl;
  } else {
    for (int i = 0; i < N - 2; i++) {
      for (int j = i + 1; j < N - 1; j++) {
        int m = 2 * j - i;
        if (m < N) {
          if ((S.at(i) != S.at(j)) && (S.at(i) != S.at(m)) &&
              (S.at(m) != S.at(j))) {
            count = count + 1;
          }
        }
      }
    }
    cout << sum - count << endl;
  }
}
Problem p02714 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    N = int(input())
    S = input()
    cnt = 0
    for i in range(N):
        for j in range(i + 1, N):
            for k in range(j + 1, N):
                if j - i == k - j:
                    if S[j] != S[i] and S[i] != S[k] and S[k] != S[j]:
                        cnt += 1
    print(S.count("R") * S.count("B") * S.count("G") - cnt)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    N = int(input())
    S = input()
    cnt = 0
    for i in range(N):
        for j in range(i + 1, N):
            k = 2 * j - i
            if k >= N:
                continue
            if S[j] != S[i] and S[i] != S[k] and S[k] != S[j]:
                cnt += 1
    print(S.count("R") * S.count("B") * S.count("G") - cnt)


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

Buggy submission (status: Time Limit Exceeded):
n = int(input())
s = input()
ans = 0
for i in range(len(s)):
    one = s[i]
    for j in range(i + 1, len(s)):
        if s[j] == one:
            continue
        dis1 = j - i
        two = s[j]
        for k in range(j + 1, len(s)):
            if s[k] == one or s[k] == two:
                continue
            else:
                dis2 = k - j
                if dis1 != dis2:
                    ans += 1

print(ans)


Fixed submission:
n = int(input())
s = input()

ans = s.count("R") * s.count("G") * s.count("B")

for i in range(n):
    for j in range(i + 1, n):
        k = 2 * j - i
        # 三個目が範囲内 かつ 三文字違う かつ 距離が同じはダメ
        if (
            k < n
            and (s[i] != s[j] and s[j] != s[k] and s[i] != s[k])
            and j - i == k - j
        ):
            ans -= 1

print(ans)
Problem p02714 (C++)

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

int n;
string s;
vector<int> v[3];

int idx(int b, int a, int i) { // 在 v[b] 中寻找第一个不小于 v[a][i] 的下标
  return upper_bound(v[b].begin(), v[b].end(), v[a][i]) - v[b].begin();
}

ll cal(int a, int b, int c) {
  ll ret = 0;
  for (int i = 0; i < v[a].size(); i++) {
    for (int j = idx(b, a, i); j < v[b].size(); j++) {
      for (int k = idx(c, b, j); k < v[c].size(); k++) {
        if (v[b][j] - v[a][i] !=
            v[c][k] - v[b][j]) { // 等价于条件中的 j - i != k - j
          ++ret;
        }
      }
    }
  }
  return ret;
}

int main() {
  cin >> n >> s;
  for (int i = 0; i < n; i++) { // 存储不同字母的位置
    if (s[i] == 'R')
      v[0].push_back(i);
    else if (s[i] == 'G')
      v[1].push_back(i);
    else
      v[2].push_back(i);
  }
  ll ans = 0;
  for (int i = 0; i < 3; i++) { // 枚举RGB的前后情况
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        if (i != j && i != k && j != k) {
          ans += cal(i, j, k);
        }
      }
    }
  }
  cout << ans;
}


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

int n;
string s;
vector<int> v[3];

inline int idx(int b, int a,
               int i) { // 在 v[b] 中寻找第一个不小于 v[a][i] 的下标
  return upper_bound(v[b].begin(), v[b].end(), v[a][i]) - v[b].begin();
}

ll cal(int a, int b, int c) {
  ll ret = 0;
  for (int i = 0; i < v[a].size(); i++) {
    for (int j = idx(b, a, i); j < v[b].size(); j++) {
      for (int k = idx(c, b, j); k < v[c].size(); k++) {
        if (v[b][j] - v[a][i] !=
            v[c][k] - v[b][j]) { // 等价于条件中的 j - i != k - j
          ++ret;
        }
      }
    }
  }
  return ret;
}

int main() {
  cin >> n >> s;
  for (int i = 0; i < n; i++) { // 存储不同字母的位置
    if (s[i] == 'R')
      v[0].push_back(i);
    else if (s[i] == 'G')
      v[1].push_back(i);
    else
      v[2].push_back(i);
  }
  ll ans = 0;
  for (int i = 0; i < 3; i++) { // 枚举RGB的前后情况
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        if (i != j && i != k && j != k) {
          ans += cal(i, j, k);
        }
      }
    }
  }
  cout << ans;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  vector<int> RED;
  vector<int> GR;
  vector<int> BLUE;
  rep(i, S.size()) {
    if (S[i] == 'R') {
      RED.push_back(i);
    } else if (S[i] == 'G') {
      GR.push_back(i);
    } else {
      BLUE.push_back(i);
    }
  }
  long long res = 0;
  for (auto r : RED) {
    for (auto g : GR) {
      for (auto b : BLUE) {
        if (abs(r - g) == abs(g - b) || abs(r - b) == abs(b - g) ||
            abs(g - r) == abs(r - b))
          continue;
        res++;
      }
    }
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  vector<int> RED;
  vector<int> GR;
  vector<int> BLUE;
  rep(i, S.size()) {
    if (S[i] == 'R') {
      RED.push_back(i);
    } else if (S[i] == 'G') {
      GR.push_back(i);
    } else {
      BLUE.push_back(i);
    }
  }
  long long res = 0;
  // for(auto r : RED) {
  //     for(auto g : GR) {
  //         // for(auto b : BLUE) {
  //         //     if (abs(r-g) == abs(g-b) || abs(r-b) == abs(b-g) || abs(g-
  //         r) == abs(r-b)) continue;
  //         //     res++;
  //         // }
  //         int diff = abs(r-g);
  //     }
  // }
  res += RED.size() * BLUE.size() * GR.size();
  // cout << res << endl;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      if (j + j - i >= N)
        continue;
      if (S[i] != S[j] && S[i] != S[j + j - i] && S[j] != S[j + j - i]) {
        // cout << i << j;
        res--;
      }
    }
  }
  cout << res << endl;
  return 0;
}
Problem p02714 (Python)

Buggy submission (status: Runtime Error):
def main():
    from collections import Counter

    n, s = int(input()), input()
    c = Counter(s)
    d = list(c.values())

    ans = d[0] * d[1] * d[2]
    rgb = {"R", "G", "B"}
    for i in range(1, n // 2 + 1):
        for j in range(i):
            x = s[j::i]
            for a, b, c in zip(x, x[1:], x[2:]):
                g = {a, b, c}
                if g == rgb:
                    ans -= 1

    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    from collections import Counter

    n, s = int(input()), input()
    c = Counter(s)
    d = list(c.values())
    if len(d) < 3:
        print(0)
        exit()
    ans = d[0] * d[1] * d[2]
    rgb = {"R", "G", "B"}
    for i in range(1, n // 2 + 1):
        for j in range(i):
            x = s[j::i]
            for a, b, c in zip(x, x[1:], x[2:]):
                g = {a, b, c}
                if g == rgb:
                    ans -= 1

    print(ans)


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

Buggy submission (status: Time Limit Exceeded):
n = int(input())
s = list(input())
tot = 0
R = [i for i, x in enumerate(s) if x == "R"]
G = [i for i, x in enumerate(s) if x == "G"]
B = [i for i, x in enumerate(s) if x == "B"]
tot += len(R) * len(G) * len(B)
for i in range(len(R)):
    for j in range(len(G)):
        for k in range(len(B)):
            if (
                2 * R[i] == G[j] + B[k]
                or 2 * G[j] == R[i] + B[k]
                or 2 * B[k] == G[j] + R[i]
            ):
                tot -= 1
print(tot)


Fixed submission:
n = int(input())
s = list(input())
tot = 0
tot += s.count("R") * s.count("G") * s.count("B")
for i in range(n):
    for j in range(i + 1, n):
        k = 2 * j - i
        if k < n and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            tot -= 1
print(tot)
Problem p02714 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
S = input()
ans = S.count("R") * S.count("G") * S.count("B")

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if S[i] != S[j]:
            x = 2 * j - i
            if x <= N - 1 and S[i] != S[x] and S[x] != S[j]:
                ans -= 1

print(ans)


Fixed submission:
N = int(input())
S = input()
ans = S.count("R") * S.count("G") * S.count("B")

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if S[i] != S[j]:
            x = 2 * j - i
            if x < N and S[i] != S[x] and S[x] != S[j]:
                ans -= 1

print(ans)
Problem p02714 (C++)

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

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


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

int main() {
  int n;
  cin >> n;
  string a;
  cin >> a;
  long long int r = 0, g = 0, b = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == 'R')
      r++;
    else if (a[i] == 'G')
      g++;
    else
      b++;
  }
  long long int ans = r * g * b;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int k = 2 * j - i;
      if (k >= n)
        continue;
      if (a[i] != a[j] && a[j] != a[k] && a[k] != a[i])
        ans--;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02714 (C++)

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

int main() {
  ios::sync_with_stdio(0);
  cout << setprecision(10) << fixed;
  int n;
  string s;
  cin >> n >> s;
  vector<int> a[3];
  map<char, int> mp = {{'R', 0}, {'G', 1}, {'B', 2}};
  for (int i = 0; i < n; ++i) {
    a[mp[s[i]]].push_back(i);
  }
  long long ans = 0;
  for (int i = 0; i < a[0].size(); ++i) {
    for (int j = 0; j < a[1].size(); ++j) {
      for (int k = 0; k < a[2].size(); ++k) {
        vector<int> b = {a[0][i], a[1][j], a[2][k]};
        sort(b.begin(), b.end());
        if (b[1] - b[0] == b[2] - b[1])
          continue;
        ans++;
      }
    }
  }
  cout << ans;
  return 0;
}


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

int main() {
  ios::sync_with_stdio(0);
  cout << setprecision(10) << fixed;
  int n;
  string s;
  cin >> n >> s;
  vector<int> a[3];
  map<char, int> mp = {{'R', 0}, {'G', 1}, {'B', 2}};
  for (int i = 0; i < n; ++i) {
    a[mp[s[i]]].push_back(i);
  }
  long long ans = 0;
  for (int i = 0; i < a[0].size(); ++i) {
    for (int j = 0; j < a[1].size(); ++j) {
      ans += (a[2].size());
      int x = max(a[0][i], a[1][j]);
      int y = min(a[0][i], a[1][j]);
      int diff = x - y;
      if (x + diff < n && s[x + diff] == 'B')
        ans--;
      if (y - diff >= 0 && s[y - diff] == 'B')
        ans--;
      if ((x + y) % 2 == 0 && s[(x + y) / 2] == 'B')
        ans--;
    }
  }
  cout << ans;
  return 0;
}
Problem p02714 (C++)

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

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

  vector<int> r, g, b;
  rep(i, n) {
    if (s[i] == 'R')
      r.emplace_back(i);
    else if (s[i] == 'G')
      g.emplace_back(i);
    else if (s[i] == 'B')
      b.emplace_back(i);
  }

  ll ans = (ll)r.size() * g.size() * b.size();
  for (int i : r)
    for (int j : g)
      for (int k : b) {
        int a = abs(i - j);
        int b = abs(j - k);
        int c = abs(k - i);
        if (a == b || b == c || c == a)
          ans--;
      }

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

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

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

  vector<int> r, g, b;
  rep(i, n) {
    if (s[i] == 'R')
      r.emplace_back(i);
    else if (s[i] == 'G')
      g.emplace_back(i);
    else if (s[i] == 'B')
      b.emplace_back(i);
  }

  ll ans = (ll)r.size() * g.size() * b.size();
  for (int i = 0; i < n - 2; i++)
    for (int j = i + 1; j < n - 1; j++) {
      if (j + j - i >= n)
        continue;
      if (s[i] == s[j] || s[i] == s[j + j - i] || s[j] == s[j + j - i])
        continue;
      else
        ans--;
    }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
using namespace std;
vector<int> r, g, b;
char s[4005];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      r.pb(i);
    else if (s[i] == 'G')
      g.pb(i);
    else
      b.pb(i);
  }
  int sr = r.size(), sg = g.size(), sb = b.size();
  ll ans = sr * sg * sb;
  for (int i = 0; i < sr; i++) {
    for (int j = 0; j < sg; j++) {
      int L = r[i], R = g[j];
      int d = R - L;
      if (d % 2 == 0 && s[L + d / 2] == 'B')
        ans--;
      if (L - d > 0 && s[L - d] == 'B')
        ans--;
      if (R + d < n && s[R + d] == 'B')
        ans--;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
using namespace std;
vector<int> r, g, b;
char s[4005];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      r.pb(i);
    else if (s[i] == 'G')
      g.pb(i);
    else
      b.pb(i);
  }
  int sr = r.size(), sg = g.size(), sb = b.size();
  ll ans = (ll)sr * sg * sb;
  for (int d = 1; d <= n; d++) {
    for (int a = 0; a + d + d < n; a++) {
      if (s[a] != s[a + d] && s[a] != s[a + d + d] && s[a + d] != s[a + d + d])
        ans--;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02714 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
typedef long long ll;
using namespace std;

int main() {
  int n;
  ll ans = 0;
  string s;
  cin >> n >> s;
  vector<int> r;
  vector<int> g;
  vector<int> b;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      r.push_back(i + 1);
    if (s[i] == 'G')
      g.push_back(i + 1);
    if (s[i] == 'B')
      b.push_back(i + 1);
  }
  for (int i = 0; i < r.size(); i++) {
    for (int j = 0; j < g.size(); j++) {
      for (int k = 0; k < b.size(); k++) {
        if (max(r[i], max(g[j], b[k])) -
                (r[i] + g[j] + b[k] - max(r[i], max(g[j], b[k])) -
                 min(r[i], min(g[j], b[k]))) !=
            -min(r[i], min(g[j], b[k])) +
                (r[i] + g[j] + b[k] - max(r[i], max(g[j], b[k])) -
                 min(r[i], min(g[j], b[k])))) {
          ans++;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
typedef long long ll;
using namespace std;

int main() {
  int n;
  ll ans = 0;
  string s;
  cin >> n >> s;
  vector<int> r;
  vector<int> g;
  vector<int> b;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      r.push_back(i + 1);
    if (s[i] == 'G')
      g.push_back(i + 1);
    if (s[i] == 'B')
      b.push_back(i + 1);
  }
  for (int i = 0; i < r.size(); i++) {
    for (int j = 0; j < g.size(); j++) {
      ans += b.size();
      int big = max(r[i], g[j]);
      int small = min(r[i], g[j]);
      int sa = big - small;
      if (binary_search(b.begin(), b.end(), big + sa)) {
        ans -= 1;
      }
      if (binary_search(b.begin(), b.end(), small - sa)) {
        ans -= 1;
      }
      if (sa % 2 == 0) {
        if (binary_search(b.begin(), b.end(), small + sa / 2)) {
          ans -= 1;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02714 (Python)

Buggy submission (status: Time Limit Exceeded):
#!/usr/bin/env python3
def main():
    N = int(input())
    S = input()

    ans = 0
    for i in range(N):
        for j in range(i, N):
            for k in range(j, N):
                if (
                    1
                    and S[i] != S[j]
                    and S[i] != S[k]
                    and S[j] != S[k]
                    and j - i != k - j
                ):
                    ans += 1
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3
def main():
    N = int(input())
    S = input()

    res = 0
    R, G, B = S.count("R"), S.count("G"), S.count("B")
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            k = j + j - i
            if k >= N:
                continue
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                res += 1
    print(R * G * B - res)


if __name__ == "__main__":
    main()
Problem p02715 (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 <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
const int MOD = 1000000007;
const int MAX_N = 500010;

ll modpow(ll a, ll n, ll mod) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = ret * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return ret;
}

int main() {
  ll n, k, ans = 0;
  cin >> n >> k;
  vector<ll> g(n + 2);
  for (ll m = k; m >= 1; m--) {
    ll kata = 1;
    while (m * (kata + 1) <= k)
      kata++;
    g[m] = modpow(kata, n, MOD) % MOD;
    for (ll i = 2; m * i <= k; i++) {
      g[m] -= g[m * i];
      g[m] %= MOD;
    }
  }
  for (int i = 1; i <= k; i++) {
    // cout<<i<<" "<<g[i]<<endl;
    ans += i * g[i];
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
const int MOD = 1000000007;
const int MAX_N = 500010;

ll modpow(ll a, ll n, ll mod) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = ret * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return ret;
}

int main() {
  ll n, k, ans = 0;
  cin >> n >> k;
  vector<ll> g(k + 2);
  for (ll m = k; m >= 1; m--) {
    ll kata = 1;
    while (m * (kata + 1) <= k)
      kata++;
    g[m] = modpow(kata, n, MOD) % MOD;
    for (ll i = 2; m * i <= k; i++) {
      g[m] -= g[m * i];
      g[m] %= MOD;
    }
  }
  for (int i = 1; i <= k; i++) {
    // cout<<i<<" "<<g[i]<<endl;
    ans += i * g[i];
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}
Problem p02715 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define int long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define mp make_pair
#define Pii pair<int, int>
#define Pid pair<int, double>
#define Pdd pair<double, double>
#define MOD 1000000007
#define EPS 0.00000001
#define N_MAX 100000

using namespace std;

int powmod(int x, int y) {
  int ret = 1;
  REP(_, y) ret = (ret * x) % MOD;
  return ret;
}

int gcd(int x, int y) {
  if (x > y)
    swap(x, y);
  while (true) {
    y = y % x;
    if (y == 0)
      return x;
    swap(x, y);
  }
}

signed main() {
  int n, k;
  int ans = 0;
  cin >> n >> k;
  vector<int> a(k);
  REP(i, k) { a[i] = k / (i + 1); }
  vector<int> b(k);
  REP(i, k) {
    int idx = k - 1 - i;
    REP(j, a[idx] - 1) {
      b[idx] = (b[idx] - b[(j + 2) * (idx + 1) - 1] + MOD) % MOD;
    }
    b[idx] = (b[idx] + powmod(a[idx], n)) % MOD;
  }
  REP(i, k) {
    // cout << b[i] << "\n";
    ans = (ans + b[i] * (i + 1)) % MOD;
  }
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define int long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define mp make_pair
#define Pii pair<int, int>
#define Pid pair<int, double>
#define Pdd pair<double, double>
#define MOD 1000000007
#define EPS 0.00000001
#define N_MAX 100000

using namespace std;

int powmod(int x, int y) {
  int ret = 1;
  while (y > 0) {
    if (y & 1)
      ret = (ret * x) % MOD;
    x = (x * x) % MOD;
    y >>= 1;
  }
  return ret % MOD;
}

int gcd(int x, int y) {
  if (x > y)
    swap(x, y);
  while (true) {
    y = y % x;
    if (y == 0)
      return x;
    swap(x, y);
  }
}

signed main() {
  int n, k;
  int ans = 0;
  cin >> n >> k;
  vector<int> a(k);
  REP(i, k) { a[i] = k / (i + 1); }
  vector<int> b(k);
  REP(i, k) {
    int idx = k - 1 - i;
    REP(j, a[idx] - 1) {
      b[idx] = (b[idx] - b[(j + 2) * (idx + 1) - 1] + MOD) % MOD;
    }
    b[idx] = (b[idx] + powmod(a[idx], n)) % MOD;
  }
  REP(i, k) {
    // cout << b[i] << "\n";
    ans = (ans + b[i] * (i + 1)) % MOD;
  }
  cout << ans << "\n";
  return 0;
}
Problem p02715 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define inf 1001001001
#define mod 1000000007
#define pi 3.141592653589793
typedef vector<int> vi;
typedef vector<ll> vl;
ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
ll rep_jijo(ll n, ll x) {
  if (x == 0)
    return 1;
  if (x % 2 == 0) {
    ll t = rep_jijo(n, x / 2);
    return t * t % mod;
  }
  return n * rep_jijo(n, x - 1) % mod;
}
ll com(ll n, ll r) {
  ll x = 1, y = 1;
  ll tmp = n;
  while (tmp >= n - r + 1) {
    x = x * tmp % mod;
    tmp--;
  }
  while (r > 0) {
    y = y * r % mod;
    r--;
  }
  return x * rep_jijo(y, mod - 2) % mod;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> d(K);

  d[K - 1] = 1;
  for (ll i = K; i > 0; i--) {
    ll sum = 0;
    for (ll j = 2 * i; j <= K; j += i) {
      sum += d[j - 1];
    }
    ll z = 1;
    ll tmp = N;
    ll x = K / i;
    while (tmp > 0) {
      z *= x;
      z %= mod;
      tmp--;
    }

    d[i - 1] = z - sum;
  }
  ll ans = 0;
  rep(i, K) {
    ans += d[i] * (i + 1);
    ans %= mod;
  }

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define inf 1001001001
#define mod 1000000007
#define pi 3.141592653589793
typedef vector<int> vi;
typedef vector<ll> vl;
ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
ll rep_jijo(ll n, ll x) {
  if (x == 0)
    return 1;
  if (x % 2 == 0) {
    ll t = rep_jijo(n, x / 2);
    return t * t % mod;
  }
  return n * rep_jijo(n, x - 1) % mod;
}
ll com(ll n, ll r) {
  ll x = 1, y = 1;
  ll tmp = n;
  while (tmp >= n - r + 1) {
    x = x * tmp % mod;
    tmp--;
  }
  while (r > 0) {
    y = y * r % mod;
    r--;
  }
  return x * rep_jijo(y, mod - 2) % mod;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> d(K);

  d[K - 1] = 1;
  for (ll i = K; i > 0; i--) {
    ll sum = 0;
    for (ll j = 2 * i; j <= K; j += i) {
      sum += d[j - 1];
    }
    ll z = rep_jijo(K / i, N);
    d[i - 1] = z - sum;
  }
  ll ans = 0;
  rep(i, K) {
    ans += d[i] * (i + 1);
    ans %= mod;
  }

  cout << ans % mod << endl;
  return 0;
}
Problem p02715 (Python)

Buggy submission (status: Time Limit Exceeded):
from collections import defaultdict

N, K = [int(i) for i in input().split()]
mod = 10**9 + 7
dd = defaultdict(int)

ans = 0
for i in range(K, 0, -1):
    dd[i] = (K // i) ** N
    for temp in range(i * 2, K + 1, i):
        dd[i] -= dd[temp]
    ans += dd[i] * i
    ans %= mod
print(ans)


Fixed submission:
from collections import defaultdict

N, K = [int(i) for i in input().split()]
mod = 10**9 + 7
dd = defaultdict(int)

ans = 0
for i in range(K, 0, -1):
    dd[i] = pow(K // i, N, mod)
    for temp in range(i * 2, K + 1, i):
        dd[i] -= dd[temp]
    ans += dd[i] * i
    ans %= mod
print(ans)
Problem p02715 (C++)

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

using namespace std;

#define int long long
#define endl '\n'
#define maxn 100010
const int MOD = 1000000007;

int pw(int a, int b, int m = MOD) {
  if (b == 0)
    return 1;
  if (b & 1)
    return a * pw(a, b - 1) % m;
  return pw(a * a % m, b / 2);
}

void solve() {
  int n, k;
  cin >> n >> k;

  vector<int> ans(n + 1);
  int pans = 0;
  for (int i = k; i >= 1; i--) {
    ans[i] = pw(k / i, n);
    for (int j = 2; j * i <= k; j++) {
      ans[i] = (ans[i] - ans[i * j] + MOD) % MOD;
    }
    pans = (pans + ans[i] * i % MOD) % MOD;
  }
  cout << pans << endl;
}

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

  int t = 1;
  //    cin >> t;

  while (t--)
    solve();
  return 0;
}

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

using namespace std;

#define int long long
#define endl '\n'
#define maxn 100010
const int MOD = 1000000007;

int pw(int a, int b, int m = MOD) {
  if (b == 0)
    return 1;
  if (b & 1)
    return a * pw(a, b - 1) % m;
  return pw(a * a % m, b / 2);
}

void solve() {
  int n, k;
  cin >> n >> k;

  vector<int> ans(k + 1);
  int pans = 0;
  for (int i = k; i >= 1; i--) {
    ans[i] = pw(k / i, n);
    for (int j = 2; j * i <= k; j++) {
      ans[i] = (ans[i] - ans[i * j] + MOD) % MOD;
    }
    pans = (pans + ans[i] * i % MOD) % MOD;
  }
  cout << pans << endl;
}

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

  int t = 1;
  //    cin >> t;

  while (t--)
    solve();
  return 0;
}
Problem p02715 (C++)

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

using namespace std;
typedef long long ll;

const ll mo = 1000000007;

ll modpow(ll a, ll n, ll mod) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = ret * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return ret;
}

ll MOD(ll a, ll mod) {
  ll temp = a % mod;
  if (temp < 0)
    temp += mod;
  return temp;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> dp(N + 1, 0);

  for (ll i = K; i >= 1; i--) {
    ll temp = modpow(K / i, N, mo);
    ll b = 2 * i;
    while (b <= K) {
      temp = MOD(temp - dp[b] % mo, mo);
      b += i;
    }
    dp[i] = temp;
  }

  ll sum = 0;
  for (ll i = 0; i <= K; i++) {
    sum = (sum + dp[i] * i % mo) % mo;
  }

  cout << sum << endl;
  return 0;
}

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

using namespace std;
typedef long long ll;

const ll mo = 1000000007;

ll modpow(ll a, ll n, ll mod) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = ret * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return ret;
}

ll MOD(ll a, ll mod) {
  ll temp = a % mod;
  if (temp < 0)
    temp += mod;
  return temp;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> dp(K + 1, 0);

  for (ll i = K; i >= 1; i--) {
    ll temp = modpow(K / i, N, mo);
    ll b = 2 * i;
    while (b <= K) {
      temp = MOD(temp - dp[b] % mo, mo);
      b += i;
    }
    dp[i] = temp;
  }

  ll sum = 0;
  for (ll i = 0; i <= K; i++) {
    sum = (sum + dp[i] * i % mo) % mo;
  }

  cout << sum << endl;
  return 0;
}
Problem p02715 (C++)

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

ll modpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}

int main() {
  ll N, K;
  cin >> N >> K;

  ll ans = 0;
  ll num[N + 1];
  for (ll i = K; i >= 1; i--) {
    num[i] = modpow(K / i, N);
    for (ll j = 2; i * j <= K; j++)
      num[i] = (num[i] - num[i * j] + MOD) % MOD;
    ans = (ans + i * num[i]) % MOD;
  }

  cout << ans << endl;
}

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

ll modpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}

int main() {
  ll N, K;
  cin >> N >> K;

  ll ans = 0;
  ll num[K + 1];
  for (ll i = K; i >= 1; i--) {
    num[i] = modpow(K / i, N);
    for (ll j = 2; i * j <= K; j++)
      num[i] = (num[i] - num[i * j] + MOD) % MOD;
    ans = (ans + i * num[i]) % MOD;
  }

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

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

long long mpow(long long x, int n) {
  long long r = 1;
  while (n > 0) {
    if (n & 1)
      r = r * x % 1000000007;
    x = x * x % 1000000007;
    n >>= 1;
  }
  return r;
}

int main() {
  int n, k;
  cin >> n >> k;

  long long sumg = 0;
  vector<long long> cntg(k);
  for (int i = k; 0 < i; i--) {
    long long cntb = k / i;
    cntg[i] = mpow(cntb, n);
    for (int j = 2; j * i <= k; j++) {
      cntg[i] += (1000000007 - cntg[j * i]);
      cntg[i] %= 1000000007;
      if (cntg[i] < 0)
        cout << 1;
    }
    sumg += cntg[i] * i;
    sumg %= 1000000007;
  }

  cout << sumg << endl;
}


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

long long mpow(long long x, int n) {
  long long r = 1;
  while (n > 0) {
    if (n & 1)
      r = r * x % 1000000007;
    x = x * x % 1000000007;
    n >>= 1;
  }
  return r;
}

int main() {
  int n, k;
  cin >> n >> k;

  long long sumg = 0;
  long long cntg[100010];
  for (int i = k; 0 < i; i--) {
    long long cntb = k / i;
    cntg[i] = mpow(cntb, n);
    for (int j = 2; j * i <= k; j++) {
      cntg[i] += (1000000007 - cntg[j * i]);
      cntg[i] %= 1000000007;
      if (cntg[i] < 0)
        cout << 1;
    }
    sumg += cntg[i] * i;
    sumg %= 1000000007;
  }

  cout << sumg << endl;
}
Problem p02715 (Python)

Buggy submission (status: Runtime Error):
# YouTube解説
MOD = 1000000007
n, k = [int(x) for x in input().split()]
d = [0] * (k + 1)

for i in range(0, k + 1):
    d[i] = pow(k // i, n, MOD)

for i in range(k, 0, -1):  # 大きいほうから
    for j in range(i * 2, k + 1, i):  # iの倍数
        # d[6] = d'[6] - d[12] - d[18] ...
        d[i] -= d[j]
        d[i] %= MOD
ans = 0
for i, item in enumerate(d):
    ans += i * item
    ans %= MOD
print(ans)


Fixed submission:
# YouTube解説
MOD = 1000000007
n, k = [int(x) for x in input().split()]
d = [0] * (k + 1)

for i in range(1, k + 1):
    d[i] = pow(k // i, n, MOD)

for i in range(k, 0, -1):  # 大きいほうから
    for j in range(i * 2, k + 1, i):  # iの倍数
        # d[6] = d'[6] - d[12] - d[18] ...
        d[i] -= d[j]
        d[i] %= MOD
ans = 0
for i, item in enumerate(d):
    ans += i * item
    ans %= MOD
print(ans)
Problem p02715 (C++)

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

#define mod 1000000007

using namespace std;
using ll = long long;

ll mypow(ll a, ll b) {
  ll res = a;
  for (int i = 1; i < b; ++i) {
    res = (res * a) % mod;
  }
  return res;
}

int main() {
  ll N, K;
  cin >> N >> K;

  ll res = 0;
  ll cnt[100001] = {};
  for (ll i = K; i > 0; --i) {
    cnt[i] += mypow((K / i), N);
    for (ll j = 2; j <= K; ++j) {
      if (i * j <= K) {
        cnt[i] -= cnt[i * j];
      } else {
        break;
      }
    }
    res += (cnt[i] * i) % mod;
  }
  res = res % mod;
  cout << res;
  return 0;
}

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

#define mod 1000000007

using namespace std;
using ll = long long;

ll mypow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

int main() {
  ll N, K;
  cin >> N >> K;

  ll res = 0;
  ll cnt[100001] = {};
  for (ll i = K; i > 0; --i) {
    cnt[i] += mypow((K / i), N);
    for (ll j = 2; j <= K; ++j) {
      if (i * j <= K) {
        cnt[i] -= cnt[i * j];
      } else {
        break;
      }
    }
    res += (cnt[i] * i) % mod;
  }
  res = res % mod;
  cout << res;
  return 0;
}
Problem p02715 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define all(c) c.begin(), c.end()
#define gmax(x, y) x = max(x, y)
#define gmin(x, y) x = min(x, y)
#define gadd(x, y) x = add(x, y)
#define gmul(x, y) x = mul(x, y)
using namespace std;

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

const int MOD = 1e9 + 7;

inline int add(int x, int y) {
  ll res = x + y;
  while (res >= MOD)
    res -= MOD;
  return res;
}

inline int mul(int x, int y) { return (1LL * x * y) % MOD; }

int modpow(int x, int p) {
  int res = 1;
  while (p) {
    if (p & 1)
      gmul(res, x);
    gmul(x, x);
    p /= 2;
  }
  return res;
}

inline int invert(int x) { return modpow(x, MOD - 2); }

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int n, k;
  cin >> n >> k;
  vector<int> dp(n + 1);
  rep(i, 1, k + 1) {
    dp[i] = modpow(i, n);
    int la;
    for (int x = 2; x <= i; x = la + 1) {
      la = min(i / (i / x), i);
      gadd(dp[i], MOD - mul(la - x + 1, dp[i / x]));
    }
  }
  int ans = 0;
  rep(i, 1, k + 1) { gadd(ans, mul(i, dp[k / i])); }
  cout << ans << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define all(c) c.begin(), c.end()
#define gmax(x, y) x = max(x, y)
#define gmin(x, y) x = min(x, y)
#define gadd(x, y) x = add(x, y)
#define gmul(x, y) x = mul(x, y)
using namespace std;

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

const int MOD = 1e9 + 7;

inline int add(int x, int y) {
  ll res = x + y;
  while (res >= MOD)
    res -= MOD;
  return res;
}

inline int mul(int x, int y) { return (1LL * x * y) % MOD; }

int modpow(int x, int p) {
  int res = 1;
  while (p) {
    if (p & 1)
      gmul(res, x);
    gmul(x, x);
    p /= 2;
  }
  return res;
}

inline int invert(int x) { return modpow(x, MOD - 2); }

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int n, k;
  cin >> n >> k;
  vector<int> dp(k + 1);
  rep(i, 1, k + 1) {
    dp[i] = modpow(i, n);
    int la;
    for (int x = 2; x <= i; x = la + 1) {
      la = min(i / (i / x), i);
      gadd(dp[i], MOD - mul(la - x + 1, dp[i / x]));
    }
  }
  int ans = 0;
  rep(i, 1, k + 1) { gadd(ans, mul(i, dp[k / i])); }
  cout << ans << '\n';
}
Problem p02715 (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;

const LL MOD = 1000000007LL;

LL modpow(LL x, LL n) {
  LL r = 1;
  while (n != 0) {
    if (n & 1)
      r = r * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return r;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<LL> count(N + 1);
  LL ret = 0;
  for (int i = K; i >= 1; --i) {
    LL n = modpow(K / i, N);
    for (int j = i * 2; j <= K; j += i) {
      n = (n + MOD - count[j]) % MOD;
    }
    count[i] = n;
    ret = (ret + n * i) % MOD;
  }
  cout << ret << 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;

const LL MOD = 1000000007LL;

LL modpow(LL x, LL n) {
  LL r = 1;
  while (n != 0) {
    if (n & 1)
      r = r * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return r;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<LL> count(K + 1);
  LL ret = 0;
  for (int i = K; i >= 1; --i) {
    LL n = modpow(K / i, N);
    for (int j = i * 2; j <= K; j += i) {
      n = (n + MOD - count[j]) % MOD;
    }
    count[i] = n;
    ret = (ret + n * i) % MOD;
  }
  cout << ret << endl;
}
Problem p02715 (C++)

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

using namespace std;

static const int64_t MOD = 1000000007;

int64_t modpow(int64_t base, int64_t k, int64_t mod) {
  base %= mod;
  int64_t answer = 1;
  while (k > 0) {
    if (k & 1)
      answer = (answer * base) % mod;
    base = (base * base) % mod;
    k >>= 1;
  }
  return answer;
}

int main() {
  int64_t N, K;
  cin >> N >> K;

  vector<int64_t> counts(K);

  int64_t answer = 0;
  for (int64_t gcd = K; gcd >= 1; gcd--) {
    int64_t multiples = K / gcd;
    int64_t count = modpow(multiples, N, MOD);
    for (int k = 2; gcd * k <= K; k++) {
      count = (count + MOD - counts[gcd * k]) % MOD;
    }
    counts[gcd] = count;
    answer = (answer + gcd * counts[gcd]) % MOD;
  }
  cout << answer << endl;

  return 0;
}


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

using namespace std;

static const int64_t MOD = 1000000007;

int64_t modpow(int64_t base, int64_t k, int64_t mod) {
  base %= mod;
  int64_t answer = 1;
  while (k > 0) {
    if (k & 1)
      answer = (answer * base) % mod;
    base = (base * base) % mod;
    k >>= 1;
  }
  return answer;
}

int main() {
  int64_t N, K;
  cin >> N >> K;

  vector<int64_t> counts(K + 1);

  int64_t answer = 0;
  for (int64_t gcd = K; gcd >= 1; gcd--) {
    int64_t multiples = K / gcd;
    int64_t count = modpow(multiples, N, MOD);
    for (int k = 2; gcd * k <= K; k++) {
      count = (count + MOD - counts[gcd * k]) % MOD;
    }
    counts[gcd] = count;
    answer = (answer + gcd * counts[gcd]) % MOD;
  }
  cout << answer << endl;

  return 0;
}
Problem p02715 (C++)

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

long BIG = 1000000007;

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  long N, K;
  long long ans = 0;
  cin >> N >> K;

  vector<long> vec(N + 10, -1);
  for (int i = K; i > 0; i--) {
    vec[i] = modpow(K / i, N, BIG);
    for (int j = i * 2; j <= K; j += i) {
      vec[i] = (vec[i] - vec[j] + BIG) % BIG;
    }
    ans = (ans % BIG + vec[i] * i % BIG) % BIG;
    // cout << ans << endl;
  }
  cout << ans << endl;
}

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

long BIG = 1000000007;

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  long N, K;
  long long ans = 0;
  cin >> N >> K;

  vector<long> vec(K + 10, -1);
  for (int i = K; i > 0; i--) {
    vec[i] = modpow(K / i, N, BIG);
    for (int j = i * 2; j <= K; j += i) {
      vec[i] = (vec[i] - vec[j] + BIG) % BIG;
    }
    ans = (ans % BIG + vec[i] * i % BIG) % BIG;
    // cout << ans << endl;
  }
  cout << ans << endl;
}
Problem p02715 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long gcd(long long a, long long b) {
  if (a < b) {
    swap(a, b);
  }
  if (a % b == 0) {
    return b;
  }
  return gcd(b, a % b);
}

long long power(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b % 2) {
      ans = ans * a % mod;
    }
    a = a * a % mod;
    b /= 2;
  }
  return ans;
}

int main() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(k + 1, 0);
  for (long long i = k; i >= 1; i--) {
    a[i] = power(k / i, n);
    int j = 2;
    while (j <= i) {
      a[i] = (a[i] - a[i * j]) % mod;
      if (a[i] < 0) {
        a[i] += mod;
      }
      j++;
    }
  }
  long long ans = 0;
  for (long long i = 1; i <= k; i++) {
    ans += a[i] * i % mod;
    ans %= mod;
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long gcd(long long a, long long b) {
  if (a < b) {
    swap(a, b);
  }
  if (a % b == 0) {
    return b;
  }
  return gcd(b, a % b);
}

long long power(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b % 2) {
      ans = ans * a % mod;
    }
    a = a * a % mod;
    b /= 2;
  }
  return ans;
}

int main() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(k + 1, 0);
  for (long long i = k; i >= 1; i--) {
    a[i] = power(k / i, n);
    int j = 2;
    while (i * j <= k) {
      a[i] = (a[i] - a[i * j]) % mod;
      if (a[i] < 0) {
        a[i] += mod;
      }
      j++;
    }
  }
  long long ans = 0;
  for (long long i = 1; i <= k; i++) {
    ans += a[i] * i % mod;
    ans %= mod;
  }
  cout << ans;
  return 0;
}