1 second
256 megabytes
standard input
standard output
Fox Ciel has a robot on a 2D plane. Initially it is located in (0, 0). Fox Ciel code a command to it. The command was represented by strings. Each character of s is one move operation. There are four move operations at all:
- 'U': go up, (x, y) → (x, y+1);
- 'D': go down, (x, y) → (x, y-1);
- 'L': go left, (x, y) → (x-1, y);
- 'R': go right, (x, y) → (x+1, y).
The robot will do the operations in s from left to right, and repeat it infinite times. Help Fox Ciel to determine if after some steps the robot will located in (a,b).
The first line contains two integers a and b, (-109≤a,b≤109). The second line contains a string s (1≤|s|≤100, s only contains characters 'U', 'D', 'L', 'R') — the command.
Print "Yes" if the robot will be located at (a,b), and "No" otherwise.
2 2
RU
Yes
1 2
RU
No
-1 1000000000
LRRLU
Yes
0 0
D
Yes
In the first and second test case, command string is "RU", so the robot will go right, then go up, then right, and then up and so on.
The locations of its moves are (0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) → ...
So it can reach (2, 2) but not (1, 2).
官方题解:
322C - Ciel and Robot 321A - Ciel and Robot
Note that after Ciel execute string s, it will moves (dx, dy).
And for each repeat, it will alway moves (dx, dy).
So the total movement will be k * (dx, dy) + (dx[p], dy[p]) which (dx[p], dy[p]) denotes the movement after execute first p characters.
We can enumerate p since (0 <= p < |s| <= 100), and check if there are such k exists.
Note that there are some tricks:
We can divide dx or dy directly because they both can become zero.
Another trick is that k must be non-negative.
Many people failed on this test case (which no included in the pretest):
-1 -1
UR