Language: C++

#define DEBUGGING
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()

template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
using ll = int64_t;
using ull = uint64_t;
using PLL = pair<ll, ll>;

template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return max(t, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
template <typename T> const T& clamp(const T &t, const T &low, const T &high) { return max(low, min(high, t)); }
template <typename T> void chclamp(T &t, const T &low, const T &high) { t = clamp(t, low, high); }

namespace init__ {

struct InitIO {
    InitIO() {
        cin.tie(nullptr);
        ios_base::sync_with_stdio(false);
        cout << fixed << setprecision(30);
    }
} init_io;

}

#ifdef DEBUGGING
// #include "../debug/debug.cpp"
#include "../../debug/debug.cpp"
#else
#define DEBUG(...) 0
#define DEBUG_SEPARATOR_LINE 0
#endif

template <typename T>
T make_v(T init) { return init; }

template <typename T, typename... Tail>
auto make_v(T init, size_t s, Tail... tail) {
#define rec make_v(init, tail...)
    return V<decltype(rec)>(s, rec);
#undef rec
}

ll solve() {
    string S;
    ll K;
    cin >> S >> K;
    for(ll i = 0; i < 26; i++) if(S == string(S.size(), 'a' + i)) return S.size() * K / 2;

    auto calc = [&](ll left, ll right) {
        char pre = S[left];
        ll cnt = 1;
        ll ret = 0;
        for(ll i = left + 1; i < right; i++) {
            if(pre == S[i]) {
                cnt++;
            } else {
                DEBUG(make_tuple(left, right, cnt));
                ret += cnt / 2;
                pre = S[i];
                cnt = 1;
            }
        }
        DEBUG(ret+cnt/2);
        return ret + cnt / 2;
    };

    if(S[0] == S.back()) {
        ll lidx = S.size() - 1, ridx = S.size();
        ll joint = 0;
        string T = S + S;
        while(0 < lidx) {
            if(T[lidx - 1] == S.back()) lidx--;
            else break;
        }
        while(ridx < T.size()) {
            if(T[ridx] == S.back()) ridx++;
            else break;
        }
        joint = (ridx - lidx) / 2;
        ridx -= S.size();

        ll ans = 0;
        DEBUG(joint);

        ans += calc(0, lidx);
        ans += joint * (K - 1);
        ans += calc(ridx, lidx) * (K - 2);
        ans += calc(ridx, S.size());
        return ans;
    } else {
        return calc(0, S.size()) * K;
    }
}

int main() {
    cout << solve() << endl;
    return 0;
}


Compiler output:
a.cc:37:10: fatal error: ../../debug/debug.cpp: No such file or directory
   37 | #include "../../debug/debug.cpp"
      |          ^~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.