Upload code_segments/segment_165.txt with huggingface_hub
Browse files
code_segments/segment_165.txt
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
This is the hard version of the problem. The only difference is that in this version $k \le 10^{12}$. You can make hacks only if both versions of the problem are solved.
|
| 2 |
+
|
| 3 |
+
Given a $w \times h$ rectangle on the $Oxy$ plane, with points $(0, 0)$ at the bottom-left and $(w, h)$ at the top-right of the rectangle.
|
| 4 |
+
|
| 5 |
+
You also have a robot initially at point $(0, 0)$ and a script $s$ of $n$ characters. Each character is either L, R, U, or D, which tells the robot to move left, right, up, or down respectively.
|
| 6 |
+
|
| 7 |
+
The robot can only move inside the rectangle; otherwise, it will change the script $s$ as follows:
|
| 8 |
+
|
| 9 |
+
* If it tries to move outside a vertical border, it changes all L characters to R's (and vice versa, all R's to L's). * If it tries to move outside a horizontal border, it changes all U characters to D's (and vice versa, all D's to U's).
|
| 10 |
+
|
| 11 |
+
Then, it will execute the changed script starting from the character which it couldn't execute.
|
| 12 |
+
|
| 13 |
+
 An example of the robot's movement process, $s = \texttt{"ULULURD"}$
|
| 14 |
+
|
| 15 |
+
The script $s$ will be executed for $k$ times continuously. All changes to the string $s$ will be retained even when it is repeated. During this process, how many times will the robot move to the point $(0, 0)$ in total? Note that the initial position does NOT count.
|
| 16 |
+
|
| 17 |
+
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
|
| 18 |
+
|
| 19 |
+
The first line of each test case contains four integers $n$, $k$, $w$, and $h$ ($1 \le n, w, h \le 10^6$; $1 \le k \le 10^{12}$).
|
| 20 |
+
|
| 21 |
+
The second line contains a single string $s$ of size $n$ ($s_i \in \\{\texttt{L}, \texttt{R}, \texttt{U}, \texttt{D}\\}$) — the script to be executed.
|
| 22 |
+
|
| 23 |
+
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
|
| 24 |
+
|
| 25 |
+
For each test case, print a single integer — the number of times the robot reaches $(0, 0)$ when executing script $s$ for $k$ times continuously.
|
| 26 |
+
|
| 27 |
+
In the first test case, the robot only moves up and right for the first two executions. After th
|