Magic Ship (Codeforces 1117C)
Tags: Others, binary search

You a captain of a ship. Initially you are standing in a point $$$(x_1, y_1)$$$ (obviously, all positions in the sea can be described by cartesian plane) and you want to travel to a point $$$(x_2, y_2)$$$. You know the weather forecast — the string $$$s$$$ of length $$$n$$$, consisting only of letters U, D, L and R. The letter corresponds to a direction of wind. Moreover, the forecast is periodic, e.g. the first day wind blows to the side $$$s_1$$$, the second day — $$$s_2$$$, the $$$n$$$-th day — $$$s_n$$$ and $$$(n+1)$$$-th day — $$$s_1$$$ again and so on. Ship coordinates change the following way:  if wind blows the direction U, then the ship moves from $$$(x, y)$$$ to $$$(x, y + 1)$$$;  if wind blows the direction D, then the ship moves from $$$(x, y)$$$ to $$$(x, y - 1)$$$;  if wind blows the direction L, then the ship moves from $$$(x, y)$$$ to $$$(x - 1, y)$$$;  if wind blows the direction R, then the ship moves from $$$(x, y)$$$ to $$$(x + 1, y)$$$. The ship can also either go one of the four directions or stay in place each day. If it goes then it's exactly 1 unit of distance. Transpositions of the ship and the wind add up. If the ship stays in place, then only the direction of wind counts. For example, if wind blows the direction U and the ship moves the direction L, then from point $$$(x, y)$$$ it will move to the point $$$(x - 1, y + 1)$$$, and if it goes the direction U, then it will move to the point $$$(x, y + 2)$$$.You task is to determine the minimal number of days required for the ship to reach the point $$$(x_2, y_2)$$$.

Input: The first line contains two integers $$$x_1, y_1$$$ ($$$0 \le x_1, y_1 \le 10^9$$$) — the initial coordinates of the ship.The second line contains two integers $$$x_2, y_2$$$ ($$$0 \le x_2, y_2 \le 10^9$$$) — the coordinates of the destination point.It is guaranteed that the initial coordinates and destination point coordinates are different.The third line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the string $$$s$$$.The fourth line contains the string $$$s$$$ itself, consisting only of letters U, D, L and R.

Output: The only line should contain the minimal number of days required for the ship to reach the point $$$(x_2, y_2)$$$.If it's impossible then print "-1".

Note: In the first example the ship should perform the following sequence of moves: "RRRRU". Then its coordinates will change accordingly: $$$(0, 0)$$$ $$$\rightarrow$$$ $$$(1, 1)$$$ $$$\rightarrow$$$ $$$(2, 2)$$$ $$$\rightarrow$$$ $$$(3, 3)$$$ $$$\rightarrow$$$ $$$(4, 4)$$$ $$$\rightarrow$$$ $$$(4, 6)$$$.In the second example the ship should perform the following sequence of moves: "DD" (the third day it should stay in place). Then its coordinates will change accordingly: $$$(0, 3)$$$ $$$\rightarrow$$$ $$$(0, 3)$$$ $$$\rightarrow$$$ $$$(0, 1)$$$ $$$\rightarrow$$$ $$$(0, 0)$$$.In the third example the ship can never reach the point $$$(0, 1)$$$.

Sample Inputs:
0 0
4 6
3
UUU

0 3
0 0
3
UDD

0 0
0 1
1
L

Sample Outputs:
5

3

-1

Solution (Python):
(x1, y1) = list(map(int, input().split()))
(x2, y2) = list(map(int, input().split()))
n = int(input())
S = input()
x = 0
y = 0
WALKLIST = [(0, 0)]
for s in S:
    if s == 'U':
        y += 1
    elif s == 'D':
        y -= 1
    elif s == 'L':
        x -= 1
    elif s == 'R':
        x += 1
    WALKLIST.append((x, y))
L = 0
R = 10 ** 15
impossible = False
while L != R:
    mid = (L + R) // 2
    (Q, rem) = divmod(mid, n)
    x3 = x1 + Q * x + WALKLIST[rem][0]
    y3 = y1 + Q * y + WALKLIST[rem][1]
    xoffset = abs(x2 - x3)
    yoffset = abs(y2 - y3)
    if xoffset + yoffset <= mid:
        R = mid
    else:
        L = mid + 1
    if L == 10 ** 15:
        print(-1)
        impossible = True
if not impossible:
    print(R)

Solution (Python):
(x1, y1) = list(map(int, input().split()))
(x2, y2) = list(map(int, input().split()))
n = int(input())
S = input()
x = 0
y = 0
WALKLIST = [(0, 0)]
for s in S:
    if s == 'U':
        y += 1
    elif s == 'D':
        y -= 1
    elif s == 'L':
        x -= 1
    elif s == 'R':
        x += 1
    WALKLIST.append((x, y))
L = 0
R = 10 ** 15
impossible = False
while L != R:
    mid = (L + R) // 2
    (Q, rem) = divmod(mid, n)
    x3 = x1 + Q * x + WALKLIST[rem][0]
    y3 = y1 + Q * y + WALKLIST[rem][1]
    xoffset = abs(x2 - x3)
    yoffset = abs(y2 - y3)
    if xoffset + yoffset <= mid:
        R = mid
    else:
        L = mid + 1
    if L == 10 ** 15:
        print(-1)
        impossible = True
if not impossible:
    print(R)

Solution (Python):
start = tuple(map(int, input().split()))
end = tuple(map(int, input().split()))
num = int(input())
winds = input()

def solve(start, end, winds):
    (x1, y1) = start
    (x2, y2) = end
    n = len(winds)
    x = 0
    y = 0
    walk_list = [[0, 0]]
    for wind in winds:
        if wind == 'U':
            y += 1
        elif wind == 'D':
            y -= 1
        elif wind == 'R':
            x += 1
        elif wind == 'L':
            x -= 1
        walk_list.append([x, y])
    (l, r) = (0, n * (abs(x1 - x2) + abs(y1 - y2)))
    ans = -1
    while l <= r:
        mid = (l + r) // 2
        complete_cycles = mid // n
        partial_cycles = mid % n
        x3 = x1 + complete_cycles * x + walk_list[partial_cycles][0]
        y3 = y1 + complete_cycles * y + walk_list[partial_cycles][1]
        if abs(x2 - x3) + abs(y2 - y3) > mid:
            l = mid + 1
        else:
            ans = mid
            r = mid - 1
    return ans
print(solve(start, end, winds))