#P14044. [SDCPC 2019] Wandering Robot

[SDCPC 2019] Wandering Robot

题目描述

DreamGrid creates a programmable robot to explore an infinite two-dimension plane. The robot has a basic instruction sequence a1,a2,ana_1, a_2, \dots a_n and a "repeating parameter" kk, which together form the full instruction sequence s1,s2,,sn,sn+1,,snks_1, s_2, \dots, s_n, s_{n+1}, \dots, s_{nk} and control the robot.

There are 4 types of valid instructions in total, which are U (up), D (down), L (left) and R (right). Assuming that the robot is currently at (x,y)(x,y), the instructions control the robot in the way below:

  • U: Moves the robot to (x,y+1)(x,y+1).
  • D: Moves the robot to (x,y1)(x,y-1).
  • L: Moves the robot to (x1,y)(x-1,y).
  • R: Moves the robot to (x+1,y)(x+1,y).

The full instruction sequence can be derived from the following equations $$\begin{cases} s_i = a_i & \text{if } 1 \le i \le n \ s_i = s_{i-n} & \text{otherwise} \end{cases}$$

The robot is initially at (0,0)(0,0) and executes the instructions in the full instruction sequence one by one. To estimate the exploration procedure, DreamGrid would like to calculate the largest Manhattan distance between the robot and the start point (0,0)(0,0) during the execution of the nknk instructions.

Recall that the Manhattan distance between (x1,y1)(x_1,y_1) and (x2,y2)(x_2,y_2) is defined as $\left| x_1 - x_2 \right| + \left| y_1 - y_2 \right|$.

输入格式

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains two integers nn and kk (1n105,1k1091 \le n \le 10^5, 1 \le k \le 10^9), indicating the length of the basic instruction sequence and the repeating parameter.

The second line contains a string A=a1a2anA = a_1a_2\dots a_n (A=n|A| = n, $a_i \in \{\text{`L'},\text{`R'},\text{`U'},\text{`D'}\}$), where aia_i indicates the ii-th instruction in the basic instriction sequence.

It's guaranteed that the sum of A|A| of all test cases will not exceed 2×1062 \times 10^6.

输出格式

For each test case output one line containing one integer indicating the answer.

2
3 3
RUL
1 1000000000
D
4
1000000000

提示

For the first sample test case, the final instruction sequence is RULRULRUL and the route of the robot is $(0, 0) - (1, 0) - (1, 1) - (0, 1) - (1, 1) - (1, 2) - (0, 2) - (1, 2) - (1, 3) - (0, 3)$. It's obvious that the farthest point on the route is (1,3)(1, 3) and the answer is 44.