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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.FileNotFoundException;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws FileNotFoundException {
		new Main().run();
	}

	final long MODULO = 924844033L;

	void run() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long K = sc.nextLong();
		String s = sc.next();
		ArrayDeque<Integer> dq = new ArrayDeque<>();
		for (int i = 0; i < s.length(); ++i) {
			dq.add(s.charAt(i) == 'A' ? 1 : -1);
		}
		int cur = 1;
		long res = 0;
		HashMap<String, Integer> map = new HashMap<>();
		HashMap<Integer, String> map2 = new HashMap<>();
		int id = 0;
		for (int i = 0; i < K; ++i) {
			int v = dq.pollFirst();
			if (v * cur == 1) {
				dq.addFirst(cur * (-1));
				continue;
			}
			cur *= -1;
			dq.addLast(cur);
			if (i > 2 * N) {
				res = K - (i + 1);
				String str = getans(dq, cur);
				if (map.containsKey(str)) {
					int ansid = (int) res % id;
					System.out.println(map2.get(ansid));
					return;
				} else {
					map.put(str, id);
					map2.put(id, str);
					++id;
				}
			}
		}

		StringBuilder sb = new StringBuilder();
		while (!dq.isEmpty()) {
			sb.append(dq.pollFirst() * cur == 1 ? 'A' : 'B');
		}
		System.out.println(sb);
	}

	String getans(ArrayDeque<Integer> dq, int cur) {
		StringBuilder sb = new StringBuilder();
		ArrayDeque<Integer> tmp = new ArrayDeque<>();
		tmp.addAll(dq);
		while (!tmp.isEmpty()) {
			sb.append(tmp.pollFirst() * cur == 1 ? 'A' : 'B');
		}
		return sb.toString();

	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}