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 string s. 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).
Input
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.
Output
Print "Yes" if the robot will be located at (a, b), and "No" otherwise.
Examples
Input
2 2
RU
Output
Yes
Input
1 2
RU
Output
No
Input
-1 1000000000
LRRLU
Output
Yes
Input
0 0
D
Output
Yes
Note
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).
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to determine if there exists some step t (>=0, integer) such that after executing the sequence s repeatedly (t steps, not necessarily an integer multiple of |s|? Actually robot executes operations in s from left to right, and repeats infinitely. So after t steps, the position is the sum of the first t vectors from the infinite repetition of s. Equivalent: let n = len(s). Let dx[i] and dy[i] for i=1..n be the increments per character. The overall displacement for one full cycle is (dx_total, dy_total). For any t, we can write t = kn + r, where 0 <= r < n. Then position = k(dx_total, dy_total) + prefix_sum[r] where prefix_sum[0]= (0,0) and prefix_sum[i] = sum of first i increments.
We need to check if exists k>=0 and r in [0, n-1] (or r=0 meaning after full cycles? Actually r steps within the cycle, including after complete cycles? Since t can be 0? At time 0, robot is at (0,0). So we should also consider t=0 (r=0,k=0). So r can be 0..n-1 inclusive? Actually prefix_sum[0] = (0,0) corresponds to 0 steps. After exactly n steps (k=1, r=0) we have position = 1 * total + prefix_sum[0] = total. But prefix_sum[n] is not defined; we consider only r from 0 to n-1 for the remainder. However, note that after exactly full cycles we have position = k*total. This corresponds to r=0 with k>=0. So that's covered.
Thus condition: exists integer k >= 0, and r in {0,1,...,n-1} such that:
a = kdx_total + px[r]
b = kdy_total + py[r]
where (px[r], py[r]) are prefix sums after r moves.
We need to check existence.
Constraints: a,b up to 1e9, n up to 100. So brute force over r (n possibilities) and solve for integer k. For each r, we need to check if there exists k>=0 integer satisfying the equations.
The equations are linear in k:
dx_total * k = a - px[r]
dy_total * k = b - py[r]
If dx_total and dy_total are both zero, then total displacement per cycle is zero. Then the robot's positions are exactly the set of prefix positions visited during the first cycle (and repeats after cycle, but they are the same positions because zero net displacement). Actually if total = (0,0), then after any number of full cycles we return to the same positions as after the first cycle? Wait careful: If total = (0,0), then after each full cycle the position returns to the position before that cycle. So the set of positions reachable is just the set of positions visited during the first cycle (including start). That includes all prefix positions for 0 <= t <= n (but after n steps we return to (0,0)). Actually after n steps, we are back to start because total is zero. So positions visited are prefix positions for t=0..n-1 (since after n we are back to start which is already counted). But we can also have t multiple cycles plus remainder: because after each full cycle, you're back to start? Wait if total=0, then after k cycles you're at start (0,0) (since starting at (0,0) and each cycle nets zero). So then positions you can reach are: For any t = k*n + r, position = prefix[r] (since k cycles add zero). So indeed the reachable set is exactly { prefix[r] : r=0..n-1 } (including (0,0) for r=0). So we just need to check if (a,b) equals any prefix.
But careful: Could t be negative? No, only nonnegative integer steps. So that's it.
If one of dx_total, dy_total is zero but the other is nonzero, we have to handle.
General case: For a given r, we need k to satisfy both equations simultaneously. So we need:
a - px[r] must be divisible by dx_total if dx_total != 0, but if dx_total == 0 then we require a - px[r] == 0. Similarly for dy. Also k must be the same. So conditions:
If dx_total != 0 and dy_total != 0: Then we need (a - px[r]) / dx_total = (b - py[r]) / dy_total, and that quotient must be integer and >=0. Since division might be negative or fractional. Actually we need:
Let kx = (a - px[r]) / dx_total, must be integer and kx >= 0.
Let ky = (b - py[r]) / dy_total, must be integer and ky >= 0.
And kx == ky.
But careful about signs: dx_total could be negative. So division should be exact integer division, i.e., (a - px[r]) % dx_total == 0? In programming, we check that (a - px[r]) is divisible by dx_total: (a - px[r]) % dx_total == 0. But modulus with negative numbers can be tricky; better to check using multiplication: there exists integer k such that k * dx_total = a - px[r] and k * dy_total = b - py[r]. Then we can solve by checking if dx_total != 0 then k = (a - px[r]) / dx_total must be integer (check that division yields integer with no remainder). But if dx_total = 0 we need a - px[r] = 0.
Better: For each r, we can attempt to solve for k if possible. Since n <= 100 and we can try k from? Not feasible because a,b up to 1e9 but k could be huge. So we need analytic.
Alternative approach: we can think of the robot's path as an arithmetic progression in 2D. For each r, the positions after t = k*n + r steps are P(r,k) = prefix[r] + k * total. So we need to check if (a,b) lies on that line. For a given r, we need k such that:
(a,b) = prefix[r] + k * total
=> If total = (0,0), then we require (a,b) = prefix[r]. Otherwise, total is nonzero (at least one component nonzero). Then we can find k by solving along the direction. But we need consistent k for both coordinates.
We can treat as solving:
If total != (0,0), then we can attempt to compute k as rational from whichever component is nonzero. But we need integer k >=0 and must satisfy both.
One method: For each r, compute diff = (a - px[r], b - py[r]). We need diff to be a scalar multiple of total (i.e., diff = k * total for some integer k >=0). So we can check if total.x * diff.y == total.y * diff.x (i.e., cross product zero) to ensure collinearity. Additionally, if total.x != 0, then k = diff.x / total.x must be integer >=0 and same for y. But we need to be careful with zero components.
Simplify: Check if total.x == 0 and total.y == 0 => special case.
Otherwise, if total.x == 0:
Then we need diff.x == 0. And then compute k = diff.y / total.y (if total.y != 0) must be integer >=0.
Similarly if total.y == 0.
If both nonzero, we check cross product == 0. Then compute k = diff.x / total.x (integer) and also ensure k >=0 and diff.y == k * total.y.
But we must also handle negative totals, but k must be >=0. So k must be non-negative integer. If total is negative, then diff must be opposite sign to make k positive? Actually if total.x negative and diff.x positive, then k = diff.x / total.x would be negative (since positive divided by negative is negative) so not allowed. So we need k >=0, which implies that diff.x and total.x must have the same sign (or both zero). But if total.x is negative, then diff.x must also be negative (or zero) to have k non-negative. But if diff.x is negative and total.x negative, k = positive. That's fine. However, we must also ensure that k is integer and matches for y.
Thus algorithm:
Compute total displacement per cycle: (dx, dy).
Compute prefix arrays px[0..n], py[0..n] where px[0]=0, py[0]=0, and for i from 1 to n, px[i] = px[i-1] + delta_x(s[i]), etc. Note: we only need prefix up to n-1? But include px[n] if needed? Actually after n steps we are back? Not necessarily; total might be nonzero so after n steps we are at total. But we want r from 0 to n-1 inclusive? For r=0 we have prefix[0] = (0,0). For r=n, that corresponds to k cycles + n steps = (k+1)total + prefix[0]? Actually t = (k+1)n + 0? Hmm, let's be careful: The representation t = kn + r with 0 <= r < n. So r ranges 0..n-1. After k full cycles plus r steps, position = ktotal + prefix[r]. This includes the case r=0 (k cycles). For r=n, that would be k*n + n = (k+1)*n + 0, so that's actually covered by r=0 with k' = k+1. So we don't need to consider r = n separately. So prefix sums for r from 0 to n-1 inclusive.
But we also have the possibility that after some steps we are exactly at (a,b) at a time that is multiple of n (i.e., r=0). So that is covered by r=0.
Thus we need to check for each r in 0..n-1. Actually including r=0: px[0]=0, py[0]=0.
Now:
If total == (0,0):
For each r in 0..n-1: check if (a,b) == (px[r], py[r]). If any match, output Yes; else No.
Else (total != (0,0)):
For each r in 0..n-1:
diff_x = a - px[r]
diff_y = b - py[r]
// We need to check if there exists integer k >= 0 such that diff = k * total.
// This is equivalent to: if total.x != 0, then k = diff_x / total.x must be integer and >=0, and diff_y must equal k * total.y. If total.x == 0, then we need diff_x == 0 and then k = diff_y / total.y integer >=0.
// But careful: if total.x == 0 and total.y != 0, we need diff_x == 0 and k integer, diff_y = k * total.y.
// If total.x != 0 and total.y == 0, similar.
code
But note: total.x and total.y could both be non-zero. We could choose to compute k from whichever component is non-zero. But we need to handle negative total correctly: In many languages, -7 % 3 might be negative. Better to check divisibility using: diff_x % total.x == 0? But if total.x is negative, diff_x % total.x might be defined with sign of divisor? In C++ negative modulus yields remainder with sign of dividend (or implementation?) Actually in C++ before C++11, it was implementation-defined, but since C++11, the remainder satisfies (a/b)*b + (a%b) = a, with / truncated toward zero. So a%b has sign of a. That means checking diff_x % total.x == 0 may fail if total.x negative because e.g., diff_x=2, total.x=-2: 2 % -2 is 0? In C++11, 2 % -2 = 0, since remainder zero. So it's okay. But safer: check that total.x != 0 and (a - px[r]) is divisible by total.x meaning there exists integer k such that total.x * k = diff_x. This can be tested by: total.x divides diff_x, i.e., diff_x % total.x == 0. But careful: If total.x is negative, modulus may produce 0 if divisible. So that's fine. However, in Python, % always yields a nonnegative remainder with respect to the divisor's sign? Actually in Python, a % b has sign of divisor b, so remainder is always >=0 if b>0 else <=0? Actually Python's modulo: a % b = a - floor(a/b)*b, so remainder has same sign as b. For negative divisor, remainder may be negative. But if divisible, remainder is 0 anyway. So diff_x % total.x == 0 works as long as total.x != 0 and diff_x is divisible, remainder 0. So it's fine.
Thus we can implement the condition cleanly.
But there is also the possibility that the robot could be at (a,b) after some steps even if it is not exactly on the line? No, because each candidate r gives a line offset by prefix[r] in the direction of total. So it's either exactly on that line or not.
Edge Cases:
(a,b) = (0,0) is always reachable at step 0 (starting point). So answer should be Yes. In the problem example 4: 0 0 with "D" outputs Yes. Because at step 0 it's at (0,0). So we must include t=0. So we must include r=0, k=0 case: diff = (0,0) gives k=0.
Also, if total = (0,0) and (a,b) = (0,0) is reachable as prefix[0] = (0,0). So it's fine.
If a,b huge, but we can have large k, that's fine; we don't need to iterate k.
But there might also be the possibility that the robot can be at (a,b) after a step that is not of the form kn + r because we assume that r is the remainder modulo n. That's correct because steps are integer and we can break after each operation; so any t can be expressed as kn + r with 0 <= r < n. So it's exhaustive.
Thus the solution is straightforward.
But let's think about a subtle point: Could the robot reach (a,b) at some step that is greater than the number of steps we consider because the direction vector total might be zero but the path may cross (a,b) in between? Actually if total = (0,0), the robot repeats a closed loop. The positions visited are exactly the prefix positions. But could the robot be at (a,b) at some step that is not exactly a prefix? Wait, the positions are the sequence: start (0,0), after 1st move (p1), after 2nd move (p2), ..., after n moves (p_n = total = (0,0) if total=0). Then after n+1 moves, it's again p1, etc. So the set of positions visited at any positive integer step is the set {p1, p2, ..., p_n} (p_n = (0,0) but that's also start, but it's included). However, note that p_n = (0,0) if total=0. So the set of distinct positions visited (including step 0) is {prefix[0], prefix[1], ..., prefix[n-1]}. Because after n steps, we return to start, which is already prefix[0]. So it's correct to check r in 0..n-1.
If total != (0,0), then the positions are infinite and form arithmetic progressions for each remainder.
Now, there might be also the possibility that (a,b) could be reached at a time before we complete the first cycle, but that's covered by r in 0..n-1 with k=0. So we should also include r=0? Yes.
Implementation details:
Read a,b as long long (or int, but up to 1e9, fits in 32-bit, but for safety use 64-bit for intermediate multiplication? cross product: total.x * diff_y can be up to? total.x sum of up to 100 moves each of magnitude 1, so total.x in [-100, 100]. diff_y up to a - py[r], a up to 1e9, py up to 100, so diff up to 1e9. Product up to 100*1e9 = 1e11, fits in 64-bit. But 64-bit safe.
Compute n = len(s). Compute arrays dx, dy for each char.
Compute total (dx_total, dy_total).
Compute prefix arrays px[0..n] (0-indexed). Actually px[0]=0, then for i from 1 to n: px[i] = px[i-1] + dx_i (for character s[i-1] if 0-index). But we need prefix for r from 0 to n-1: px[r] for r steps. That's px[r] (where px[0] is 0). So we can store px[0..n] and use px[r] for r from 0 to n-1.
Alternative: compute as we iterate.
If total == (0,0):
For r in 0..n-1: if a == px[r] and b == py[r] -> Yes.
Else:
For r in 0..n-1:
diff_x = a - px[r];
diff_y = b - py[r];
// Check if exists integer k >=0 such that diff = k * total.
// We'll implement a function that given total (tx,ty) and diff (dx, dy) determines if there is integer k >=0.
code
bool ok = false;
if (tx != 0) {
if (diff_x % tx != 0) continue;
k = diff_x / tx;
// Check that k >=0 and dy matches.
if (k < 0) continue;
if (ty == 0) {
if (diff_y != 0) continue;
} else {
if (diff_y != k * ty) continue;
}
} else {
// tx == 0
if (diff_x != 0) continue;
// Now need to handle ty !=0 because total != (0,0) so ty !=0
if (diff_y % ty != 0) continue;
k = diff_y / ty;
if (k < 0) continue;
}
If ok -> Yes.
If loop ends without success, output No.
We must also consider the possibility that k can be negative? For reachable steps, k is number of full cycles, must be non-negative integer. So we enforce k >=0.
But also consider the case where total != (0,0) but one component zero and the other non-zero. The above handles.
What about the case when both total.x and total.y are non-zero? Our code chooses to use tx !=0 branch. But if tx !=0 but ty also non-zero, we still compute k from tx and then check diff_y == k*ty. That's fine.
But there is a nuance: if tx !=0 and diff_x is 0, then k=0, works if diff_y also 0? Because if diff_x=0, then k=0, then diff_y must be 0 as well? But what if total.y !=0? diff_y must be 0? Wait: if diff_x=0 and tx !=0, then k=0. Then we require diff_y = 0*ty = 0. So diff_y must be 0. That matches expectation: diff must be (0,0) for k=0. So if diff_x=0 but diff_y not 0, then not reachable for that r. So correct.
Now, is there any possibility that the robot can reach (a,b) at some step that is not of the form kn + r because maybe the robot after some steps repeats the same position but with different remainder? Actually the representation is unique for each t: t = kn + r with 0 <= r < n. So all steps covered.
But there might be a subtle scenario: maybe the robot reaches (a,b) after a step that is not a multiple of the cycle length? That is included as r>0. So it's fine.
Now, let's test with examples:
Example 1:
a=2,b=2, s="RU"
n=2
Moves: R: (1,0), U: (0,1)
total = (1,1)
prefix: r=0: (0,0); r=1: (1,0) (after first char)
Check r=0: diff=(2,2); tx=1 => k=2/1=2 integer, k>=0, diff_y=2, kty=21=2 -> match => Yes.
Example 2:
a=1,b=2: same s.
Check r=0: diff=(1,2) => k=1/1=1 integer, diff_y=2 vs k*ty=1 => mismatch -> no.
r=1: diff=(0,2) => tx=1 => diff_x=0 => k=0, diff_y=2 vs 0 -> no.
So No.
Example 3:
a=-1, b=1000000000, s="LRRLU"
Compute s: L: (-1,0); R: (1,0); R: (1,0); L: (-1,0); U: (0,1). Actually string "LRRLU": L,R,R,L,U. So net total: x: -1+1+1-1+0 = 0; y:0+0+0+0+1 = 1. So total = (0,1). n=5.
prefix positions:
r=0: (0,0)
r=1 after L: (-1,0)
r=2 after LR: (0,0) because L->(-1,0); R->(0,0) actually L then R: start (0,0)->L->(-1,0)->R->(0,0). So (0,0)
r=3 after LRR: (1,0) because add R: (1,0)
r=4 after L R R L: (0,0) because add L: (-1,0) from previous? Actually step by step: start(0,0), L: (-1,0), R: (0,0), R: (1,0), L: (0,0). So (0,0).
Thus prefix positions: (0,0), (-1,0), (0,0), (1,0), (0,0). Also r=5? Not needed.
We need to check if (-1, 1e9) reachable.
total=(0,1). So we need diff_x = a - px[r] and diff_y = b - py[r]. Since total.x=0, we require diff_x ==0. And then k = diff_y / 1 = diff_y integer and >=0.
Example 4:
a=0,b=0, s="D"
total = (0,-1)
prefix r=0: (0,0) => diff=(0,0) => total.x=0? Actually total=(0,-1). So tx=0, we require diff_x=0 (true), then diff_y=0, diff_y % ty? ty=-1, diff_y=0 => 0 % -1 = 0, k = 0 / -1 = 0, >=0 -> yes. Also can be found via r=0. So Yes.
Now, need to consider negative total and negative a,b. Should be fine.
But what about the case where total is (0,0) and we have prefix that equals (a,b) after some step? Already handled.
Now, are there any pitfalls? Possibly integer overflow when computing k * ty if k large? k = diff_x / tx. diff_x up to 1e9, tx up to 100, so k up to 1e9. kty up to 1e9100=1e11, still fits 64-bit. But if diff_y up to 1e9 and ty up to 100, product also 1e11. But we might compute k*ty and compare to diff_y. That's fine if using 64-bit.
But also consider the case where tx=0 and ty=0, but we handled earlier.
Also consider the case where the total is (0,0) but (a,b) is reachable after infinitely many cycles? Actually no, because after each cycle you return to start, and the positions are only the prefix ones. So only those.
Now, the algorithm seems correct.
But wait: there is an additional nuance: The robot's path is infinite and we consider after some steps. But could it be that the robot reaches (a,b) at some step that is not exactly a point on the line from a prefix? Actually we derived that position = prefix[r] + k*total. That is exact representation. So no.
But there is also the possibility that the robot might reach (a,b) at some step that is not of the form k*n + r because we might consider stepping partially through a command? No, commands are atomic moves. So each step corresponds to one character. So that's fine.
Thus solution.
But can we simplify further? Perhaps we can avoid checking all r by solving linear equation? However n is only 100, so O(n) is trivial.
Now, test some edge cases:
a=0,b=0,s="U". total=(0,1). r=0 diff (0,0) -> k=0 -> yes. So reachable (start). Should output Yes.
a=0,b=1,s="U". total=(0,1). r=0 diff(0,1) -> k=1 (since diff_x=0, diff_y=1, ty=1 => k=1) -> yes. Also r=1? n=1, r=0 only? Actually n=1, r can be 0 only? Because r in 0..n-1 -> only 0. So yes. So reachable at step 1.
a=0,b=2,s="U". diff(0,2) -> k=2 -> yes. So after 2 cycles? But wait s="U", n=1. After 1 step: (0,1). After 2 steps: (0,2). So indeed reachable. So okay.
a= -1,b=0,s="R". total=(1,0). diff(-1,0) -> k = -1 -> not allowed because k<0. But could it be reachable via r? Only r=0. So no. But is there a step where robot is at (-1,0)? Starting at (0,0) and moving right only, positions: (0,0),(1,0),(2,0),... never negative. So correct.
a=0,b=0,s="R". r=0 diff (0,0) -> k=0 -> yes. So start.
Now consider total=(0,0) but robot might cross (a,b) at some intermediate point? For example s="UD". total=(0,0). prefix: (0,0), (0,1) after U, (0,0) after D. So positions visited: (0,0) and (0,1). So (0,0) reachable; (0,1) reachable; (0, -1) not. So fine.
Now what about s="URDL"? That makes a rectangle, total=(0,0). Prefix: (0,0), (0,1), (1,1), (1,0), (0,0). So positions: (0,0),(0,1),(1,1),(1,0). So if (a,b)=(2,2) not reachable.
Now, what if total != (0,0) but we might have negative k? For example, suppose we want to check if (a,b) could be reached with a negative number of full cycles? Not allowed. So we must enforce k >=0. Could there be a situation where diff = k * total with k negative, but that would correspond to moving backwards in time? Not allowed.
Now, is there any scenario where (a,b) could be reached for some r and k that is not integer? No, because k is number of cycles, integer.
Thus algorithm is sound.
But wait: There's a nuance: The robot starts at (0,0) and executes commands repeatedly. So after infinite repetitions, it will be at points: for any non-negative integer t, position = prefix[t % n] + floor(t/n) * total. That's our expression. So we check if there exists t such that equations hold.
But could there be a scenario where we need to consider t such that r could be equal to n? Since floor(t/n) = k, and remainder r = t - k*n, which is in [0, n-1] if we use Euclidean remainder. For t = n, k=1, r=0. So r never equals n. So we are good.
Now, we should double-check with examples where the total is nonzero and the prefix includes (a,b) with negative k? Not possible.
Now, consider potential pitfalls with negative total and negative diff where k computed from diff_x/tx yields positive if both negative. For instance total=(-1,0), and we want to see if (a,b)=(-5,0) reachable? Starting at (0,0), moves: all 'L'? Actually 'L' gives -1,0? L is (-1,0) so total = (-1,0). The positions: after 1 step: (-1,0); after 2: (-2,0); after 3: (-3,0); etc. So (-5,0) reachable after 5 steps. Our code: r=0 only (n=1). diff = (-5,0). tx = -1. diff_x % tx? -5 % -1 = 0 (in many languages). k = (-5)/(-1) = 5 >=0. diff_y = 0, ty=0? Actually total.y=0. In branch tx !=0: k = diff_x/tx = 5. Then we check ty==0? yes, need diff_y == 0 (true). So ok. So fine.
Now consider total = (2,2), and we check (a,b) = (3,3). diff = (3,3) -> k = 1.5? Actually 3/2 not integer. So fails. But is (3,3) reachable? positions: after each cycle of 2 moves? Actually if s = e.g., "RU"? That's total (1,1) not (2,2). But if s length >1, we might have total (2,2). But could there be a situation where (3,3) reachable via r? For instance, suppose s = "RRUU"? Actually moves: R,R,U,U => total (2,2). prefix after 0: (0,0); after1: (1,0); after2: (2,0); after3: (2,1); after4: (2,2). So positions after steps: (0,0), (1,0), (2,0), (2,1), (2,2), then after 5: (3,2)?? Wait after 4 steps we are at (2,2). Step 5: repeat first char R: (3,2). Step6: (4,2). Step7: (4,3). Step8: (4,4). So (3,3) never appears? Let's check: step7 (4,3), step6 (4,2), step5 (3,2). Not (3,3). Could there be some k and r: r=1 gives prefix (1,0) => positions (1,0)+k*(2,2) = (1+2k, 0+2k). For (3,3): need 1+2k=3 => k=1 => then y=0+2*1=2, not 3. r=2: (2,0) -> (2+2k,0+2k): set 2+2k=3 => k=0.5 not integer. r=3: (2,1) -> (2+2k,1+2k): need 2+2k=3 => k=0.5; r=4: (2,2) -> (2+2k,2+2k): need 2+2k=3 => k=0.5. So unreachable. So our condition works.
Now, consider total = (0,2) and diff_x=0, diff_y=1. That would require k = 0.5, not integer, so unreachable. Indeed if moves only go up by 2 each cycle, you can only reach even y offsets.
Thus solution is correct.
We should also consider the possibility of t=0 being the only solution. That is covered by r=0, diff=(0,0) and k=0.
Edge case: s length 0? But length >=1.
Now, could there be a scenario where (a,b) is reachable with k negative but also with some r? For example, if total is (1,0) and we have prefix r=1 = (1,0). Then position = (1,0)+k*(1,0) = (1+k,0). If we set k=-1, we get (0,0) which is reachable at step 0. But we cannot have negative k because that would correspond to negative cycles. However, we can achieve (0,0) with r=0 and k=0. So the representation with negative k is not allowed but the point may be reachable with some other representation (r, k) where k>=0. So it's fine. Our algorithm only checks non-negative k.
Now, check for potential overflow in cross product? We might compute cross = total.x * diff_y - total.y * diff_x. But we aren't using cross product directly; we are using divisibility. So safe.
But there is a subtlety: When total.x !=0 and we compute k = diff_x / total.x, we rely on integer division with truncation toward zero. But we must ensure that diff_x is exactly divisible by total.x, meaning that diff_x % total.x == 0. In C++ with negative numbers, the remainder sign is that of the dividend, but if diff_x is divisible, remainder should be 0. For example, diff_x = -5, total.x = 2: -5 % 2 = -1? In C++11, -5 % 2 = -1 because -5/2 = -2 (trunc toward zero), -2*2 = -4, remainder -1. Not zero, so not divisible. That's correct. For diff_x = -4, total.x = 2: -4 % 2 = 0. So fine. For diff_x = 4, total.x = -2: 4 % -2 = 0? In C++11, 4 / -2 = -2, -2 * -2 = 4, remainder 0. So ok. So checking diff_x % total.x == 0 works for divisibility regardless of sign. But note: In C++ the modulo operator's result satisfies (a/b)b + (a%b) = a, where a/b is truncated toward zero. So if a is divisible by b (i.e., a = kb for integer k), then a%b = 0. So it's safe. In Python, 4 % -2 = -0? Actually Python: 4 % -2 = -0? Python's modulo yields a result with the sign of divisor. For 4 % -2, the result is 0 (since -2 divides 4). So it's 0. For -4 % 2 = 0. So fine.
Thus no problem.
Now, test some more tricky cases.
Case: total = (2,0), s length 3? Actually s arbitrary. Suppose prefix includes negative x values. For example, s = "L" repeated? Actually "L": total = (-1,0). We did. Or s = "LRR": L (-1,0), R (1,0), R (1,0) => total = (1,0). Prefix: r0 (0,0), r1 (-1,0), r2 (0,0), r3 (1,0). So positions: For r=1 and k cycles: (-1 + k, 0). For k=1, (0,0) which is also reachable with r=0,k=0. So ok. Checking if ( -2, 0 ) reachable? r=1: diff = (-2 - (-1)) = -1? Actually a=-2,b=0, r=1: px=-1, diff_x = -1, total.x=1, diff_x%1=0? -1%1=0, k=-1 -> but k negative, so no. r=0: diff = (-2,0), k=-2? negative. r=3: px=1, diff_x=-3, k=-3 negative. r=2: px=0, diff_x=-2, k=-2 negative. So unreachable. But could we reach -2 by some step? Starting at 0, moves: L -> -1, then R -> 0, R->1, then repeats: L->0? Actually sequence: L,R,R,L,R,R,... positions: step0:0,1:-1,2:0,3:1,4:0,5:1? Let's simulate: after 3 steps (LRR) we at 1; step4: L -> 0; step5: R ->1; step6: R->2? Wait after step3: (1,0); step4: L => (0,0); step5: R => (1,0); step6: R => (2,0); step7: L => (1,0); step8: R => (2,0); step9: R => (3,0). So never -2. So correct.
Now, consider total = (0,0) but with prefixes that include (a,b) not necessarily visited at a specific r because maybe the robot visits (a,b) at a time that is not exactly at the end of a move? Actually the robot only visits positions at the end of each move; we don't care about positions during a move. So only the endpoints are considered. So prefix positions correspond to after each move. That's correct.
Now, one more nuance: In the case total != (0,0), the set of positions visited is { prefix[r] + k*total | k>=0, r=0..n-1 }. However, is it possible that some position can be represented by multiple (r,k) pairs? Yes, but that doesn't matter.
Now, we must be careful about integer division when total.x = 0 or total.y = 0. For example, total = (0, -2). Then we need diff_x = 0 and diff_y divisible by -2. If diff_y = 4, then k = 4 / -2 = -2, which is negative. But is (0,4) reachable? Starting at (0,0), moves give negative y? Actually if total is (0,-2), each cycle net goes down 2. So positions after cycles: (0,0), then after 1 cycle: (0,-2), after 2 cycles: (0,-4). So (0,4) not reachable. So we need k>=0. So k negative not allowed.
Now, what if total = (0, -2) and diff_y = -4, diff_x=0, then k = (-4)/(-2)=2 >=0, so reachable.
So we must enforce k >= 0.
Now, consider scenario where total = (0,0) but there is a prefix that matches (a,b) after some step that is not at the end of a cycle. That's covered.
Thus algorithm is complete.
Now, let's consider the input constraints: a,b up to 1e9. n up to 100. So O(n) trivial.
Now, implement in code. We'll read a and b as long long (or int). Then read string s.
Compute total and prefix.
We need to be careful that for r=0 we already included. For r from 0 to n-1 inclusive.
For i in 0..n-1:
char c = s[i]
if c == 'U': dy=1;dx=0;
elif 'D': dy=-1;
elif 'L': dx=-1;
elif 'R': dx=1;
Update tx += dx; ty += dy;
px[i+1] = px[i] + dx; py[i+1] = py[i] + dy;
But note: we need prefix for r steps: after r moves, that's px[r], py[r] (with px[0]=0). So we can store px[0..n] but we only need up to n-1? Actually px[n] is the total. We may not need it.
But for iteration, we can just compute an array of prefix positions for r from 0 to n-1. We can compute them on the fly: start with current (0,0) for r=0; then after each character, update current, store for next r. So we can store vector<pair<int,int>> prefixes; push (0,0) first then after each move push new position.
Simplify:
code
vector<long long> pref_x, pref_y;
pref_x.push_back(0);
pref_y.push_back(0);
long long cur_x=0, cur_y=0;
for (char c: s) {
if (c=='U') cur_y++;
else if (c=='D') cur_y--;
else if (c=='L') cur_x--;
else if (c=='R') cur_x++;
pref_x.push_back(cur_x);
pref_y.push_back(cur_y);
}
// Now pref_x.size() = n+1; pref_x[0]=0; pref_x[1] after first move... pref_x[n] after all moves = total.
// We need r from 0 to n-1, so we can loop i from 0 to n-1 (inclusive). That gives pref_x[i] for r=i.
But note: pref_x[n] is after n moves, which corresponds to remainder 0 with k increased by 1? Actually for r = n, it's covered by r=0 and k+1. So we don't need to consider r=n. So we use i in [0, n-1].
Now compute total = (pref_x[n], pref_y[n]).
Now main check:
if (total_x == 0 && total_y == 0) {
for i in 0..n-1:
if (a == pref_x[i] && b == pref_y[i]) return Yes;
else No.
} else {
for i in 0..n-1:
long long diff_x = a - pref_x[i];
long long diff_y = b - pref_y[i];
// check if exists k>=0 integer s.t. diff = k * total.
bool ok = false;
if (total_x != 0) {
if (diff_x % total_x != 0) continue;
long long k = diff_x / total_x;
if (k < 0) continue;
// Check y condition
if (total_y != 0) {
if (diff_y != k * total_y) continue;
} else {
if (diff_y != 0) continue;
}
ok = true;
} else { // total_x == 0
if (diff_x != 0) continue;
// total_y must be non-zero because total != (0,0)
if (diff_y % total_y != 0) continue;
long long k = diff_y / total_y;
if (k < 0) continue;
ok = true;
}
if (ok) return Yes;
}
return No;
}
Edge Cases: total_y could be 0 if total_x != 0, handled in first branch. In second branch, we assume total_y != 0 because total != (0,0). So it's safe.
But what about total_x !=0 but total_y = 0? The first branch: we compute k from diff_x, then check if diff_y == 0 (since total_y=0). That's correct.
Now, consider both total_x and total_y != 0: first branch works.
Now, verify with some random tests.
Let's also consider scenario where diff_x and diff_y are both zero. Then k=0 works regardless of total (as long as we have r that gives prefix = (a,b) i.e., diff zero). So if (a,b) equals some prefix, we return Yes. That's correct.
Now, test with a case where (a,b) is not a prefix but lies on the line beyond. Works.
Now, check with total = (0,0) but (a,b) equals a prefix that appears only after multiple cycles? For total zero, after cycles you only repeat same positions. So only prefixes.
Now, one more nuance: Could the robot be at (a,b) at a step that is exactly a multiple of n, i.e., after a full cycle? That's r=0 and k>0. So our check includes that via r=0 and diff = a - 0, b - 0. So it's covered.
Now, check with example: a=2,b=2,s="RU": as earlier, r=0, diff=(2,2) -> k=2 (since total=(1,1)) -> Yes.
Now, what about a=0,b=0,s="RU"? Should be Yes because step 0. Check: total=(1,1) not zero. For r=0, diff=(0,0) -> k=0 -> Yes.
Now, think about potential pitfalls: When total_x is not zero, we compute diff_x % total_x. If total_x is negative, as said, modulo works fine. But note: In C++, if total_x is negative and diff_x % total_x yields zero when divisible; else non-zero. So it's okay.
But careful: We might have integer division rounding toward zero. For example, if diff_x = -1 and total_x = 2: -1 % 2 is -1, not 0, so fails. That's fine. However, if diff_x = -2 and total_x = -1, then -2 % -1 = 0, and k = (-2)/(-1)=2, ok.
Now, check for potential overflow in k * total_y: we compute k * total_y. Since k up to maybe 1e9, total_y up to 100, product up to 1e11, fits 64-bit. But if total_y is -100, product similarly. So safe.
Now, consider the case where total_x is not zero, but diff_x might be 0 and total_x negative? Then k = 0. Works.
Thus algorithm is robust.
But there is also the possibility of (a,b) being huge, like 10^9, and we need to check if it lies on the line. Since we only compute diff and divisibility, it's fine.
Now, let's think about correctness proof: Essentially we need to check existence of integers k >=0 and 0 <= r < n such that (a,b) = (px[r], py[r]) + k*(tx,ty). That's a linear Diophantine problem. For each r, it's checking if diff is a non-negative integer multiple of total. Equivalent to total vector being a direction and diff lying on the ray from the prefix in that direction.
Now, consider an alternative approach: we could also check if after infinite steps, the set of positions visited is a 1D lattice. However, our approach is straightforward.
Now, let's test with a custom case: s = "U", total=(0,1). We'll try a=0,b=-1. diff=(0,-1). Since total.x=0, diff_x must be 0, okay. diff_y%1=0? -1%1=0. k = -1. k<0 -> not allowed. So No. Indeed robot only moves up, so never negative y.
Now, case: s = "UD", total=(0,0). Prefix positions: (0,0), (0,1). So (0,-1) not reachable. Good.
Now, case: s = "ULDR", total=(0,0). prefixes: (0,0), (0,1), (1,1), (1,0). So (1,1) reachable? Yes (after 2 moves). (2,2) not reachable.
Now, case: s = "URDL", total=(0,0). Prefix: (0,0),(0,1),(1,1),(1,0),(0,0). So (1,1) reachable after 2 moves. (2,2) not.
Now, case: s = "RURDLU"? Not needed.
Now, think about potential bug: In the loop for total != (0,0), we might need to also consider r such that the prefix is after r steps where r could be n? Already argued not needed. But what about the scenario where the robot at time t = 0 (r=0) is (0,0). That is included.
Now, is it possible that the robot reaches (a,b) at a step that is exactly the sum of a prefix and a negative multiple of total? That would correspond to going backwards in cycles. Since cycles are repeated forward only, not allowed.
Thus done.
Now, we should also consider that the robot's path may cross (a,b) at a step that is not exactly at a prefix point? No, because the robot only changes position at integer steps. So only at step times.
Now, check some tricky case: The robot's command s = "RRR". total=(3,0). Prefix: r0: (0,0), r1: (1,0), r2: (2,0). So positions reachable: (k3 + 0) = (3k,0); (3k+1,0); (3k+2,0). So any integer x >=0? Actually all non-negative integers? Let's see: For x=4: can we get 4? Write as 3k + r where r=0,1,2. 4 = 31+1 -> r=1, k=1 gives (1+3=4). So yes. x=5: 31+2 -> r=2 => 2+3=5. x=6: 32+0 ->6. x=7: 3*2+1 ->7. So indeed all non-negative integers reachable. So our algorithm should say Yes for any (a,0) with a >=0. Let's test with a=4,b=0: total=(3,0). For r=1, diff_x = 4-1=3, diff_x%3=0, k=1>=0, diff_y=0, total.y=0 => ok. For a= -1,b=0: diff negative, k negative? Let's test r=2: diff_x = -1-2 = -3, k=-1? Actually -3/3 = -1, k negative => not allowed. r=1: -1-1=-2, -2%3 !=0; r=0: -1-0=-1, not multiple. So No. Good.
Now, consider total=(2,2) with s length something. Could we get all points on line y=x? Possibly only those with parity matching prefix. For r=0: (0,0)+k*(2,2) => even coordinates. For r=1: (1,0)+k*(2,2) => (1+2k, 2k) => y = 2k, x = 1+2k, so y = x-1, not y=x. For r=2: (2,0)+k*(2,2) => (2+2k,2k) => y = x-2. So (3,3) not reachable. So only points where x=y and x even are reachable. So algorithm captures.
Now, final check: Input constraints: a,b up to 1e9 (absolute). Our algorithm uses long long.
Now, complexity: O(n) time, O(n) memory (or O(1) if we compute prefixes on the fly and store only needed? But storing is fine, n <= 100.)
Now, we should consider if there is any scenario where the robot could be at (a,b) at a step that is not exactly at a prefix + k*total because of integer overflow? No.
Now, also note that the robot's path could intersect (a,b) multiple times; we just need existence.
Now, are there any edge cases with large n? Not an issue.
Now, let's also think about a potential simplification: Actually we can treat the problem as checking if there exists t such that after t steps the position equals (a,b). Since the movement is periodic, we can compute after each step up to maybe some bound? Not necessary.
Now, implement.
But before finalizing, we need to double-check the logic for total.x !=0 branch when total.y !=0: We compute k from diff_x, then check diff_y == ktotal_y. But is it possible that diff_x divisible by total_x and diff_y == ktotal_y but the cross product not zero? Actually if diff_x divisible, then k integer, and if diff_y == ktotal_y, then automatically diff = ktotal, so cross product zero. So we don't need cross product check. So it's fine.
Now, consider the case where total.x !=0, total.y !=0, but diff_x is divisible by total_x, k computed, and diff_y == ktotal_y but maybe diff_y is not divisible by total_y? But if diff_y == ktotal_y, then diff_y divisible by total_y obviously. So fine.
Now, consider the case where total.x !=0 and total.y =0: we compute k from diff_x, then require diff_y == 0. Good.
Now, consider the case where total.x =0 and total.y !=0: we require diff_x ==0, and diff_y divisible by total_y, and k = diff_y/total_y >=0. Good.
Now, think about when total.x = 0 and total.y = 0 already handled.
Now, corner case: total.x !=0, but diff_x =0 and total.x = some value, then k=0, we then check diff_y == 0 (if total.y=0) or diff_y ==0 (since k=0) if total.y !=0? Actually if total.y !=0, then diff_y must equal 0*total_y = 0. So both diff_x and diff_y must be 0. So that's correct: for k=0, we require prefix equals (a,b). So the point must be exactly a prefix if k=0.
Now, also if total.x !=0, but diff_x =0 and total.x is not zero, k=0, so must have prefix = (a,b). So that is consistent.
Now, is it possible that total.x !=0 but we could have k negative but with an alternative r giving positive k? That's fine.
Now, confirm with a case where (a,b) lies beyond but with negative total.x but positive diff_x? Example: total = (-1,0) and we want (2,0). Is (2,0) reachable? Starting at (0,0) and moving L each step: positions: -1,-2,... So not. Our algorithm: r=0 diff=(2,0). total.x=-1. diff_x%(-1)=0? 2 % -1 = 0? In C++ 2 % -1 = 0? Since -1 divides 2? -1 * -2 = 2, so yes remainder 0. k = 2 / -1 = -2, negative -> not allowed. So No. Good.
If we want (-2,0): diff = (-2,0) -> k = (-2)/(-1)=2 >=0, and diff_y=0, ok -> Yes.
Now, test with total = (-1,2) and some point. Not needed.
Now, I think the solution is correct.
But before finalizing, let's test with more random small cases by mental simulation or reasoning.
Case: s = "R", n=1, total=(1,0). Check (a,b) = (5,0). r=0 diff=(5,0) -> k=5 -> Yes. (5,1): diff=(5,1) -> k=5, then check diff_y? total.y=0 => diff_y must be 0, but diff_y=1 => No. So correct.
Case: s = "UR", n=2, moves: U (0,1), R (1,0). total=(1,1). prefixes: r0:(0,0); r1:(0,1). So positions: (k,k) and (0+ k,1+ k) = (k, 1+k). So set: all points (x,y) where y = x or y = x+1, with x,y >=? Actually for k>=0, these are points: (0,0),(0,1),(1,1),(1,2),(2,2),(2,3),... So (1,0) not reachable? Check: r0: (k,k) => (1,0) would need k=1 => (1,1) no; r1: (k,1+k) => set (1,0) => need k=1 gives (1,2), k=0 gives (0,1). So no. Our algorithm: for (1,0), r0 diff=(1,0). total=(1,1), compute k = diff_x / total_x = 1/1=1, then check diff_y=0 vs kty=1 -> fail. r1: diff = (1-0, 0-1) = (1,-1). k = 1/1=1, diff_y = -1, kty=1 -> fail. So No. Good.
Now, test with total=(0,0) but the command may cross the target during a move, not at endpoint? But problem asks "located in (a, b)" after some steps. Steps refer to after each operation? The robot executes operation, then we consider its location. So only after complete moves. So endpoints.
Thus algorithm done.
Now, write code.
Implementation details:
We'll use long long for all coordinates.
Pseudo:
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b;
cin >> a >> b;
string s;
cin >> s;
int n = s.size();
vector px(n+1), py(n+1);
px[0] = 0; py[0] = 0;
for (int i = 0; i < n; ++i) {
char c = s[i];
long long dx = 0, dy = 0;
if (c == 'U') dy = 1;
else if (c == 'D') dy = -1;
else if (c == 'L') dx = -1;
else if (c == 'R') dx = 1;
px[i+1] = px[i] + dx;
py[i+1] = py[i] + dy;
}
long long tx = px[n], ty = py[n];
code
if (tx == 0 && ty == 0) {
for (int i = 0; i < n; ++i) {
if (a == px[i] && b == py[i]) {
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
}
for (int i = 0; i < n; ++i) {
long long diff_x = a - px[i];
long long diff_y = b - py[i];
bool ok = false;
if (tx != 0) {
if (diff_x % tx != 0) continue;
long long k = diff_x / tx;
if (k < 0) continue;
if (ty != 0) {
if (diff_y != k * ty) continue;
} else {
if (diff_y != 0) continue;
}
ok = true;
} else {
// tx == 0, so ty != 0
if (diff_x != 0) continue;
if (diff_y % ty != 0) continue;
long long k = diff_y / ty;
if (k < 0) continue;
ok = true;
}
if (ok) {
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
}
But wait: In the case where tx != 0 and ty == 0, we check diff_y != 0 after computing k. That's correct.
Now, one more nuance: In the tx != 0 branch, we compute k = diff_x / tx after checking diff_x % tx == 0. However, because tx might be negative, we need to ensure that k is integer. The modulus check ensures that. But what if diff_x and tx have opposite signs and diff_x is divisible? Then k will be negative (since diff_x/tx negative). We then check k < 0 and reject. That's correct.
Now, what about the scenario where tx != 0, diff_x = 0, and tx is negative? Then k = 0, ok.
Now, potential issue: When tx != 0 and ty != 0, we check diff_y == k * ty. But if k is huge (like 1e9), k*ty might overflow a 32-bit int, but we are using long long, so fine.
Now, consider also the possibility of tx = 0 and ty = 0, already handled.
Thus solution seems correct.
But we might want to simplify by using cross product and then solving for k using one division that ensures integer and non-negative. However, the above is clear.
Now, let's think about some edge cases that might break the algorithm:
s length = 100, with total nonzero, but prefix values may be large? Actually each prefix coordinate is sum of up to 100 moves, each ±1, so within [-100,100]. So small.
diff_x and diff_y may be large, but we handle with 64-bit.
When tx != 0, we check diff_x % tx == 0. But if tx is negative, diff_x % tx yields remainder, but we have to be careful: In C++ for negative divisor, the remainder may be negative or positive? But if divisible, remainder is 0. So it's fine. However, what if tx = -1, diff_x = -2, -2 % -1 = 0? Yes. If diff_x = -2, tx = 2? -2 % 2 = 0? -2 % 2 = 0 (since -2/2 = -1, remainder 0). So okay. Actually in C++, the sign of the remainder is the sign of the dividend. But if remainder is 0, it's 0 regardless. So safe.
But what about diff_x = 2, tx = -2? 2 % -2 = 0? 2 / -2 = -1, -1 * -2 = 2, remainder 0. Yes.
Thus it's safe.
Now, we must also consider the case where tx != 0 but diff_x might be 0 and tx is 0? Already handled.
Now, one more potential subtlety: The robot may reach (a,b) after a step that is exactly after a full cycle, which is r=0, k>0. That's covered.
Now, what about a step that is after multiple cycles and a partial cycle that ends exactly at the end of the command (i.e., r=0)? That's covered.
Now, might there be a situation where (a,b) is reachable with a negative k for some r, but also reachable with a non-negative k for a different r? That's fine; we only need one representation.
Now, test with a case where total = (2,2) and we have (a,b) = (1,1). Is (1,1) reachable? prefixes: (0,0) and maybe (some). For s =? Let's construct s = "RURD"? Actually need total (2,2). For simplicity, s = "RU" repeated twice? Actually "RURU" gives total (2,2). Prefixes: 0:(0,0),1:(1,0),2:(1,1),3:(2,1),4:(2,2). So (1,1) is prefix r=2, so reachable. So yes.
Now, consider (a,b) = (3,3) with same s. Not prefix. For r=2 diff = (3-1,3-1) = (2,2) => k=1? Actually k = diff_x / tx = 2/2 =1, diff_y = 2, kty=2 => yes? Wait (3,3) from r=2: (1,1) + 1(2,2) = (3,3). That suggests (3,3) is reachable! But earlier we argued that with total (2,2) and prefixes, (3,3) might not be reachable? Let's recalc: s = "RURU". Let's simulate: step0: (0,0). step1: R -> (1,0). step2: U -> (1,1). step3: R -> (2,1). step4: U -> (2,2). step5: R -> (3,2). step6: U -> (3,3). Indeed (3,3) appears at step6. So reachable. But earlier we had an example with total (2,2) but different prefixes? That example s = "RRUU"? That gave prefixes (0,0),(1,0),(2,0),(2,1),(2,2). That set yields (3,3) not reachable. So it depends on the specific prefix. So our algorithm is correct.
Now, consider total = (0,0) but the robot might be at (a,b) after many cycles because the path might intersect (a,b) multiple times but always at the same points. So we only check prefixes.
Now, we should also consider the case where the robot may start at (0,0) and after some steps return to (0,0) again. That's included.
Now, consider a=0,b=0,s any: always Yes because step 0. Our algorithm returns Yes if we check r=0, diff=0 in the else branch? In else branch, for r=0 diff=0, tx !=0? Suppose tx !=0: diff_x%tx? 0%tx =0, k=0, diff_y=0, so ok. So returns Yes.
Now, I think it's fine.
But wait: Is there a scenario where (a,b) is reachable but our algorithm fails because we only consider r up to n-1? Consider t = some step where remainder r is n? Actually remainder after division by n is defined as 0..n-1. So if t = k*n + n = (k+1)*n + 0, remainder is 0. So covered.
Now, there is a nuance: For t < n, the remainder is t, and we have r = t, which is in 0..n-1. So covered.
Thus exhaustive.
Now, we could also consider that if total is not zero, the robot's positions for each remainder form an arithmetic progression. So we can also solve by checking for each remainder whether the point lies on the line and then checking if k integer and non-negative. That's what we did.
Now, confirm with example from problem: Input 3: -1 1000000000 LRRLU -> we already matched.
Now, let's test a tricky case: a=0,b=1, s = "UD". total=(0,0). prefixes: (0,0),(0,1),(0,0). So (0,1) is prefix r=1 => Yes. So robot reaches (0,1) at step1.
Now, a=0,b=2, s="UD". prefixes: only (0,0),(0,1). So (0,2) not reachable. Indeed robot goes up then down, never reaches 2.
Now, a=0,b=0, s="UD": prefixes include (0,0) => Yes.
Now, a=0,b=0, s="U": prefix includes (0,0) => Yes.
Now, a=0,b=0, s="D": Yes.
All good.
Now, think about input reading: first line two integers, second line string. There could be whitespace. Use cin.
Now, coding.
But wait, I recall that some solutions for this problem (Codeforces 227B? Actually problem "Fox And Command" maybe) use a different approach: they simulate first n steps and then check if (a,b) can be expressed as prefix + k*total, similar.
Now, there is one more nuance: The robot's path includes step 0. Yes.
Now, one more corner: When total is (0,0) and (a,b) = (0,0), we will output Yes even if (0,0) appears as a prefix (which it does as r=0). So fine.
Now, what about when total is (0,0) and (a,b) is something else that appears only as prefix after the full cycle? But full cycle returns to start, so if (a,b) appears as a prefix after some steps, it's included.
Now, potential bug: In total zero case, we only check prefixes for i=0 to n-1. But could (a,b) be equal to the position after exactly n steps? That's (total) = (0,0) if total zero, which is (0,0). So if (a,b) = (0,0) we already have it from i=0. So fine. If total zero but (a,b) equals some other position that is only visited after n steps? But after n steps we are at (0,0). So it's not new. So ok.
Now, let's implement.
But we might also consider an alternative: for each i in 0..n (including n) but treat i as remainder? Actually if we include i=n, that would be the same as i=0 with k increased by 1. But for total zero, i=n is the same as i=0. So no need.
Now, just to be safe, we could also include i=n in the total zero case, but it's redundant.
Now, also we must consider that the robot may be at (a,b) at step t = 0, and we have to output Yes. So we must include that. Our algorithm does.
Now, code.
But before finalizing, we must test with another tricky case: s = "L", a=-1000000000, b=0. total=(-1,0). For r=0 diff = (-1e9,0). diff_x % -1 = 0, k = 1e9, >=0, diff_y=0, ok => Yes. So reachable after 1e9 steps left. Indeed robot moves left each step, so after 1e9 steps it's at (-1e9,0). So correct.
Now, s = "URDL", total=(0,0). a=2,b=2 not reachable. Our code will check all prefixes, none matches, so No.
Now, s = "RURDL", total? R: (1,0); U: (0,1); R: (1,0); D: (0,-1); L: (-1,0). Sum: (1,0). So total=(1,0). n=5. prefixes: 0:(0,0);1:(1,0);2:(1,1);3:(2,1);4:(2,0);5:(1,0). Actually after 5 steps we are at (1,0). So prefixes for r=0..4: (0,0),(1,0),(1,1),(2,1),(2,0). So (3,0) reachable? r=1: diff = (2,0) -> k=2 (since tx=1) -> (1+2,0) = (3,0) -> check: diff_x = 3-1=2, k=2, diff_y=0, total.y=0 -> ok. So reachable.
Now, (2,2) reachable? r=2: (1,1) + k*(1,0) = (1+k,1). For (2,2) need y=2 => k such that 1=2? No. r=3: (2,1)+k*(1,0) => (2+k,1) -> y always 1. r=4: (2,0)+k*(1,0) => y=0. r=0: (0,0) => y=0. So no. So correct.
Now, I think it's all good.
Now, about output: Print "Yes" or "No" exactly as required.
Now, one more nuance: Could there be a scenario where total is (0,0) but the robot visits (a,b) at a step that is not a prefix because it could be visited in between cycles? But we only consider positions after each operation; the robot does not teleport, it moves step by step. So the positions are exactly the prefix positions. So fine.
Thus final answer.
But I recall a known Codeforces problem: "Fox And Command" maybe 510B? Actually I think it's problem "A. Robot Sequence" or something else? But anyway.
Now, let's implement code.
Edge Cases Re-check:
a,b = -10^9,10^9, s length 100, with total nonzero. Use long long.
Now, we must be careful that when we compute diff_x % tx, tx may be 0. So we avoid that branch.
Now, code:
code
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
ll a, b;
cin >> a >> b;
string s;
cin >> s;
int n = s.size();
vector<ll> px(n+1), py(n+1);
px[0] = 0; py[0] = 0;
for (int i = 0; i < n; ++i) {
ll dx = 0, dy = 0;
char c = s[i];
if (c == 'U') dy = 1;
else if (c == 'D') dy = -1;
else if (c == 'L') dx = -1;
else if (c == 'R') dx = 1;
px[i+1] = px[i] + dx;
py[i+1] = py[i] + dy;
}
ll tx = px[n], ty = py[n];
// Check reachable
for (int i = 0; i < n; ++i) {
ll diff_x = a - px[i];
ll diff_y = b - py[i];
if (tx == 0 && ty == 0) {
if (diff_x == 0 && diff_y == 0) {
cout << "Yes" << endl;
return 0;
}
} else {
// Check if diff is multiple of total
if (tx != 0) {
if (diff_x % tx != 0) continue;
ll k = diff_x / tx;
if (k < 0) continue;
if (ty != 0) {
if (diff_y != k * ty) continue;
} else {
if (diff_y != 0) continue;
}
cout << "Yes" << endl;
return 0;
} else {
// tx == 0 => ty != 0
if (diff_x != 0) continue;
if (diff_y % ty != 0) continue;
ll k = diff_y / ty;
if (k < 0) continue;
cout << "Yes" << endl;
return 0;
}
}
}
// Also check the case total == (0,0) but we didn't find match? Actually loop above would have covered if diff zero at some i? Wait our loop handles both cases? But we used separate condition inside loop: if total zero we only check if diff zero at each i, but we break if found. However if total zero and we loop over all i and none diff zero, we exit loop and print No. That's okay. But note that in total zero case we are not using the else branch; we have condition if (tx==0 && ty==0) inside loop. That's okay but it's slightly inefficient because we check that condition each iteration. But it's fine. However, we must ensure that we loop over i from 0 to n-1. For total zero, we check each i: if diff zero at any i, print Yes. If none, after loop print No.
// But there is a subtlety: In total zero case, we might also consider that (a,b) could be reachable at step 0 which corresponds to i=0. That's included.
// However, what about total non-zero? The loop's else branch will attempt to find a match. If found, print Yes and return. If none, after loop we print No.
// So the structure is okay.
cout << "No" << endl;
return 0;
}
But the loop currently has if (tx==0 && ty==0) inside, which is constant for all iterations. Could evaluate once outside to avoid repeated checks. But not necessary.
However, there is a bug: In the branch for total zero, we have the check inside the loop. That's okay. But after the loop, we output No. That covers all cases. However, if total zero, the loop will check each i; if none match, No. Good.
But we need to be careful: If total non-zero, the else branch executes for each i, and if found, print Yes; else after loop No. Good.
But wait: In the total zero case, we also have the else part? No, because we have an if-else inside the loop that checks tx and ty. So for total zero, we go into if (tx==0 && ty==0) branch. That's fine.
But one more nuance: In the total zero case, we also need to consider that after infinite repetitions, the positions are just the prefixes. So (a,b) must equal some prefix. That's exactly what we check.
Now, what about the case where total zero and n=0? Not possible.
Thus final.
But we can simplify by separating the two cases outside the loop for clarity.
I'll write as:
if (tx == 0 && ty == 0) {
for i ...
} else {
for i ...
}
That's cleaner.
I'll adjust code accordingly.
Now, final answer.
But before finalizing, let's consider if there is any scenario where the robot could be at (a,b) after a step that is not of the form prefix[r] + ktotal due to the possibility that the robot may be at a point that is visited after exactly a full cycle, which is prefix[0] + ktotal? Actually after k full cycles, position = ktotal. That's prefix[0] + ktotal. So covered.
Thus done.
Now, produce final solution.
I'll write a clear explanation and code.
But I'm a ChatGPT answering, so I need to output the answer (analysis) and final code. The user said: "Help Fox Ciel to determine if after some steps the robot will located in (a, b)." So we provide solution.
Now, double-check with example Input:
Example 1:
2 2
RU
Our algorithm:
n=2, px[0]=0,py[0]=0; after R: px[1]=1,py[1]=0; after U: px[2]=1,py[2]=1? Wait R then U: Actually s="RU": first char R: (1,0). Second char U: (1,1). So px[2]=1,py[2]=1. But I think I mis computed earlier. Let's recalc: Starting (0,0). After R: (1,0). After U: (1,1). So total = (1,1). Prefixes: i=0: (0,0); i=1: (1,0). So for a=2,b=2: Check i=0: diff=(2,2). tx=1, diff%tx=0, k=2, check diff_y == k*ty => 2==2 -> Yes. So output Yes. Good.
But earlier I thought px[1] after first char is (1,0). That's correct.
Now, check example 2: (1,2) s="RU". i=0 diff=(1,2) => k=1, diff_y=2 vs 1 -> No. i=1 diff=(0,2) => tx=1, diff%tx? 0%1=0, k=0, diff_y=2 vs 0 -> No. So No.
Example 3: we already tested.
Example 4: 0 0 D: n=1, total=(0,-1). i=0 diff=(0,0). tx=0? Actually tx=0, ty=-1. So go to else branch with tx=0: diff_x=0, diff_y%ty? 0 % -1 =0, k=0, k>=0 => Yes. Good.
All match.
Now, test some random case: (0,0) with s="U". total=(0,1). i=0 diff=(0,0). tx=0? Actually tx=0, ty=1. So else branch tx=0: diff_x=0, diff_y%1=0, k=0 => Yes.
Now, (0,-1) with s="U". diff=(0,-1). k = -1, fails. So No.
Now, test with s="LR" (L then R). total=(0,0). prefixes: (0,0),(-1,0),(0,0). So positions: (0,0) and (-1,0). Check (0,0): Yes. Check (-1,0): Yes. Check (1,0): No.
Our algorithm for total zero: i=0: (0,0) matches (0,0)? Not if a=1,b=0: false. i=1: (-1,0) false. So No.
All good.
Now, time complexity O(n). Space O(n).
Now, there is also possibility that we could reduce memory but not needed.
Now, the solution is ready.
One more thought: The robot might also be at (a,b) after an infinite number of steps? The problem says "after some steps", so finite.
Now, let's consider a more complex scenario: The robot's path may cross (a,b) at a step that is not exactly an integer multiple of the cycle length? Already considered.
Thus final.
Now, I'll output the final answer with explanation and code. The robot executes the command string s repeatedly, moving one step per character.
We need to check whether there exists a non‑negative integer (t) such that after (t) steps the robot is exactly at ((a,b)).
Let (n = |s|).
Define the displacement per character:
'U': ((0,1))
'D': ((0,-1))
'L': ((-1,0))
'R': ((1,0))
Let (P_0 = (0,0)) and for (1 \le i \le n) let (P_i) be the position after executing the first (i) characters.
The total displacement after one full execution of (s) is (T = (T_x, T_y) = P_n).
After (t = k \cdot n + r) steps ((k \ge 0), (0 \le r < n)), the robot's position is
[
P(t) = P_r + k \cdot T.
]
Thus we must decide whether there exist integers (k \ge 0) and (r \in {0,1,\dots,n-1}) such that
[
(a,b) = P_r + k \cdot T .
]
The solution iterates over all possible remainders (r). For each (r) it computes the difference
[
(d_x, d_y) = (a - P_r^x,; b - P_r^y)
]
and checks whether ((d_x, d_y)) is a non‑negative integer multiple of (T).
If (T = (0,0)) (the command leaves the robot at the origin after each full cycle), the robot only ever visits the positions (P_0, P_1, \dots, P_{n-1}). So we simply check whether ((a,b)) equals any of them.
Otherwise, for each (r) we verify that (d_x, d_y) satisfy the required multiple condition, taking care of zero components.
In detail, for (T \neq (0,0)):
If (T_x \neq 0):
require (d_x) divisible by (T_x); set (k = d_x / T_x).
Then require (k \ge 0) and (d_y = k \cdot T_y) (if (T_y = 0) then (d_y) must be (0)).
If (T_x = 0) (then automatically (T_y \neq 0)):
require (d_x = 0) and (d_y) divisible by (T_y); set (k = d_y / T_y); require (k \ge 0).
If any (r) satisfies these conditions, the answer is "Yes"; otherwise "No".
Since (n \le 100) the loop is trivial.
cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll a, b;
string s;
cin >> a >> b >> s;
int n = s.size();
vector<ll> px(n + 1), py(n + 1);
px[0] = py[0] = 0;
for (int i = 0; i < n; ++i) {
ll dx = 0, dy = 0;
char c = s[i];
if (c == 'U') dy = 1;
else if (c == 'D') dy = -1;
else if (c == 'L') dx = -1;
else if (c == 'R') dx = 1;
px[i + 1] = px[i] + dx;
py[i + 1] = py[i] + dy;
}
ll tx = px[n], ty = py[n];
// Case 1: total displacement is zero
if (tx == 0 && ty == 0) {
for (int i = 0; i < n; ++i) {
if (a == px[i] && b == py[i]) {
cout << "Yes\n";
return 0;
}
}
cout << "No\n";
return 0;
}
// Case 2: total displacement non‑zero
for (int i = 0; i < n; ++i) {
ll dx = a - px[i];
ll dy = b - py[i];
bool ok = false;
if (tx != 0) {
if (dx % tx != 0) continue;
ll k = dx / tx;
if (k < 0) continue;
if (ty != 0) {
if (dy != k * ty) continue;
} else {
if (dy != 0) continue;
}
ok = true;
} else { // tx == 0 → ty != 0
if (dx != 0) continue;
if (dy % ty != 0) continue;
ll k = dy / ty;
if (k < 0) continue;
ok = true;
}
if (ok) {
cout << "Yes\n";
return 0;
}
}
cout << "No\n";
return 0;
}
Actually we can combine by checking if total.x * diff_y == total.y * diff_x (cross product zero) to ensure collinearity. Then compute k:
if total.x != 0:
if diff_x % total.x != 0: then k not integer.
else: k = diff_x / total.x.
else if total.y != 0:
if diff_y % total.y != 0: then k not integer.
else: k = diff_y / total.y.
else: // both zero, but we are in else case so not both zero.
Then check that k >= 0 and also if total.x != 0: diff_y == k * total.y (should hold automatically if cross product zero? Not exactly: if total.x = 0 and total.y != 0, cross product zero means total.x * diff_y - total.y * diff_x = 0 => - total.y * diff_x = 0 => diff_x = 0. So that condition is satisfied. But if both nonzero, cross product zero ensures ratio is same, but integer check ensures exact equality if k computed from x matches y. But we should also check that diff_y == k * total.y to avoid issues if total.x != 0 but diff_x divisible but diff_y not exactly k * total.y due to integer rounding? Actually if cross product zero, then diff_x * total.y = diff_y * total.x. If total.x != 0, then diff_y = (diff_x * total.y) / total.x. If we compute k = diff_x / total.x (integer), then diff_y = k * total.y exactly because diff_x = k * total.x => (k * total.x * total.y)/total.x = k * total.y, provided total.x divides diff_x exactly. So cross product zero ensures that if total.x divides diff_x, then automatically diff_y equals k * total.y. However, if total.x does not divide diff_x, cross product zero might still hold but diff_x not multiple of total.x? Let's examine: Suppose total.x = 2, total.y = 3. diff_x = 1, diff_y = 1.5? But diff_y must be integer. Actually diff_x and diff_y are integers. cross product: 2 * diff_y - 3 * diff_x = 0 => 2*diff_y = 3*diff_x => diff_y = (3*diff_x)/2. For diff_x integer, diff_y integer only if 3*diff_x even. But cross product zero condition requires equality of cross product. If we compute cross product as integer, we can check zero. But it's possible that cross product zero but diff_x not divisible by total.x. For example total.x=2, total.y=3, diff_x=2, diff_y=3 => cross = 2*3-3*2=0. diff_x divisible by total.x? 2/2=1 integer => fine. Another: diff_x=4, diff_y=6 => divisible. diff_x= -2, diff_y= -3 => divisible. diff_x=1, diff_y=1.5 not integer, so cross product would be 2*? Actually diff_y must be integer, so 1.5 impossible. So cross product zero with integer diff_x, diff_y and total.x, total.y integer means diff_x / total.x = diff_y / total.y as rational number. But diff_x might not be a multiple of total.x; but the ratio could still be equal if both fractions reduce? For integers, if total.x and total.y have a common factor, it's possible that diff_x is not a multiple of total.x but the ratio diff_x/total.x = diff_y/total.y is a rational number, but for it to be integer k, we need diff_x = k*total.x and diff_y = k*total.y. However, cross product zero only implies that diff_x * total.y = diff_y * total.x. That does not guarantee that diff_x is a multiple of total.x unless total.x and total.y are coprime? Actually no: consider total.x = 4, total.y = 6. diff_x = 2, diff_y = 3. Then cross = 4*3 - 6*2 = 12 -12 = 0. diff_x=2 is not a multiple of 4. However, is there an integer k such that (2,3) = k*(4,6)? That would require k=0.5, not integer. So cross product zero but not integer multiple. So we must check integer divisibility or that k computed is integer and consistent. Therefore we need both: cross product zero (ensuring collinearity) and that the quotient k is integer (i.e., if total.x !=0, diff_x % total.x ==0) and k>=0. And also diff_y = k * total.y (should follow from cross product zero and divisibility? Let's test: total.x=4, total.y=6, diff_x=2, diff_y=3. cross zero. diff_x % total.x = 2%4 =2 !=0 so fails. So we don't need extra diff_y check if we compute k from x and then check diff_y == k*total.y. That would catch it because k=0 if integer division truncates? Actually diff_x / total.x as integer division would be 0 (if using integer truncation) but 0* total.y = 0 != 3. So that fails. So we can compute k via division only if diff_x is exactly divisible. So algorithm: if total.x != 0, check diff_x % total.x == 0, set k = diff_x / total.x; then check diff_y == k * total.y and k >= 0. Similarly if total.x == 0, then check diff_x == 0, and total.y !=0, check diff_y % total.y == 0, k = diff_y / total.y, k>=0. That works. Also handle case total.x == 0 and total.y == 0 separately.