Problem:
Score : 900 points

Problem Statement

Takahashi has a lot of peculiar devices.
These cylindrical devices receive balls from left and right.
Each device is in one of the two states A and B, and for each state, the device operates as follows:

When a device in state A receives a ball from either side (left or right), the device throws out the ball from the same side, then immediately goes into state B.

When a device in state B receives a ball from either side, the device throws out the ball from the other side, then immediately goes into state A.

The transition of the state of a device happens momentarily and always completes before it receives another ball.

Takahashi built a contraption by concatenating N of these devices. In this contraption,

A ball that was thrown out from the right side of the i-th device from the left (1 \leq i \leq N-1) immediately enters the (i+1)-th device from the left side.

A ball that was thrown out from the left side of the i-th device from the left (2 \leq i \leq N) immediately enters the (i-1)-th device from the right side.

The initial state of the i-th device from the left is represented by the i-th character in a string S.
From this situation, Takahashi performed the following K times: put a ball into the leftmost device from the left side, then wait until the ball comes out of the contraption from either end.
Here, it can be proved that the ball always comes out of the contraption after a finite time.
Find the state of each device after K balls are processed.

Constraints

1 \leq N \leq 200,000

1 \leq K \leq 10^9

|S|=N

Each character in S is either A or B.

Input

The input is given from Standard Input in the following format:

N K
S

Output

Print a string that represents the state of each device after K balls are processed.
The string must be N characters long, and the i-th character must correspond to the state of the i-th device from the left.

Sample Input 1

5 1
ABAAA

Sample Output 1

BBAAA

In this input, we put a ball into the leftmost device from the left side, then it is returned from the same place.

Sample Input 2

5 2
ABAAA

Sample Output 2

ABBBA

Sample Input 3

4 123456789
AABB

Sample Output 3

BABA

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
#include <set>

using namespace std;

#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

typedef long long ll;
typedef long double ld;

ll n, k;
vector<char> vec;
vector<char> need;

void input() {
	cin >> n >> k;
	vec.resize(n);
	for (auto &cur : vec) cin >> cur;

	ll cur = 1;
	if (n % 2 == 0) cur = 0;
	need.resize(n);
	for (int i = 0; i < vec.size(); i++) {
		if (cur) {
			need[i] = 'A';
		}
		else {
			need[i] = 'B';
		}
		cur = !cur;
	}
}

void play2() {
	if (vec[0] == 'A') {
		vec[0] = 'B';
		return;
	}

	if (vec.back() == 'B') {
		vec.back() = 'A';
	}
	for (int i = 1; i < vec.size(); i++) {
		if (vec[i - 1] == vec[i]) {
			if (vec[i - 1] == 'A') {
				vec[i - 1] = 'B';
			}
			else {
				vec[i - 1] = 'A';
			}
		}
	}
}

void play() {
	ll x = 0, side = 1;
	while (x >= 0 && x < n) {
		if (side == 1) {
			if (vec[x] == 'A') {
				side = 0;
				vec[x] = 'B';
				x--;
			}
			else {
				side = 1;
				vec[x] = 'A';
				x++;
			}
			
		}
		else {
			if (vec[x] == 'A') {
				side = 1;
				vec[x] = 'B';
				x++;
			}
			else {
				side = 0;
				vec[x] = 'A';
				x--;
			}
		}
	}

	for (auto cur : vec) cout << cur;
	if (x < 0) {
		cout << " front";
	}
	else {
		cout << " back";
	}
	cout << "\n";
}

void rand_test() {
	n = rand() % 1000 + 1, k = rand() % 10000;
	vec.resize(n);
	for (int i = 0; i < n; i++) {
		vec[i] = ('A' + rand() % 2);
	}

	ll cur = 1;
	if (n % 2 == 0) cur = 0;
	need.resize(n);
	for (int i = 0; i < vec.size(); i++) {
		if (cur) {
			need[i] = 'A';
		}
		else {
			need[i] = 'B';
		}
		cur = !cur;
	}
}

ll find_k() {
	ll cnt = 0;
	while (need != vec) {
		cnt++;
		play();
	}

	return cnt;
}

int main() {
	fastInp;

	input();
	
	while (k > 0) {
		k--;
		play2();
		if (vec == need) {
			k %= 2;
		}
	}

	for (auto cur : vec) cout << cur;
	return 0;
	
	ll t = 1000;
	while (t--) {
		rand_test();
		vector<char> sv = vec;
		cout << n << " " << find_k() << "\n";
		for (auto cur : sv) cout << cur;
		cout << "\n";
		cout << "-----------------------------\n";
	}

	return 0;
}