欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF2156B.Strange Machine
Strange Machine
You are given machines arranged in a circle, where is at most . Each machine is either of type A or type B. The machines are numbered clockwise from to , and the type of the -th machine is denoted by . Each machine takes an integer and updates it according to its type:
- Type
A: Decrease by . Formally, update . - Type
B: Replace with the floor of half its value. Formally, update , where denotes the floor of , which is the greatest integer less than or equal to .
You are given queries, each consisting of a single integer . In each query, you start at machine holding an integer . Each second, the following two actions occur in order:
- The current machine updates according to its type.
- Then, move one step clockwise to the next machine. Formally
- If you are at machine where , move to machine .
- If you are at machine , move to machine .
This process continues until your integer becomes . For each query, determine the number of seconds required for to reach .
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 (). The description of the test cases follows.
The first line of each test case contains two integers and (, ) — the number of machines, and the number of queries, respectively.
The second line of each test case contains a string ( and ) — the types of the machines.
The third line of each test case contains integers () — the initial integer of each query.
Note that there are no constraints on the sum of over all test cases.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output integers representing the answers to each query.
Note
In the first test case, the queries are as follows:
- Query :
- Start at machine . Machine replaces with the floor of half its value. Now, .
- Move to machine . Machine decreases by . Now, .Therefore, it takes seconds for to reach .
- Query :
- Start at machine . Machine replaces with the floor of half its value. Now, .
- Move to machine . Machine decreases by . Now, .
- Move back to machine . Machine replaces with the floor of half its value. Now, .Therefore, it takes seconds for to reach .
In the second test case, there is only one query with :
- Start at machine . Machine replaces with the floor of half its value. Now, .
- Move to machine . Machine replaces with the floor of half its value. Now, .
- Move to machine . Machine replaces with the floor of half its value. Now, .
- Move to machine . Machine replaces with the floor of half its value. Now, .
- Move to machine . Machine replaces with the floor of half its value. Now, .
Therefore, it takes seconds for to reach .
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
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |