训练赛3_J_Steps

Steps

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 (x**c, y**c). To play, he has got a list of k vectors (dx**i, dy**i) 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 x**c and y**c — the initial square’s coordinates (1 ≤ x**c ≤ n, 1 ≤ y**c ≤ 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 dx**i and dy**i (|dx**i|, |dy**i| ≤ 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 %I64dspecificator.

Examples

input

Copy

4 5
1 1
3
1 1
1 1
0 -2

output

Copy

4

input

Copy

10 10
1 2
1
-1 0

output

Copy

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.

题目大意

有一幅地图,地图的大小是 n × m n \times m ( 1 , 1 ) (1,1) 是起点,然后给一个初始的位置给你,再给个 k k 个向量,表示接下来的步伐的大小与方向,每读取一个向量,就往那个方向走到不能走。然后读取下一个变量,继续走。最后输出所走的步数。

题目分析

模拟题,直接模拟就好。但是有几个重要的点要注意一下

  • 题目要求用I64d输出,这个要注意。
  • 注意向量中的正负问题,是用当前坐标除还是用地图边界减去当前坐标除
  • 注意坐标是从 ( 1 , 1 ) (1,1) 开始。不能整除,坐标要剩下一点,所以要先减1再除。
  • 向量不用存。边读边加。

代码

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef struct{
  int x, y;
}node;
typedef long long ll;
const int inf = 0x3f3f3f3f;
int n, m, x, y, k;
ll f(node v){
  int a = inf, b = inf;
  if(v.x < 0)
    a = (x - 1) / abs(v.x);
  else if(v.x > 0)
    a = (n - x) / abs(v.x);
  if(v.y < 0)
    b = (y - 1) / abs(v.y);
  else if(v.y > 0)
    b = (m - y) / abs(v.y);
  int mn = min(a, b);
  x += mn * v.x; y += mn * v.y;
  return mn;
}
int main(int argc, char const *argv[]) {
  scanf("%d%d%d%d", &n, &m, &x, &y);
  scanf("%d", &k);
  ll ans = 0;
  for(int i = 0; i < k; i++){
    node t;
    scanf("%d%d", &t.x, &t.y);
    ans += f(t);
  }
  printf("%I64d\n", ans);
  return 0;
}

猜你喜欢

转载自blog.csdn.net/IT_w_TI/article/details/88144862
今日推荐