Language: C++

#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("unswitch-loops")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("section-anchors")
#pragma GCC optimize("profile-reorder-functions")
#pragma GCC optimize("profile-values")
#pragma GCC optimize("tracer")
#pragma GCC optimize("vpt")
#pragma GCC optimize("rename-registers")
#pragma GCC optimize("move-loop-invariants")
#pragma GCC optimize("function-sections")
#pragma GCC optimize("data-sections")
#pragma GCC optimize("branch-target-load-optimize")
#pragma GCC optimize("branch-target-load-optimize2")
#pragma GCC optimize("btr-bb-exclusive")
#pragma GCC target("sse2")
#pragma GCC target("sse3")
#pragma GCC target("ssse3")
#pragma GCC target("sse4.1")
#pragma GCC target("sse4.2")
#pragma GCC target("avx")
#pragma GCC target("avx2")
#pragma GCC target("popcnt")
#pragma GCC target("abm")
#pragma GCC target("mmx")
#pragma GCC target("tune=native")
#include<bits/stdc++.h>
using namespace std;
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC

const int N = 2e5 + 7;
bitset <N> a, inv;

int n, k;
void print() {
    for (int i = 0; i < n; ++i) {
        if (a[i])
            cout << 'A';
        else
            cout << 'B';
    }   
    cout << endl;
}

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    cin >> n >> k;
    string s;
    cin >> s;
    for (int i = 0; i < n; ++i)
        a[i] = s[i] == 'A';


    if (n == 1) {
        a[0] = a[0] ^ (k & 1);
        print();
        exit(0);
    }   

    while (!a[n - 1] && k) {
        --k;
        if (a[0]) {
            a[0] = a[0] ^ 1;
        }   
        else {
            for (int i = 0; i + 1 < n; ++i) {
                if (a[i] == a[i + 1]) {
                    a[i] = a[i] ^ 1;
                }   
            }   
            a[n - 1] = 1;
        }   
    }        
    
    if (k == 0) {
        print();
        exit(0);
    }
    
    for (int i = 0; i < n - 1; ++i)
        inv[i] = 1;

    for (int i = 0; i < 2 * n && k; ++i) {
        --k;
        if (a[0]) {
            a[0] = a[0] ^ 1;
        }   
        else {            
            a ^= (a ^ (a >> 1) ^ inv) & inv;
        }   
    }   
    if (a[0] || (a[0] == a[1])) {
        a[0] = a[0] ^ (k & 1);
    }   
    print();
}


Compiler output:
a.cc:6:39: error: section anchors must be disabled when toplevel reorder is disabled
    6 | #pragma GCC optimize("section-anchors")
      |                                       ^
a.cc:8:38: warning: bad option '-fprofile-values' to pragma 'optimize' [-Wpragmas]
    8 | #pragma GCC optimize("profile-values")
      |                                      ^
a.cc:13:41: warning: bad option '-ffunction-sections' to pragma 'optimize' [-Wpragmas]
   13 | #pragma GCC optimize("function-sections")
      |                                         ^
a.cc:14:37: warning: bad option '-fdata-sections' to pragma 'optimize' [-Wpragmas]
   14 | #pragma GCC optimize("data-sections")
      |                                     ^
a.cc:15:51: warning: bad option '-fbranch-target-load-optimize' to pragma 'optimize' [-Wpragmas]
   15 | #pragma GCC optimize("branch-target-load-optimize")
      |                                                   ^
a.cc:16:52: warning: bad option '-fbranch-target-load-optimize2' to pragma 'optimize' [-Wpragmas]
   16 | #pragma GCC optimize("branch-target-load-optimize2")
      |                                                    ^
a.cc:17:40: warning: bad option '-fbtr-bb-exclusive' to pragma 'optimize' [-Wpragmas]
   17 | #pragma GCC optimize("btr-bb-exclusive")
      |                                        ^
In file included from /usr/include/c++/14/cassert:43,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:33,
                 from a.cc:29:
/usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h:321:29: error: section anchors must be disabled when toplevel reorder is disabled
  321 |   inline void __terminate() _GLIBCXX_USE_NOEXCEPT
      |                             ^~~~~~~~~~~~~~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h: In function 'void std::__terminate()':
/usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h:323:82: error: section anchors must be disabled when toplevel reorder is disabled
  323 |     void terminate() _GLIBCXX_USE_NOEXCEPT __attribute__ ((__noreturn__,__cold__));
      |                                                                                  ^
/usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h:323:82: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h: At global scope:
/usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h:543:29: error: section anchors must be disabled when toplevel reorder is disabled
  543 |   __is_constant_evaluated() _GLIBCXX_NOEXCEPT
      |                             ^~~~~~~~~~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h:582:3: error: section anchors must be disabled when toplevel reorder is disabled
  582 |   _GLIBCXX_NOEXCEPT;
      |   ^~~~~~~~~~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h:582:3: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h:608:25: error: section anchors must be disabled when toplevel reorder is disabled
  608 |   __glibcxx_assert_fail()
      |                         ^
In file included from /usr/include/features.h:523,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/os_defines.h:39,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h:683:
/usr/include/assert.h:71:45: error: section anchors must be disabled when toplevel reorder is disabled
   71 |      __THROW __attribute__ ((__noreturn__)) __COLD;
      |                                             ^~~~~~
/usr/include/assert.h:71:45: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/assert.h:76:45: error: section anchors must be disabled when toplevel reorder is disabled
   76 |      __THROW __attribute__ ((__noreturn__)) __COLD;
      |                                             ^~~~~~
/usr/include/assert.h:76:45: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/assert.h:82:45: error: section anchors must be disabled when toplevel reorder is disabled
   82 |      __THROW __attribute__ ((__noreturn__)) __COLD;
      |                                             ^~~~~~
/usr/include/assert.h:82:45: error: section anchors must be disabled when toplevel reorder is disabled
In file included from /usr/include/c++/14/cctype:42,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:35:
/usr/include/ctype.h:80:40: error: section anchors must be disabled when toplevel reorder is disabled
   80 |      __THROW __attribute__ ((__const__));
      |                                        ^
/usr/include/ctype.h:80:40: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:82:40: error: section anchors must be disabled when toplevel reorder is disabled
   82 |      __THROW __attribute__ ((__const__));
      |                                        ^
/usr/include/ctype.h:82:40: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:84:40: error: section anchors must be disabled when toplevel reorder is disabled
   84 |      __THROW __attribute__ ((__const__));
      |                                        ^
/usr/include/ctype.h:84:40: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:108:1: error: section anchors must be disabled when toplevel reorder is disabled
  108 | __exctype (isalnum);
      | ^~~~~~~~~
/usr/include/ctype.h:108:1: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:109:1: error: section anchors must be disabled when toplevel reorder is disabled
  109 | __exctype (isalpha);
      | ^~~~~~~~~
/usr/include/ctype.h:109:1: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:110:1: error: section anchors must be disabled when toplevel reorder is disabled
  110 | __exctype (iscntrl);
      | ^~~~~~~~~
/usr/include/ctype.h:110:1: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:111:1: error: section anchors must be disabled when toplevel reorder is disabled
  111 | __exctype (isdigit);
      | ^~~~~~~~~
/usr/include/ctype.h:111:1: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:112:1: error: section anchors must be disabled when toplevel reorder is disabled
  112 | __exctype (islower);
      | ^~~~~~~~~
/usr/include/ctype.h:112:1: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:113:1: error: section anchors must be disabled when toplevel reorder is disabled
  113 | __exctype (isgraph);
      | ^~~~~~~~~
/usr/include/ctype.h:113:1: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:114:1: error: section anchors must be disabled when toplevel reorder is disabled
  114 | __exctype (isprint);
      | ^~~~~~~~~
/usr/include/ctype.h:114:1: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:115:1: error: section anchors must be disabled when toplevel reorder is disabled
  115 | __exctype (ispunct);
      | ^~~~~~~~~
/usr/include/ctype.h:115:1: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:116:1: error: section anchors must be disabled when toplevel reorder is disabled
  116 | __exctype (isspace);
      | ^~~~~~~~~
/usr/include/ctype.h:116:1: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:117:1: error: section anchors must be disabled when toplevel reorder is disabled
  117 | __exctype (isupper);
      | ^~~~~~~~~
/usr/include/ctype.h:117:1: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:118:1: error: section anchors must be disabled when toplevel reorder is disabled
  118 | __exctype (isxdigit);
      | ^~~~~~~~~
/usr/include/ctype.h:118:1: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:122:30: error: section anchors must be disabled when toplevel reorder is disabled
  122 | extern int tolower (int __c) __THROW;
      |                              ^~~~~~~
/usr/include/ctype.h:122:30: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:125:30: error: section anchors must be disabled when toplevel reorder is disabled
  125 | extern int toupper (int __c) __THROW;
      |                              ^~~~~~~
/usr/include/ctype.h:125:30: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:130:1: error: section anchors must be disabled when toplevel reorder is disabled
  130 | __exctype (isblank);
      | ^~~~~~~~~
/usr/include/ctype.h:130:1: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:135:42: error: section anchors must be disabled when toplevel reorder is disabled
  135 | extern int isctype (int __c, int __mask) __THROW;
      |                                          ^~~~~~~
/usr/include/ctype.h:135:42: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:142:30: error: section anchors must be disabled when toplevel reorder is disabled
  142 | extern int isascii (int __c) __THROW;
      |                              ^~~~~~~
/usr/include/ctype.h:142:30: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:146:30: error: section anchors must be disabled when toplevel reorder is disabled
  146 | extern int toascii (int __c) __THROW;
      |                              ^~~~~~~
/usr/include/ctype.h:146:30: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:150:1: error: section anchors must be disabled when toplevel reorder is disabled
  150 | __exctype (_toupper);
      | ^~~~~~~~~
/usr/include/ctype.h:150:1: error: section anchors must be disabled when toplevel reorder is disabled
/usr/include/ctype.h:151:1: error: section anchors must be disabled when toplevel reorder