Codeforces Round 1061 (Div. 2)
B
Strange Machine
Codeforces
1000
原题
← AC →
翻译中…

You are given $n$ machines arranged in a circle, where $n$ is at most $20$. Each machine is either of type A or type B. The machines are numbered clockwise from $1$ to $n$, and the type of the $i$-th machine is denoted by $s_i$. Each machine takes an integer $x$ and updates it according to its type:

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

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

翻译中…
  1. The current machine updates $a$ according to its type.
  2. Then, move one step clockwise to the next machine. Formally
  • If you are at machine $i$ where $1 \le i \le n - 1$, move to machine $i + 1$.
    • If you are at machine $n$, move to machine $1$.
翻译中…

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

翻译中…

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 $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $q$ ($1\le n\le 20$, $1\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 $s$ ($|s| = n$ and $s_i = \mathtt{A} \text{ or }\mathtt{B}$) — the types of the machines.

翻译中…

The third line of each test case contains $q$ integers $a_1, a_2, \ldots, a_q$ ($1\le a_i\le 10^9$) — the initial integer of each query.

翻译中…

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

翻译中…

It is guaranteed that the sum of $q$ over all test cases does not exceed $10^4$.

翻译中…

Output

翻译中…

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

翻译中…

Note

翻译中…

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

翻译中…
  • Query $1$: $a = 3$ Therefore, it takes $2$ seconds for $a$ to reach $0$.
  • Start at machine $1$. Machine $1$ replaces $a$ with the floor of half its value. Now, $a = \left\lfloor\frac{3}{2}\right\rfloor = 1$.
    • Move to machine $2$. Machine $2$ decreases $a$ by $1$. Now, $a = 1 - 1 = 0$.
  • Query $2$: $a = 4$ Therefore, it takes $3$ seconds for $a$ to reach $0$.
  • Start at machine $1$. Machine $1$ replaces $a$ with the floor of half its value. Now, $a = \left\lfloor\frac{4}{2}\right\rfloor = 2$.
    • Move to machine $2$. Machine $2$ decreases $a$ by $1$. Now, $a = 2 - 1 = 1$.
    • Move back to machine $1$. Machine $1$ replaces $a$ with the floor of half its value. Now, $a = \left\lfloor\frac{1}{2}\right\rfloor = 0$.
翻译中…

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

翻译中…
  1. Start at machine $1$. Machine $1$ replaces $a$ with the floor of half its value. Now, $a = \left\lfloor\frac{20}{2}\right\rfloor = 10$.
  2. Move to machine $1$. Machine $1$ replaces $a$ with the floor of half its value. Now, $a = \left\lfloor\frac{10}{2}\right\rfloor = 5$.
  3. Move to machine $1$. Machine $1$ replaces $a$ with the floor of half its value. Now, $a = \left\lfloor\frac{5}{2}\right\rfloor = 2$.
  4. Move to machine $1$. Machine $1$ replaces $a$ with the floor of half its value. Now, $a = \left\lfloor\frac{2}{2}\right\rfloor = 1$.
  5. Move to machine $1$. Machine $1$ replaces $a$ with the floor of half its value. Now, $a = \left\lfloor\frac{1}{2}\right\rfloor = 0$.
翻译中…

Therefore, it takes $5$ seconds for $a$ to reach $0$.

翻译中…

样例

Input #1
3
2 2
BA
3 4
1 1
B
20
6 4
BAABBA
2 8 32 95
Output #1
2
3
5
2
5
8
11
InputCopy OutputCopy
题目信息
题号
2156B
时间限制
1.5 s
内存限制
256 MB
标签
二分
暴力
贪心
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。