B. Steps

来源:互联网 发布:java动态网页 编辑:程序博客网 时间:2024/05/16 14:56

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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).

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 dxiand 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 cincout streams or the%I64d specificator.

Sample test(s)
input
4 51 131 11 10 -2
output
4
input
10 101 21-1 0
output
0
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.


解题说明:此题是一个临界判断问题。针对每一个方向向量,判断走多少步出界,依次针对每个向量判断其最大步数即可。


#include <iostream>#include<algorithm>#include<cstdio>#include<cmath>#include<cstring>#include<cstdlib>using namespace std;int main(){    int n, m, x, y, k, dx, dy, tx, ty, t;    long long s = 0;    scanf("%d%d%d%d%d", &n, &m, &x, &y, &k);    while (k--){          scanf("%d%d", &dx, &dy);          if (dx == 0)  {  tx = 2000000000;  }  else if (dx < 0) { tx = (x - 1) / -dx;  }  else  {  tx = (n - x) / dx;  }  if (dy == 0)  {  ty = 2000000000;  }  else if (dy < 0)  {  ty = (y - 1) / -dy;  }          else   { ty = (m - y) / dy;  }  t = tx < ty ? tx : ty;          x += dx * t;  y += dy * t;          s += t;    }    printf("%I64d\n", s);    return 0;}


原创粉丝点击