Problem p02891 (C++)

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

// Defination {{{
#define PI std::pair<int, int>
#define mk std::make_pair
#define reg register
#define ll long long
#define rep(i, a, b) for (reg int i = a; i <= b; ++i)
#define per(i, a, b) for (reg int i = a; i >= b; --i)
#define pb push_back
#define debug(...) fprintf(stderr, __VA_ARGS__)
// }}}

template <typename T> T max(T a, T b) { return a > b ? a : b; }
template <typename T> T min(T a, T b) { return a < b ? a : b; }

template <typename T> void read(T &x) {
  x = 0;
  reg char ch = getchar();
  reg int f = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ 48);
  x *= f;
}

#define N 105

char s[N << 1];
int k;

int main() {
#ifndef ONLINE_JUDGE
  freopen("A.in", "r", stdin);
  freopen("A.out", "w", stdout);
#endif
  scanf("%s", s + 1);
  read(k);
  int n = strlen(s + 1);
  if (n == 1) {
    printf("%d\n", k / 2);
    return 0;
  }
  if (k == 1) {
    int ans = 0, tot = 0;
    rep(i, 1, n) {
      if (s[i] != s[i - 1] && i != 1) {
        ans += tot / 2;
        tot = 0;
      }
      ++tot;
    }
    ans += tot / 2;
    printf("%d\n", ans);
    return 0;
  }
  int check = 0;
  rep(i, 2, n) if (s[i] != s[i - 1]) check = 1;
  if (!check) {
    printf("%lld\n", 1ll * n * k / 2);
    return 0;
  }
  rep(i, 1, n) s[i + n] = s[i];
  ll ans = 0, tot = 0, res = 0;
  rep(i, 1, n) {
    if (s[i] != s[i - 1] && i != 1) {
      ans += tot / 2;
      tot = 0;
    }
    ++tot;
  }
  int pos = n - tot + 1;
  tot = 0;
  rep(i, pos, n * 2) {
    if (s[i] != s[i - 1] && i != 1) {
      res += tot / 2;
      tot = 0;
    }
    ++tot;
  }
  res = res * (k - 1);
  ans += tot / 2 + res;
  printf("%lld\n", ans);
  return 0;
}

/*
 _____  ____    _____
|__  / |  _ \  |__  /
  / /  | |_) |   / /
 / /_  |  _ <   / /_
/____| |_| \_\ /____|


 _____________________________________
< A.cpp is created by zrz who is weak >
 -------------------------------------
        \   ^__^
         \  (^^)\_______
            (__)\       )\/\
             U ||----w |
                ||     ||

*/


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

// Defination {{{
#define PI std::pair<int, int>
#define mk std::make_pair
#define reg register
#define ll long long
#define rep(i, a, b) for (reg int i = a; i <= b; ++i)
#define per(i, a, b) for (reg int i = a; i >= b; --i)
#define pb push_back
#define debug(...) fprintf(stderr, __VA_ARGS__)
// }}}

template <typename T> T max(T a, T b) { return a > b ? a : b; }
template <typename T> T min(T a, T b) { return a < b ? a : b; }

template <typename T> void read(T &x) {
  x = 0;
  reg char ch = getchar();
  reg int f = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ 48);
  x *= f;
}

#define N 105

char s[N << 1];
int k;

int main() {
#ifndef ONLINE_JUDGE
  // freopen("A.in", "r", stdin);
  // freopen("A.out", "w", stdout);
#endif
  scanf("%s", s + 1);
  read(k);
  int n = strlen(s + 1);
  if (n == 1) {
    printf("%d\n", k / 2);
    return 0;
  }
  if (k == 1) {
    int ans = 0, tot = 0;
    rep(i, 1, n) {
      if (s[i] != s[i - 1] && i != 1) {
        ans += tot / 2;
        tot = 0;
      }
      ++tot;
    }
    ans += tot / 2;
    printf("%d\n", ans);
    return 0;
  }
  int check = 0;
  rep(i, 2, n) if (s[i] != s[i - 1]) check = 1;
  if (!check) {
    printf("%lld\n", 1ll * n * k / 2);
    return 0;
  }
  rep(i, 1, n) s[i + n] = s[i];
  ll ans = 0, tot = 0, res = 0;
  rep(i, 1, n) {
    if (s[i] != s[i - 1] && i != 1) {
      ans += tot / 2;
      tot = 0;
    }
    ++tot;
  }
  int pos = n - tot + 1;
  tot = 0;
  rep(i, pos, n * 2) {
    if (s[i] != s[i - 1] && i != 1) {
      res += tot / 2;
      tot = 0;
    }
    ++tot;
  }
  res = res * (k - 1);
  ans += tot / 2 + res;
  printf("%lld\n", ans);
  return 0;
}

/*
 _____  ____    _____
|__  / |  _ \  |__  /
  / /  | |_) |   / /
 / /_  |  _ <   / /_
/____| |_| \_\ /____|


 _____________________________________
< A.cpp is created by zrz who is weak >
 -------------------------------------
        \   ^__^
         \  (^^)\_______
            (__)\       )\/\
             U ||----w |
                ||     ||

*/