CF2156B.Strange Machine

传统题 时间 2000 ms 内存 256 MiB 5 尝试 1 已通过 1 标签

Strange Machine

You are given nn machines arranged in a circle, where nn is at most 2020. Each machine is either of type A or type B. The machines are numbered clockwise from 11 to nn, and the type of the ii-th machine is denoted by sis_i. Each machine takes an integer xx and updates it according to its type:

  • Type A: Decrease xx by 11. Formally, update x:=x1x := x - 1.
  • Type B: Replace xx with the floor of half its value. Formally, update x:=x2x := \left\lfloor\frac{x}{2}\right\rfloor, where y\lfloor y\rfloor denotes the floor of yy, which is the greatest integer less than or equal to yy.

You are given qq queries, each consisting of a single integer aa. In each query, you start at machine 11 holding an integer aa. Each second, the following two actions occur in order:

  1. The current machine updates aa according to its type.
  2. Then, move one step clockwise to the next machine. Formally
    • If you are at machine ii where 1in11 \le i \le n - 1, move to machine i+1i + 1.
    • If you are at machine nn, move to machine 11.

This process continues until your integer aa becomes 00. For each query, determine the number of seconds required for aa to reach 00.

Note that all queries are independent of each other.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and qq (1n201\le n\le 20, 1q1041\le q\le 10^4) — the number of machines, and the number of queries, respectively.

The second line of each test case contains a string ss (s=n|s| = n and si=A or Bs_i = \mathtt{A} \text{ or }\mathtt{B}) — the types of the machines.

The third line of each test case contains qq integers a1,a2,,aqa_1, a_2, \ldots, a_q (1ai1091\le a_i\le 10^9) — the initial integer of each query.

Note that there are no constraints on the sum of nn over all test cases.

It is guaranteed that the sum of qq over all test cases does not exceed 10410^4.

Output

For each test case, output qq integers representing the answers to each query.

Note

In the first test case, the queries are as follows:

  • Query 11: a=3a = 3
    1. Start at machine 11. Machine 11 replaces aa with the floor of half its value. Now, a=32=1a = \left\lfloor\frac{3}{2}\right\rfloor = 1.
    2. Move to machine 22. Machine 22 decreases aa by 11. Now, a=11=0a = 1 - 1 = 0.Therefore, it takes 22 seconds for aa to reach 00.
  • Query 22: a=4a = 4
    1. Start at machine 11. Machine 11 replaces aa with the floor of half its value. Now, a=42=2a = \left\lfloor\frac{4}{2}\right\rfloor = 2.
    2. Move to machine 22. Machine 22 decreases aa by 11. Now, a=21=1a = 2 - 1 = 1.
    3. Move back to machine 11. Machine 11 replaces aa with the floor of half its value. Now, a=12=0a = \left\lfloor\frac{1}{2}\right\rfloor = 0.Therefore, it takes 33 seconds for aa to reach 00.

In the second test case, there is only one query with a=20a = 20:

  1. Start at machine 11. Machine 11 replaces aa with the floor of half its value. Now, a=202=10a = \left\lfloor\frac{20}{2}\right\rfloor = 10.
  2. Move to machine 11. Machine 11 replaces aa with the floor of half its value. Now, a=102=5a = \left\lfloor\frac{10}{2}\right\rfloor = 5.
  3. Move to machine 11. Machine 11 replaces aa with the floor of half its value. Now, a=52=2a = \left\lfloor\frac{5}{2}\right\rfloor = 2.
  4. Move to machine 11. Machine 11 replaces aa with the floor of half its value. Now, a=22=1a = \left\lfloor\frac{2}{2}\right\rfloor = 1.
  5. Move to machine 11. Machine 11 replaces aa with the floor of half its value. Now, a=12=0a = \left\lfloor\frac{1}{2}\right\rfloor = 0.

Therefore, it takes 55 seconds for aa to reach 00.

Samples

3
2 2
BA
3 4
1 1
B
20
6 4
BAABBA
2 8 32 95
2
3
5
2
5
8
11

在线编程 IDE

建议全屏模式获得最佳体验