Steps (Codeforces 152B)
Tags: Implement, binary search

One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple.Vasya noticed that the yard is a rectangular n × m field. The squares have coordinates (x, y) (1 ≤ x ≤ n, 1 ≤ y ≤ m), where x is the index of the row and y is the index of the column.Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps).A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step.Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made.

Input: The first input line contains two integers n and m (1 ≤ n, m ≤ 109) — the yard's sizes. The second line contains integers xc and yc — the initial square's coordinates (1 ≤ xc ≤ n, 1 ≤ yc ≤ m).The third line contains an integer k (1 ≤ k ≤ 104) — the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≤ 109, |dx| + |dy| ≥ 1).

Output: Print the single number — the number of steps Vasya had made.Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.

Note: In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0,  - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps.In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard.

Sample Inputs:
4 5
1 1
3
1 1
1 1
0 -2

10 10
1 2
1
-1 0

Sample Outputs:
4

0

Solution (Python):
(n, m) = map(int, input().split())
(x, y) = map(int, input().split())
k = int(input())

def solve():
    global x, y
    count = 0
    for i in range(k):
        result = 0
        (dx, dy) = map(int, input().split())
        if x + dx > n or y + dy > m or x + dx <= 0 or (y + dy <= 0):
            continue
        if dx == 0:
            if dy != 0:
                if dy < 0:
                    result = (y - 1) // abs(dy)
                    y -= result * abs(dy)
                else:
                    result = (m - y) // dy
                    y += result * dy
        elif dy == 0:
            if dx != 0:
                if dx < 0:
                    result = (x - 1) // abs(dx)
                    x -= result * abs(dx)
                else:
                    result = (n - x) // dx
                    x += result * dx
        elif dx < 0:
            result1 = (x - 1) // abs(dx)
            if dy > 0:
                result2 = (m - y) // dy
                result = min(result1, result2)
                y += result * dy
                x -= result * abs(dx)
            else:
                result2 = (y - 1) // abs(dy)
                result = min(result1, result2)
                y -= result * abs(dy)
                x -= result * abs(dx)
        else:
            result1 = (n - x) // dx
            if dy > 0:
                result2 = (m - y) // dy
                result = min(result1, result2)
                y += result * dy
                x += result * dx
            else:
                result2 = (y - 1) // abs(dy)
                result = min(result1, result2)
                y -= result * abs(dy)
                x += result * abs(dx)
        count += result
    return count
print(solve())

Solution (Python):
(n, m) = map(int, input().split())
(x, y) = map(int, input().split())
k = int(input())
c = 0
for i in range(k):
    (s, d) = map(int, input().split())
    a = n + m
    if s > 0:
        a = min(a, (n - x) // s)
    if s < 0:
        a = min(a, (x - 1) // -s)
    if d > 0:
        a = min(a, (m - y) // d)
    if d < 0:
        a = min(a, (y - 1) // -d)
    c += a
    x += s * a
    y += d * a
print(c)

Solution (Python):
(n, m) = map(int, input().split())
(xc, yc) = map(int, input().split())
k = int(input())
total_steps = 0
for _ in range(k):
    (dx, dy) = map(int, input().split())
    if dx > 0:
        xsteps = (n - xc) // dx
    elif dx == 0:
        xsteps = int(1000000000.0)
    else:
        xsteps = (1 - xc) // dx
    if dy > 0:
        ysteps = (m - yc) // dy
    elif dy == 0:
        ysteps = int(1000000000.0)
    else:
        ysteps = (1 - yc) // dy
    steps = min(xsteps, ysteps)
    xc += steps * dx
    yc += steps * dy
    total_steps += steps
print(total_steps)