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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  string s;
  cin >> s;
  long long k;
  cin >> k;
  int s_length = (int)s.length();
  int tmp = 0;
  int buf = 0;
  vector<int> array(s_length, 0);
  long long ans = 0;
  if (s[0] == s[s_length - 1])
    buf = 1;
  for (int i = 1; i < s_length; i++) {
    if (s[i] == s[i - 1]) {
      array[i] = array[i - 1] + 1;
    }
    if (array[i] % 2 == 1)
      ans++;
  }
  if (buf != 1) {
    ans = ans * k;
    cout << ans << endl;
  }
  long long ans2 = 0;
  long long ans3 = 0;
  long long pic = 0;
  if (buf == 1) {
    char s2[s_length * 2], s3[s_length * 2];
    for (int i = 0; i < s_length; i++) {
      s3[i] = s[i];
      s3[i + s_length] = s[i];
      s3[i + s_length * 2] = s[i];
    }
    vector<int> array3(s_length * 3, 0);
    for (int i = 1; i < s_length * 3; i++) {
      if (s3[i] == s3[i - 1]) {
        array3[i] = array3[i - 1] + 1;
      }
      if (i < s_length * 2 && array3[i] % 2 == 1)
        ans2++;
      if (array3[i] % 2 == 1)
        ans3++;
      ;
    }
    long long res = ans3 - ans;
    if (k == 1)
      pic = ans;
    if (k == 2)
      pic = ans2;
    if (k == 3)
      pic = ans3;
    if (k > 3) {
      if (k % 2 == 0) {
        pic = ans2 + res * (k / 2 - 1);
      } else {
        pic = ans + res * (k - 1) / 2;
      }
    }
    cout << pic << endl;
  }
  //  cout<<s_length<<endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  string s;
  cin >> s;
  long long k;
  cin >> k;
  int s_length = (int)s.length();
  int tmp = 0;
  int buf = 0;
  vector<int> array(s_length, 0);
  long long ans = 0;
  if (s[0] == s[s_length - 1])
    buf = 1;
  for (int i = 1; i < s_length; i++) {
    if (s[i] == s[i - 1]) {
      array[i] = array[i - 1] + 1;
    }
    if (array[i] % 2 == 1)
      ans++;
  }
  if (buf != 1) {
    ans = ans * k;
    cout << ans << endl;
  }
  long long ans2 = 0;
  long long ans3 = 0;
  long long pic = 0;
  if (buf == 1) {
    char s2[s_length * 2], s3[s_length * 3];
    for (int i = 0; i < s_length; i++) {
      s3[i] = s[i];
      s3[i + s_length] = s[i];
      s3[i + s_length * 2] = s[i];
    }
    vector<int> array3(s_length * 3, 0);
    for (int i = 1; i < s_length * 3; i++) {
      if (s3[i] == s3[i - 1]) {
        array3[i] = array3[i - 1] + 1;
      }
      if (i < s_length * 2 && array3[i] % 2 == 1)
        ans2++;
      if (array3[i] % 2 == 1)
        ans3++;
      ;
    }
    long long res = ans3 - ans;
    if (k == 1)
      pic = ans;
    if (k == 2)
      pic = ans2;
    if (k == 3)
      pic = ans3;
    if (k > 3) {
      if (k % 2 == 0) {
        pic = ans2 + res * (k / 2 - 1);
      } else {
        pic = ans + res * (k - 1) / 2;
      }
    }
    cout << pic << endl;
  }
  //  cout<<s_length<<endl;
  return 0;
}
Problem p02891 (C++)

Buggy submission (status: Runtime Error):
// https://yamakasa.net/atcoder-agc-039-a/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
int main() {
  string S;
  ll K;
  cin >> S >> K;
  ll n = S.length();
  if (n == 1) {
    cout << K / 2 << "\n";
    return 0;
  }
  ll cnt = 1;
  REP(i, n - 1) {
    if (S[i] == S[i + 1]) {
      cnt++;
    }
  }
  if (cnt == n) {
    cout << K * n / 2 << endl;
    return 0;
  }
  ll l = 0;
  ll r = 0;
  for (int i = 0; i < n; i++) {
    if (S[0] == S[i]) {
      l++;
    } else {
      break;
    }
  }
  if (l == n) {
    cout << n * K / 2 << "\n";
    return 0;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (S[n - 1] == S[i]) {
      r++;
    } else {
      break;
    }
  }
  if (l + r == n) {
    return -1;
  }
  ll ans = 0;
  ll c = 1;
  for (int i = l; i < n - r; i++) {
    if (S[i] == S[i + 1]) {
      c++;
    } else {
      ans += c / 2 * K;
      c = 1;
    }
  }
  ans += c / 2 * (K - 1);

  if (S[0] == S[n - 1]) {
    ans += l / 2 + (l + r) / 2 * (K - 1) + r / 2;
  } else {
    ans += l / 2 * K + r / 2 * K;
  }
  // cout << l << " " << r << "\n";
  cout << ans << "\n";
  return 0;
}

Fixed submission:
// https://yamakasa.net/atcoder-agc-039-a/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
int main() {
  string S;
  ll K;
  cin >> S >> K;
  ll n = S.length();
  if (n == 1) {
    cout << K / 2 << "\n";
    return 0;
  }
  ll cnt = 1;
  REP(i, n - 1) {
    if (S[i] == S[i + 1]) {
      cnt++;
    }
  }
  if (cnt == n) {
    cout << K * n / 2 << endl;
    return 0;
  }
  ll l = 0;
  ll r = 0;
  for (int i = 0; i < n; i++) {
    if (S[0] == S[i]) {
      l++;
    } else {
      break;
    }
  }
  if (l == n) {
    cout << n * K / 2 << "\n";
    return 0;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (S[n - 1] == S[i]) {
      r++;
    } else {
      break;
    }
  }
  if (l + r == n) {
    // S[0] != S[n - 1]
    // aaaaa nnnnnnnみたいなの
    ll ans = l / 2 * K + r / 2 * K;
    cout << ans << "\n";
    return 0;
  }
  ll ans = 0;
  ll c = 1;
  for (int i = l; i < n - r; i++) {
    if (S[i] == S[i + 1]) {
      c++;
    } else {
      ans += c / 2 * K;
      c = 1;
    }
  }
  ans += c / 2 * (K - 1);

  if (S[0] == S[n - 1]) {
    ans += l / 2 + (l + r) / 2 * (K - 1) + r / 2;
  } else {
    ans += l / 2 * K + r / 2 * K;
  }
  // cout << l << " " << r << "\n";
  cout << ans << "\n";
  return 0;
}
Problem p02891 (C++)

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

using namespace std;
string s;
long long k;

int main() {
  cin >> s >> k;
  int n = s.size();
  if (n == 1) {
    cout << k / 2 << endl;
    return 0;
  }
  if (n == 2) {
    cout << (s[0] == s[1]) * k << endl;
    return 0;
  }
  long long a = 0, b = 0;
  while (s[a] == s[n - 1] && a < n)
    a++;
  while (b < n && s[0] == s[n - b - 1])
    b++;
  long long cnt = 0;
  for (int i = 0; i < n - 1; i++) {
    if (s[i] == s[i + 1])
      cnt++, i++;
  }
  if (a == n) {
    assert(0);
  }
  cout << cnt * k - ((a / 2) + (b / 2) - ((a + b) / 2)) * (k - 1) << endl;

  return 0;
}


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

using namespace std;
string s;
long long k;

int main() {
  cin >> s >> k;
  int n = s.size();
  if (n == 1) {
    cout << k / 2 << endl;
    return 0;
  }
  if (n == 2) {
    cout << (s[0] == s[1]) * k << endl;
    return 0;
  }
  long long a = 0, b = 0;
  while (s[a] == s[n - 1] && a < n)
    a++;
  while (b < n && s[0] == s[n - b - 1])
    b++;
  long long cnt = 0;
  for (int i = 0; i < n - 1; i++) {
    if (s[i] == s[i + 1])
      cnt++, i++;
  }
  if (a == n) {
    cout << n * k / 2 << endl;
    return 0;
  }
  cout << cnt * k - ((a / 2) + (b / 2) - ((a + b) / 2)) * (k - 1) << endl;

  return 0;
}
Problem p02891 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
#define repl(i, l, r) for (ll i = l; i < r; i++)
#define rep(i, n) repl(i, 0, n)

using namespace std;
using ll = long long;

int main() {

  string S;
  ll K, N;

  cin >> S >> K;

  N = S.size();

  vector<ll> c;
  ll tmp = 1;

  repl(i, 1, N) {
    if (S[i - 1] == S[i]) {
      tmp++;
      if (i == N - 1) {
        c.push_back(tmp);
      }
    } else {
      c.push_back(tmp);
      tmp = 1;
    }
  }

  ll ans = 0;
  ll n = c.size();

  rep(i, n) { ans += c[i] / 2; }

  ans = ans * K;

  if (S.front() == S.back()) {
    ans = ans -
          (c.front() / 2 + c.back() / 2 - (c.front() + c.back()) / 2) * (K - 1);
  }

  if (n == 1) {
    ans = c.front() * K / 2;
  }

  cout << ans;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
#define repl(i, l, r) for (ll i = l; i < r; i++)
#define rep(i, n) repl(i, 0, n)

using namespace std;
using ll = long long;

int main() {

  string S;
  ll K, N;

  cin >> S >> K;

  N = S.size();

  vector<ll> c;
  ll tmp = 1;

  repl(i, 1, N) {
    if (S[i - 1] == S[i]) {
      tmp++;
      if (i == N - 1) {
        c.push_back(tmp);
      }
    } else {
      c.push_back(tmp);
      tmp = 1;
    }
  }

  if (N == 1) {
    c.push_back(1);
  }

  ll ans = 0;
  ll n = c.size();

  rep(i, n) { ans += c[i] / 2; }

  ans = ans * K;

  if (S.front() == S.back()) {
    ans = ans -
          (c.front() / 2 + c.back() / 2 - (c.front() + c.back()) / 2) * (K - 1);
  }

  if (n == 1) {
    ans = c.front() * K / 2;
  }

  cout << ans;

  return 0;
}
Problem p02891 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n - 1); i >= 0; i--)
#define all(v) (v).begin(), (v).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const ll MOD = 1e9 + 7;

int main() {
  string s;
  ll k;
  cin >> s >> k;
  vector<int> chrs;
  int num = 1;
  rep(i, s.size()) {
    if (i) {
      if (s[i - 1] == s[i]) {
        num++;
      } else {
        chrs.push_back(num);
        num = 1;
      }
    }
  }
  chrs.push_back(num);
  ll cnt = 0; // sの最初と最後の文字種を除いたs内の操作回数
  rep(i, chrs.size() - 2) cnt += chrs[i + 1] / 2;

  ll ans = 0;
  if (chrs.size() == 1) { // 全結合
    ans = (k * s.size()) / 2;
  } else if (s.front() == s.back()) { // 結合あり
    ans = cnt * k + chrs.front() / 2 + chrs.back() / 2 +
          ((chrs.front() + chrs.back()) / 2) * (k - 1);
  } else { // 結合なし
    ans = (cnt + chrs.front() / 2 + chrs.back() / 2) * k;
  }

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n - 1); i >= 0; i--)
#define all(v) (v).begin(), (v).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const ll MOD = 1e9 + 7;

int main() {
  string s;
  ll k;
  cin >> s >> k;
  vector<int> chrs;
  int num = 1;
  rep(i, s.size()) {
    if (i) {
      if (s[i - 1] == s[i]) {
        num++;
      } else {
        chrs.push_back(num);
        num = 1;
      }
    }
  }
  chrs.push_back(num);
  ll cnt = 0; // sの最初と最後の文字種を除いたs内の操作回数
  rep(i, int(chrs.size()) - 2) cnt += chrs[i + 1] / 2;

  ll ans = 0;
  if (chrs.size() == 1) { // 全結合
    ans = (k * s.size()) / 2;
  } else if (s.front() == s.back()) { // 結合あり
    ans = cnt * k + chrs.front() / 2 + chrs.back() / 2 +
          ((chrs.front() + chrs.back()) / 2) * (k - 1);
  } else { // 結合なし
    ans = (cnt + chrs.front() / 2 + chrs.back() / 2) * k;
  }

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

Buggy submission (status: Runtime Error):
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
char c[105];
long long a[105];
int main() {
  long long sum = 0;
  long long res = 0;
  long long m = 0, n;
  long long cnt = 0;
  char s;
  while (s = getchar()) {
    if (s == '\n')
      break;
    c[++m] = s;
  }
  cin >> n;
  if (m == 1) {
    cout << n / 2;
    return 0;
  }
  for (int i = 1; i < m; i++) {
    if (c[i] == c[i + 1]) {
      cnt++;
    } else {
      a[i - cnt] = cnt;
      cnt = 0;
    }
  }

  if (c[m] == c[m - 1]) {
    a[m - cnt] = cnt;
  }
  // for (int i = 1; i <= m; i++)
  //	cout << a[i] << endl;
  for (int i = 1; i <= m; i++) {
    if (a[i] != 0) {
      res += (a[i] + 1) / 2;
    }
  }
  if (a[m - cnt] + 1 == m) {
    cout << n * m / 2;
    return 0;
  }
  if (c[1] == c[m]) {
    if ((a[m - cnt] + 1) % 2 == 0) {
      cout << res * n;
      return 0;
    } else {
      if ((a[1] + 1) % 2 == 0) {
        return res * n;
      }
      cout << (res + 1) * n - 1;
      return 0;
    }
  }
  cout << res * n;
  return 0;
}

Fixed submission:
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
char c[105];
long long a[105];
int main() {
  long long sum = 0;
  long long res = 0;
  long long m = 0, n;
  long long cnt = 0;
  char s;
  while (s = getchar()) {
    if (s == '\n')
      break;
    c[++m] = s;
  }
  cin >> n;
  if (m == 1) {
    cout << n / 2;
    return 0;
  }
  for (int i = 1; i < m; i++) {
    if (c[i] == c[i + 1]) {
      cnt++;
    } else {
      a[i - cnt] = cnt;
      cnt = 0;
    }
  }

  if (c[m] == c[m - 1]) {
    a[m - cnt] = cnt;
  }
  // for (int i = 1; i <= m; i++)
  //	cout << a[i] << endl;
  for (int i = 1; i <= m; i++) {
    if (a[i] != 0) {
      res += (a[i] + 1) / 2;
    }
  }
  if (a[m - cnt] + 1 == m) {
    cout << n * m / 2;
    return 0;
  }
  if (c[1] == c[m]) {
    if ((a[m - cnt] + 1) % 2 == 0) {
      cout << res * n;
      return 0;
    } else {
      if (a[1] % 2 == 1) {
        cout << res * n;
        return 0;
      }
      cout << (res + 1) * n - 1;
      return 0;
    }
  }
  cout << res * n;
  return 0;
}
Problem p02891 (Python)

Buggy submission (status: Runtime Error):
from itertools import groupby

s = input()
k = int(input())
a = [sum(1 for _ in g) for _, g in groupby(s)]
if s[0] == s[-1]:
    a[0] += a.pop()
print(sum(x // 2 for x in a) * k)


Fixed submission:
from itertools import groupby

s = input()
k = int(input())
a = [sum(1 for _ in g) for _, g in groupby(s)]
if len(a) == 1:
    print(len(s) * k // 2)
else:
    x, y = a[0], a[-1]
    b = (x + y) // 2 if s[0] == s[-1] else x // 2 + y // 2
    print(x // 2 + y // 2 + sum(x // 2 for x in a[1:-1]) * k + b * (k - 1))
Problem p02891 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e10;
const int ZERO = 0;

signed main() {
  string S;
  cin >> S;
  int K;
  cin >> K;
  bool s = true;
  for (int i = 0; i < S.size(); i++) {
    if (S.at(i) != S.at(i + 1)) {
      s = false;
    }
  }
  if (S.size() == 1) {
    cout << K / 2 << endl;
  } else if (s == true) {
    cout << S.size() * K / 2 << endl;
  } else {
    bool cnt = false;
    int count = 1;
    int A = 0;
    int B = 0;
    for (int i = 0; i < S.size() - 1; i++) {
      if (S.at(i) != S.at(i + 1) && cnt == true) {
        A += count / 2;
        count = 1;
        cnt = false;
      }
      if (S.at(i) == S.at(i + 1)) {
        cnt = true;
        count++;
      }
    }
    if (cnt == true) {
      A += count / 2;
      cnt = false;
    }
    S = S + S;
    count = 1;
    for (int i = 0; i < S.size() - 1; i++) {
      if (S.at(i) != S.at(i + 1) && cnt == true) {
        B += count / 2;
        count = 1;
        cnt = false;
      }
      if (S.at(i) == S.at(i + 1)) {
        cnt = true;
        count++;
      }
    }
    if (cnt == true) {
      B += count / 2;
    }
    B = B - A;
    cout << A + B * (K - 1) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e10;
const int ZERO = 0;

signed main() {
  string S;
  cin >> S;
  int K;
  cin >> K;
  bool s = true;
  for (int i = 0; i < S.size() - 1; i++) {
    if (S.at(i) != S.at(i + 1)) {
      s = false;
    }
  }
  if (S.size() == 1) {
    cout << K / 2 << endl;
  } else if (s == true) {
    cout << S.size() * K / 2 << endl;
  } else {
    bool cnt = false;
    int count = 1;
    int A = 0;
    int B = 0;
    for (int i = 0; i < S.size() - 1; i++) {
      if (S.at(i) != S.at(i + 1) && cnt == true) {
        A += count / 2;
        count = 1;
        cnt = false;
      }
      if (S.at(i) == S.at(i + 1)) {
        cnt = true;
        count++;
      }
    }
    if (cnt == true) {
      A += count / 2;
      cnt = false;
    }
    S = S + S;
    count = 1;
    for (int i = 0; i < S.size() - 1; i++) {
      if (S.at(i) != S.at(i + 1) && cnt == true) {
        B += count / 2;
        count = 1;
        cnt = false;
      }
      if (S.at(i) == S.at(i + 1)) {
        cnt = true;
        count++;
      }
    }
    if (cnt == true) {
      B += count / 2;
    }
    B = B - A;
    cout << A + B * (K - 1) << endl;
  }
}
Problem p02891 (C++)

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

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

int f(string S) {
  S.push_back('!');
  int res = 0;
  for (int i = 1; i < S.length(); ++i) {
    if (S[i] == S[i - 1]) {
      res += 1;
      S[i] = S[i + 1] + 1;
    }
  }
  return res;
}

signed main() {
  string S;
  int K;
  cin >> S >> K;
  if (S.length() == 1) {
    cout << K / 2 << endl;
    return 0;
  } else if (S.length() == 2) {
    if (S[0] == S[1]) {
      cout << K << endl;
      return 0;
    } else {
      cout << 0 << endl;
      return 0;
    }
  } else if (S.length() == 3) {
    if (S[0] == S[1] && S[1] == S[2]) {
      cout << 3 * K / 2;
    } else {
      if (S[0] == S[1] || S[1] == S[2]) {
        cout << K << endl;
      } else if (S[0] == S[2]) {
        cout << K - 1 << endl;
      } else {
        cout << 0 << endl;
      }
    }
    return 0;
  }
  int a3 = f(S + S + S);
  int a2 = f(S + S);
  int a1 = f(S);
  assert(a3 == 2 * (a2 - a1) + a1);
  cout << (a3 - a1) * ((K - 1) / 2) + (a2 - a1) * ((K - 1) % 2) + a1 << endl;
}


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

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

int f(string S) {
  S.push_back('!');
  int res = 0;
  for (int i = 1; i < S.length(); ++i) {
    if (S[i] == S[i - 1]) {
      res += 1;
      S[i] = S[i + 1] + 1;
    }
  }
  return res;
}

signed main() {
  string S;
  int K;
  cin >> S >> K;
  if (S.length() == 1) {
    cout << K / 2 << endl;
    return 0;
  } else if (S.length() == 2) {
    if (S[0] == S[1]) {
      cout << K << endl;
      return 0;
    } else {
      cout << 0 << endl;
      return 0;
    }
  } else if (S.length() == 3) {
    if (S[0] == S[1] && S[1] == S[2]) {
      cout << 3 * K / 2;
    } else {
      if (S[0] == S[1] || S[1] == S[2]) {
        cout << K << endl;
      } else if (S[0] == S[2]) {
        cout << K - 1 << endl;
      } else {
        cout << 0 << endl;
      }
    }
    return 0;
  }
  int a3 = f(S + S + S);
  int a2 = f(S + S);
  int a1 = f(S);
  cout << (a3 - a1) * ((K - 1) / 2) + (a2 - a1) * ((K - 1) % 2) + a1 << endl;
}
Problem p02891 (C++)

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

int main() {
  string S;
  long long K;
  cin >> S >> K;

  vector<int> len;
  int cnt = 1;
  for (int i = 1; i < S.size(); ++i) {
    if (S[i] == S[i - 1])
      ++cnt;
    if (S[i] != S[i - 1] || i + 1 == S.size()) {
      len.push_back(cnt);
      cnt = 1;
    }
  }

  if (len.size() && len[0] == S.size()) {
    cout << K * S.size() / 2 << endl;
    return 0;
  }

  long long res = 0;
  for (int n : len)
    res += n / 2;
  if (S[0] != S[S.size() - 1])
    cout << res * K << endl;
  else
    cout << res * K - (*begin(len) / 2 + *rbegin(len) / 2 -
                       (*begin(len) + *rbegin(len)) / 2) *
                          (K - 1)
         << endl;
}

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

int main() {
  string S;
  long long K;
  cin >> S >> K;

  vector<int> len;
  int cnt = 1;
  if (S.size() == 1)
    len.push_back(1);
  for (int i = 1; i < S.size(); ++i) {
    if (S[i] == S[i - 1])
      ++cnt;
    if (S[i] != S[i - 1] || i + 1 == S.size()) {
      len.push_back(cnt);
      cnt = 1;
    }
  }

  if (len.size() && len[0] == S.size()) {
    cout << K * S.size() / 2 << endl;
    return 0;
  }

  long long res = 0;
  for (int n : len)
    res += n / 2;
  if (S[0] != S[S.size() - 1])
    cout << res * K << endl;
  else
    cout << res * K - (*begin(len) / 2 + *rbegin(len) / 2 -
                       (*begin(len) + *rbegin(len)) / 2) *
                          (K - 1)
         << endl;
}
Problem p02891 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <sstream>
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctype.h>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

typedef long long int lli;
#define rep(i, s, N) for (int i = s; i < N; i++)
#define MOD 1000000007
#define more(a, b) (((a) > (b)) ? (a) : (b))
#define less(a, b) (((a) < (b)) ? (a) : (b))

using namespace std;

int main(void) {
  string S;
  cin >> S;
  lli K;
  cin >> K;
  char tmp = S[0];
  vector<int> cnt;
  int a = 1;
  rep(i, 1, S.length()) {
    if (tmp != S[i]) {
      cnt.push_back(a);
      a = 1;
      tmp = S[i];
    } else
      a++;
    if (i == S.length() - 1)
      cnt.push_back(a);
  }
  // rep(i, 0, cnt.size())cout << cnt[i] << " " << endl;
  if (cnt.size() == 1) {
    cout << K * cnt[0] / 2 << endl;
    return 0;
  }
  lli ans = 0;
  rep(i, 0, cnt.size()) ans += cnt[i] / 2;
  ans *= K;
  if (S[0] == S[S.length() - 1])
    ans += ((cnt[0] + cnt[cnt.size() - 1]) / 2 - cnt[0] / 2 -
            cnt[cnt.size() - 1] / 2) *
           (K - 1);

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

Fixed submission:
#include <iostream>
#include <sstream>
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctype.h>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

typedef long long int lli;
#define rep(i, s, N) for (int i = s; i < N; i++)
#define MOD 1000000007
#define more(a, b) (((a) > (b)) ? (a) : (b))
#define less(a, b) (((a) < (b)) ? (a) : (b))

using namespace std;

int main(void) {
  string S;
  cin >> S;
  lli K;
  cin >> K;
  char tmp = S[0];
  vector<int> cnt;
  int a = 0;
  rep(i, 0, S.length()) {
    if (tmp != S[i]) {
      cnt.push_back(a);
      a = 1;
      tmp = S[i];
    } else
      a++;
    if (i == S.length() - 1)
      cnt.push_back(a);
  }
  // rep(i, 0, cnt.size())cout << cnt[i] << " " << endl;
  if (cnt.size() == 1) {
    cout << K * cnt[0] / 2 << endl;
    return 0;
  }
  lli ans = 0;
  rep(i, 0, cnt.size()) ans += cnt[i] / 2;
  ans *= K;
  if (S[0] == S[S.length() - 1])
    ans += ((cnt[0] + cnt[cnt.size() - 1]) / 2 - cnt[0] / 2 -
            cnt[cnt.size() - 1] / 2) *
           (K - 1);

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
char S[105];
int K, las = 1;
vector<int> vec;
signed main() {
  cin >> (S + 1) >> K;
  int n = strlen(S + 1);
  for (int i = 2; i <= n; ++i)
    if (S[i] != S[i - 1])
      vec.push_back(i - las), las = i;
    else if (i == n)
      vec.push_back(i - las + 1);
  // for(auto v:vec) cout<<v<<endl;
  if (vec.size() == 1)
    return cout << n * K / 2, 0;
  if (S[1] == S[n]) {
    int ans = 0;
    for (int i = 1; i < vec.size() - 1; ++i)
      ans += vec[i] / 2;
    ans *= K;
    ans += (vec.front() + vec.back()) / 2 * (K - 1);
    ans += vec.front() / 2 + vec.back() / 2;
    cout << ans;
  } else {
    int ans = 0;
    for (auto v : vec)
      ans += v / 2 * K;
    cout << ans;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
char S[105];
int K, las = 1;
vector<int> vec;
signed main() {
  cin >> (S + 1) >> K;
  int n = strlen(S + 1);
  if (n == 1)
    return cout << n * K / 2, 0;
  for (int i = 2; i <= n; ++i)
    if (S[i] != S[i - 1])
      vec.push_back(i - las), las = i;
    else if (i == n)
      vec.push_back(i - las + 1);
  // for(auto v:vec) cout<<v<<endl;
  if (vec.size() == 1)
    return cout << n * K / 2, 0;
  if (S[1] == S[n]) {
    int ans = 0;
    for (int i = 1; i < vec.size() - 1; ++i)
      ans += vec[i] / 2;
    ans *= K;
    ans += (vec.front() + vec.back()) / 2 * (K - 1);
    ans += vec.front() / 2 + vec.back() / 2;
    cout << ans;
  } else {
    int ans = 0;
    for (auto v : vec)
      ans += v / 2 * K;
    cout << ans;
  }
  return 0;
}
Problem p02891 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <ios>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define itn int
#define retrun return
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
using namespace std;
typedef long long llong;
llong gcd(llong a, llong b) {
  if (a < b)
    std::swap(a, b);
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
llong lcm(llong a, llong b) {
  llong g = gcd(a, b);
  return a * b / g;
}
// ここまで共通
string S;
llong K;
llong ANS = 0;
vector<llong> A;
int main() {
  cin >> S;
  cin >> K;

  llong tmp = 1;
  for (int i = 1; i < S.length(); i++) {
    if (S[i - 1] == S[i]) {
      tmp++;
      if (i == S.length() - 1) {
        A.push_back(tmp);
      }
    } else {
      A.push_back(tmp);
      tmp = 1;
    }
  }
  if (A.size() == 1) {
    ANS = (A[0] * K) / 2;
  } else if (S[0] == S[S.length() - 1]) {
    llong a = A[0];
    llong b = A[A.size() - 1];
    A[0] += A[A.size() - 1];
    A.pop_back();
    for (int i = 0; i < A.size(); i++) {
      ANS += A[i] / 2;
    }
    ANS *= K;
    ANS -= A[0] / 2;
    ANS += a / 2 + b / 2;

  } else {
    for (int i = 0; i < A.size(); i++) {
      ANS += A[i] / 2;
    }
    ANS *= K;
  }
  cout << ANS << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <ios>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define itn int
#define retrun return
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
using namespace std;
typedef long long llong;
llong gcd(llong a, llong b) {
  if (a < b)
    std::swap(a, b);
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
llong lcm(llong a, llong b) {
  llong g = gcd(a, b);
  return a * b / g;
}
// ここまで共通
string S;
llong K;
llong ANS = 0;
vector<llong> A;
int main() {
  cin >> S;
  cin >> K;

  llong tmp = 1;
  if (S.length() == 1) {
    A.push_back(1);
  }
  for (int i = 1; i < S.length(); i++) {
    if (S[i - 1] == S[i]) {
      tmp++;
      if (i == S.length() - 1) {
        A.push_back(tmp);
      }
    } else {
      A.push_back(tmp);
      tmp = 1;
    }
  }
  if (A.size() == 1) {
    ANS = (A[0] * K) / 2;
  } else if (S[0] == S[S.length() - 1]) {
    llong a = A[0];
    llong b = A[A.size() - 1];
    A[0] += A[A.size() - 1];
    A.pop_back();
    for (int i = 0; i < A.size(); i++) {
      ANS += A[i] / 2;
    }
    ANS *= K;
    ANS -= A[0] / 2;
    ANS += a / 2 + b / 2;

  } else {
    for (int i = 0; i < A.size(); i++) {
      ANS += A[i] / 2;
    }
    ANS *= K;
  }
  cout << ANS << endl;
  return 0;
}
Problem p02891 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
int main() {
  std::string s[3];
  long long k, a[3] = {};
  std::cin >> s[0] >> k;
  for (size_t i = 0; i < 2; ++i)
    s[i + 1] = s[i] + s[0];
  for (size_t i = 0; i < 3; ++i)
    for (size_t j = 1; j < s[i].length(); ++j)
      if (s[i][j] == s[i][j - 1])
        ++a[i], ++j;
  std::cout << (k & 1 ? a[0] : a[1]) + (k - 1) / 2 * (a[2] - a[0]) << std::endl;
  assert(a[0] + a[1] == a[2]);
}


Fixed submission:
#include <bits/stdc++.h>
int main() {
  std::string s[3];
  long long k, a[3] = {};
  std::cin >> s[0] >> k;
  for (size_t i = 0; i < 2; ++i)
    s[i + 1] = s[i] + s[0];
  for (size_t i = 0; i < 3; ++i)
    for (size_t j = 1; j < s[i].length(); ++j)
      if (s[i][j] == s[i][j - 1])
        ++a[i], ++j;
  std::cout << (k & 1 ? a[0] : a[1]) + (k - 1) / 2 * (a[2] - a[0]) << std::endl;
  assert(a[0] + a[1] <= a[2]);
}
Problem p02891 (C++)

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

int main() {
  string S;
  ll K, ans, l = 1;
  cin >> S >> K;
  vector<pair<char, ll>> v;
  for (ll i = 0; i < S.length(); i++) {
    if (i == 0)
      continue;
    if (S[i] == S[i - 1])
      l++;
    else {
      v.push_back(make_pair(S[i - 1], l));
      l = 1;
    }
    if (i == S.length() - 1)
      v.push_back(make_pair(S[i], l));
  }

  ans = 0;

  if (v.size() == 1)
    ans = v[0].second * K / 2;
  else {
    for (ll i = 1; i < v.size() - 1; i++) {
      ans += v[i].second / 2 * K;
    }
    if (v[v.size() - 1].first == v[0].first) {
      ans += (v[0].second + v[v.size() - 1].second) / 2 * (K - 1);
      ans += v[v.size() - 1].second / 2;
      ans += v[0].second / 2;
    } else {
      ans += v[v.size() - 1].second / 2 * K;
      ans += v[0].second / 2 * K;
    }
  }

  cout << ans << endl;
}

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

int main() {
  string S;
  ll K, ans, l = 1;
  cin >> S >> K;
  vector<pair<char, ll>> v;
  for (ll i = 0; i < S.length(); i++) {
    if (i == 0)
      continue;
    if (S[i] == S[i - 1])
      l++;
    else {
      v.push_back(make_pair(S[i - 1], l));
      l = 1;
    }
    if (i == S.length() - 1)
      v.push_back(make_pair(S[i], l));
  }
  if (S.length() == 1)
    v.push_back(make_pair(S[0], 1));

  ans = 0;

  if (v.size() == 1)
    ans = v[0].second * K / 2;
  else {
    for (ll i = 1; i < v.size() - 1; i++) {
      ans += v[i].second / 2 * K;
    }
    if (v[v.size() - 1].first == v[0].first) {
      ans += (v[0].second + v[v.size() - 1].second) / 2 * (K - 1);
      ans += v[v.size() - 1].second / 2;
      ans += v[0].second / 2;
    } else {
      ans += v[v.size() - 1].second / 2 * K;
      ans += v[0].second / 2 * K;
    }
  }

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

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

using namespace std;

char d[100004];
long long k, cnt = 0;
vector<long long> v;

int main() {
  scanf("%s", d);
  scanf("%lld", &k);
  char bef = d[0];
  int cc = 1;
  for (long long i = 1; i < strlen(d); i++) {
    if (bef == d[i])
      cc++;
    else {
      v.push_back(cc);
      cc = 1;
      bef = d[i];
    }
  }
  if (cc != 1)
    v.push_back(cc);
  /* for (int i=0; i<v.size(); i++) printf("%d ",v[i]);
  printf("\n"); */
  if (v.size() == 1) {
    printf("%lld", (v[0] * k) / 2);
  } else if (d[strlen(d) - 1] == d[0] && k > 1) {
    for (long long i = 1; i < v.size() - 1; i++)
      cnt += v[i] / 2;
    cnt = cnt * k;
    cnt += (v[0] + v[v.size() - 1]) / 2 * (k - 1);
    cnt += v[0] / 2 + v[v.size() - 1] / 2;
    printf("%lld", cnt);
  } else {
    for (int i = 0; i < v.size(); i++)
      cnt += v[i] / 2;
    printf("%lld", cnt * k);
  }
  return 0;
}

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

using namespace std;

char d[100004];
long long k, cnt = 0;
vector<long long> v;

int main() {
  scanf("%s", d);
  scanf("%lld", &k);
  char bef = d[0];
  int cc = 1;
  for (long long i = 1; i < strlen(d); i++) {
    if (bef == d[i])
      cc++;
    else {
      v.push_back(cc);
      cc = 1;
      bef = d[i];
    }
  }
  if (cc != 1)
    v.push_back(cc);
  /* for (int i=0; i<v.size(); i++) printf("%d ",v[i]);
  printf("\n"); */
  if (v.size() == 1) {
    printf("%lld", (v[0] * k) / 2);
  } else if (v.size() == 0 && strlen(d) > 1) {
    if (d[0] == d[strlen(d) - 1])
      printf("%lld", k - 1);
    else
      printf("0");
  } else if (v.size() == 0 && strlen(d) == 1) {
    printf("%lld", k / 2);
  } else if (d[strlen(d) - 1] == d[0] && k > 1) {
    for (long long i = 1; i < v.size() - 1; i++)
      cnt += v[i] / 2;
    cnt = cnt * k;
    cnt += (v[0] + v[v.size() - 1]) / 2 * (k - 1);
    cnt += v[0] / 2 + v[v.size() - 1] / 2;
    printf("%lld", cnt);
  } else {
    for (int i = 0; i < v.size(); i++)
      cnt += v[i] / 2;
    printf("%lld", cnt * k);
  }
  return 0;
}
Problem p02891 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

using ll = long long;
using ld = long double;
const int INF = 1e9;
const double EPS = 1e-9;
const ll MOD = 1e9 + 7;

int main() {
  std::string S;
  std::cin >> S;
  long long K;
  scanf("%lld", &K);
  vector<pair<char, int>> v;
  int sum = 0;
  for (int i = 0; i < S.size() - 1; ++i) {
    if (S[i] == S[i + 1]) {
      sum++;
    } else {
      v.emplace_back(S[i], sum + 1);
      sum = 0;
    }
  }
  if (sum > 0)
    v.emplace_back(S[S.size() - 1], sum + 1);
  ll ans = 0;
  if (v.size() == 1) {
    ans += v[0].second * K / 2;
  } else {
    for (int i = 1; i < v.size() - 1; ++i) {
      ans += v[i].second / 2 * K;
    }
    if (v[0].first == v[v.size() - 1].first) {
      ans += v[0].second / 2 + v[v.size() - 1].second / 2;
      ans += (v[0].second + v[v.size() - 1].second) / 2 * (K - 1);
    } else {
      ans += v[0].second / 2 * K + v[v.size() - 1].second / 2 * K;
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

using ll = long long;
using ld = long double;
const int INF = 1e9;
const double EPS = 1e-9;
const ll MOD = 1e9 + 7;

int main() {
  std::string S;
  std::cin >> S;
  long long K;
  scanf("%lld", &K);
  vector<pair<char, int>> v;
  int sum = 0;
  for (int i = 0; i < S.size() - 1; ++i) {
    if (S[i] == S[i + 1]) {
      sum++;
    } else {
      v.emplace_back(S[i], sum + 1);
      sum = 0;
    }
  }
  if (sum > 0)
    v.emplace_back(S[S.size() - 1], sum + 1);
  ll ans = 0;
  if (v.empty())
    ans = K / 2;
  else if (v.size() == 1) {
    ans += v[0].second * K / 2;
  } else {
    for (int i = 1; i < v.size() - 1; ++i) {
      ans += v[i].second / 2 * K;
    }
    if (v[0].first == v[v.size() - 1].first) {
      ans += v[0].second / 2 + v[v.size() - 1].second / 2;
      ans += (v[0].second + v[v.size() - 1].second) / 2 * (K - 1);
    } else {
      ans += v[0].second / 2 * K + v[v.size() - 1].second / 2 * K;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02891 (C++)

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

template <class T> inline void read(T &x) {
  x = 0;
  char c = getchar();
  bool f = 0;
  for (; !isdigit(c); c = getchar())
    f ^= c == '-';
  for (; isdigit(c); c = getchar())
    x = x * 10 + (c ^ 48);
  x = f ? -x : x;
}

template <class T> inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  T y = 1;
  int len = 1;
  for (; y <= x / 10; y *= 10)
    ++len;
  for (; len; --len, x %= y, y /= 10)
    putchar(x / y + 48);
}

string s;
int k, len = 1;
LL ans;

signed main() {
  freopen("data.in", "r", stdin);
  freopen("my.out", "w", stdout);
  cin >> s;
  read(k);
  if (k == 1 || s[0] != s[(int)s.size() - 1]) {
    for (int i = 1; i < (int)s.size(); ++i)
      if (s[i] == s[i - 1])
        ++len;
      else {
        ans += len / 2;
        len = 1;
      }
    ans += len / 2;
    write(ans * k);
    putchar('\n');
  } else {
    int l, r;
    for (l = 1; s[l] == s[l - 1] && l < (int)s.size(); ++l)
      ;
    for (r = s.size() - 1; s[r] == s[r - 1] && r; --r)
      ;
    if (l >= r) {
      write((int)s.size() * k / 2);
      putchar('\n');
    } else {
      for (int i = l + 1; i < r; ++i)
        if (s[i] == s[i - 1])
          ++len;
        else {
          ans += len / 2;
          len = 1;
        }
      ans += len / 2;
      write(ans * k + l / 2 + ((int)s.size() - r) / 2 +
            ((int)s.size() - r + l) / 2 * (k - 1));
      putchar('\n');
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long LL;

template <class T> inline void read(T &x) {
  x = 0;
  char c = getchar();
  bool f = 0;
  for (; !isdigit(c); c = getchar())
    f ^= c == '-';
  for (; isdigit(c); c = getchar())
    x = x * 10 + (c ^ 48);
  x = f ? -x : x;
}

template <class T> inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  T y = 1;
  int len = 1;
  for (; y <= x / 10; y *= 10)
    ++len;
  for (; len; --len, x %= y, y /= 10)
    putchar(x / y + 48);
}

string s;
int k, len = 1;
LL ans;

signed main() {
  cin >> s;
  read(k);
  if (k == 1 || s[0] != s[(int)s.size() - 1]) {
    for (int i = 1; i < (int)s.size(); ++i)
      if (s[i] == s[i - 1])
        ++len;
      else {
        ans += len / 2;
        len = 1;
      }
    ans += len / 2;
    write(ans * k);
    putchar('\n');
  } else {
    int l, r;
    for (l = 1; s[l] == s[l - 1] && l < (int)s.size(); ++l)
      ;
    for (r = s.size() - 1; s[r] == s[r - 1] && r; --r)
      ;
    if (l >= r) {
      write((int)s.size() * k / 2);
      putchar('\n');
    } else {
      for (int i = l + 1; i < r; ++i)
        if (s[i] == s[i - 1])
          ++len;
        else {
          ans += len / 2;
          len = 1;
        }
      ans += len / 2;
      write(ans * k + l / 2 + ((int)s.size() - r) / 2 +
            ((int)s.size() - r + l) / 2 * (k - 1));
      putchar('\n');
    }
  }
  return 0;
}
Problem p02891 (C++)

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

int main() {
  string s, t, u;
  cin >> s;
  t = s + s;
  u = s + s + s;
  int64_t K, count1 = 0, count2 = 0, N, count3 = 0;
  cin >> K;
  N = s.size();
  if (N == 1) {
    cout << K / 2 << endl;
    return 0;
  }
  for (int i = 0; i < N - 1; i++) {
    if (s.at(i) == s.at(i + 1)) {
      count1++;
      i++;
    }
  }
  for (int i = 0; i < 2 * N - 1; i++) {
    if (t.at(i) == t.at(i + 1)) {
      count2++;
      i++;
    }
  }
  for (int i = 0; i < 3 * N - 1; i++) {
    if (u.at(i) == u.at(i + 1)) {
      count3++;
      i++;
    }
  }
  if (2 * count2 - count1 == count3) {
    cout << (count2 - count1) * (K - 1) + count1 << endl;
  } else {
    return 1;
  }
}


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

int main() {
  string s, t, u;
  cin >> s;
  t = s + s;
  u = s + s + s;
  int64_t K, count1 = 0, count2 = 0, N, count3 = 0;
  cin >> K;
  N = s.size();
  if (N == 1) {
    cout << K / 2 << endl;
    return 0;
  }
  for (int i = 0; i < N - 1; i++) {
    if (s.at(i) == s.at(i + 1)) {
      count1++;
      i++;
    }
  }
  for (int i = 0; i < 2 * N - 1; i++) {
    if (t.at(i) == t.at(i + 1)) {
      count2++;
      i++;
    }
  }
  for (int i = 0; i < 3 * N - 1; i++) {
    if (u.at(i) == u.at(i + 1)) {
      count3++;
      i++;
    }
  }
  if (2 * count2 - count1 == count3) {
    cout << (count2 - count1) * (K - 1) + count1 << endl;
  } else {
    int a = count2 - count1, b = count3 - count2;
    if (K % 2 == 0) {
      cout << count1 + K / 2 * a + (K / 2 - 1) * b << endl;
    } else {
      cout << count1 + K / 2 * (a + b) << endl;
    }
  }
}
Problem p02891 (C++)

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

int main() {
  string S;
  long long K;
  cin >> S >> K;
  string SS = S + S;
  vector<int> X(109, 0);
  long long flag = -1;
  long long x = 1;
  long long point = 0;
  for (int i = 1; i < SS.size(); i++) {
    if (SS[i - 1] == SS[i]) {
      x += 1;
      if (i == S.size()) {
        flag = point;
      }

    }

    else {
      X[point] = x;
      point += 1;
      x = 1;
    }
    if (i == SS.size() - 1) {
      X[point] = x;
    }
  }
  long long ans = 0;
  if (flag > 0) {
    ans += X[0] / 2;
    for (int i = 1; i < X.size() - 1; i++) {
      if (X[i + 1] == 0) {
        ans += X[i] / 2;
        break;
      } else if (i < flag) {
        ans += (X[i] / 2) * K;
      }

      else if (i == flag) {
        ans += (X[i] / 2) * (K - 1);
      } else {
        continue;
      }
    }
  } else if (flag == 0) {
    ans = S.size() * K / 2;
  } else {
    for (int i = 0; i < X.size(); i++) {
      if (X[i] == 0) {
        break;
      } else {
        ans += (X[i] / 2) * K;
      }
    }
    ans /= 2;
  }

  cout << ans << endl;
}


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

int main() {
  string S;
  long long K;
  cin >> S >> K;
  string SS = S + S;
  vector<int> X(210, 0);
  long long flag = -1;
  long long x = 1;
  long long point = 0;
  for (int i = 1; i < SS.size(); i++) {
    if (SS[i - 1] == SS[i]) {
      x += 1;
      if (i == S.size()) {
        flag = point;
      }

    }

    else {
      X[point] = x;
      point += 1;
      x = 1;
    }
    if (i == SS.size() - 1) {
      X[point] = x;
    }
  }
  long long ans = 0;
  if (flag > 0) {
    ans += X[0] / 2;
    for (int i = 1; i < X.size() - 1; i++) {
      if (X[i + 1] == 0) {
        ans += X[i] / 2;
        break;
      } else if (i < flag) {
        ans += (X[i] / 2) * K;
      }

      else if (i == flag) {
        ans += (X[i] / 2) * (K - 1);
      } else {
        continue;
      }
    }
  } else if (flag == 0) {
    ans = S.size() * K / 2;
  } else {
    for (int i = 0; i < X.size(); i++) {
      if (X[i] == 0) {
        break;
      } else {
        ans += (X[i] / 2) * K;
      }
    }
    ans /= 2;
  }

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

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

int main() {
  string s;
  ll k;
  cin >> s >> k;
  char prev = s[0];
  ll cnt = 1;
  vector<ll> n;
  for (ll i = 1; i < s.size(); i++) {
    if (s[i] == prev) {
      cnt++;
      if (i == s.size() - 1)
        n.push_back(cnt);
    } else {
      n.push_back(cnt);
      cnt = 1;
      if (i == s.size() - 1)
        n.push_back(cnt);
    }
    prev = s[i];
  }
  bool c = (s[0] == s[s.size() - 1]);
  ll ans = 0;
  if (c) {
    if (n.size() == 1) {
      cout << n[0] * k / 2 << endl;
    } else {
      ll aida = n[0] + n[n.size() - 1];
      ans += (aida / 2) * (k - 1);
      ans += n[0] / 2;
      ans += n[n.size() - 1] / 2;
      for (ll i = 1; i < n.size() - 1; i++) {
        ans += (n[i] / 2) * k;
      }
      cout << ans << endl;
    }
  } else {
    rep(i, n.size()) { ans += n[i] / 2; }
    cout << ans * k << endl;
  }
}


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

int main() {
  string s;
  ll k;
  cin >> s >> k;
  if (s.size() == 1) {
    cout << k / 2 << endl;
    return 0;
  }
  char prev = s[0];
  ll cnt = 1;
  vector<ll> n;
  for (ll i = 1; i < s.size(); i++) {
    if (s[i] == prev) {
      cnt++;
      if (i == s.size() - 1)
        n.push_back(cnt);
    } else {
      n.push_back(cnt);
      cnt = 1;
      if (i == s.size() - 1)
        n.push_back(cnt);
    }
    prev = s[i];
  }
  bool c = (s[0] == s[s.size() - 1]);
  ll ans = 0;
  if (c) {
    if (n.size() == 1) {
      cout << n[0] * k / 2 << endl;
    } else {
      ll aida = n[0] + n[n.size() - 1];
      ans += (aida / 2) * (k - 1);
      ans += n[0] / 2;
      ans += n[n.size() - 1] / 2;
      for (ll i = 1; i < n.size() - 1; i++) {
        ans += (n[i] / 2) * k;
      }
      cout << ans << endl;
    }
  } else {
    rep(i, n.size()) { ans += n[i] / 2; }
    cout << ans * k << endl;
  }
}
Problem p02892 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define erep(i, a, n) for (int i = a; i <= n; i++)
typedef long long ll;
#define int long long
#define vint vector<int>
#define vvint vector<vector<int>>
#define vstring vector<string>
#define vdouble vector<double>
#define vll vector<ll>:
#define vbool vector<bool>
#define INF 1101010101010101010
#define MOD 1000000007
#define P pair<int, int>
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int color[300];
vvint to(300);

bool dfs(int x, int c) {
  color[x] = c;
  for (auto p : to[x]) {
    if (color[p] == c)
      return false;
    if (color[p] == 0 && !dfs(p, -c))
      return false;
  }
  return true;
}

signed main() {
  int n;
  cin >> n;
  vstring mass(n);
  rep(i, 0, n) rep(j, 0, n) cin >> mass[i][j];
  vvint dist(n, vint(n));
  rep(i, 0, n) {
    rep(j, 0, n) {
      if (i != j)
        dist[i][j] = INF;
      if (mass[i][j] == '1') {
        to[i].push_back(j);
        dist[i][j] = 1;
      }
    }
  }
  if (!dfs(0, 1)) {
    cout << -1 << endl;
    return 0;
  }
  rep(i, 0, n) {
    rep(j, 0, n) {
      rep(s, 0, n) { dist[j][s] = min(dist[j][s], dist[j][i] + dist[i][s]); }
    }
  }
  int ans = 0;
  rep(i, 0, n) rep(j, 0, n) ans = max(ans, dist[i][j]);
  cout << ans + 1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define erep(i, a, n) for (int i = a; i <= n; i++)
typedef long long ll;
#define int long long
#define vint vector<int>
#define vvint vector<vector<int>>
#define vstring vector<string>
#define vdouble vector<double>
#define vll vector<ll>:
#define vbool vector<bool>
#define INF 1101010101010101010
#define MOD 1000000007
#define P pair<int, int>
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int color[300];
vvint to(300);

bool dfs(int x, int c) {
  color[x] = c;
  for (auto p : to[x]) {
    if (color[p] == c)
      return false;
    if (color[p] == 0 && !dfs(p, -c))
      return false;
  }
  return true;
}

signed main() {
  int n;
  cin >> n;
  vstring mass(n);
  rep(i, 0, n) cin >> mass[i];
  vvint dist(n, vint(n));
  rep(i, 0, n) {
    rep(j, 0, n) {
      if (i != j)
        dist[i][j] = INF;
      if (mass[i][j] == '1') {
        to[i].push_back(j);
        dist[i][j] = 1;
      }
    }
  }
  if (!dfs(0, 1)) {
    cout << -1 << endl;
    return 0;
  }
  rep(i, 0, n) {
    rep(j, 0, n) {
      rep(s, 0, n) { dist[j][s] = min(dist[j][s], dist[j][i] + dist[i][s]); }
    }
  }
  int ans = 0;
  rep(i, 0, n) rep(j, 0, n) ans = max(ans, dist[i][j]);
  cout << ans + 1 << endl;
}