CF152B.Steps
普及-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
一天,瓦西娅到院子里散步,但院子里一个朋友都没有,他找不到人玩“抓人游戏”(Touch and Run)。不过,这孩子并没有因此情绪低落,而是决定自己一个人玩“抓人游戏”。你可能会问:“他怎么自己玩呢?”答案很简单。
瓦西娅注意到,院子是一个 n×m 的矩形场地。每个方格的坐标为 (x,y)(其中 1≤x≤n,1≤y≤m),x 表示行号,y 表示列号。
初始时,瓦西娅站在坐标为 (xc,yc) 的方格上。为了进行游戏,他手头有一份包含 k 个非零向量 (dxi,dyi) 的列表。游戏过程如下:瓦西娅按从 1 到 k 的顺序依次考虑每个向量,并将当前向量作为“当前向量”。选定当前向量后,他沿该向量方向尽可能多地执行合法的步数(也有可能一步都不走)。
所谓“一步”,是指从瓦西娅当前所在的方格出发,沿当前向量方向移动一次。即:若瓦西娅当前位于方格 (x,y),而当前向量为 (dx,dy),则一步会将其移动至方格 (x+dx,y+dy)。当且仅当执行该步后瓦西娅仍处于院子范围内(即不越界),该步才被视为合法。
瓦西娅就这样一步一步、一步一步地走,直到用完列表中的所有向量为止。他走了太久,以至于完全忘记了自己总共走了多少步。请帮帮他,计算他一共走了多少步。
输入格式
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).
第一行输入包含两个整数 n 和 m(1 ≤ n, m ≤ 109)—— 表示院子的尺寸。
第二行输入包含整数 xc 和 yc —— 初始方格的坐标(1 ≤ xc ≤ n, 1 ≤ yc ≤ m)。
第三行输入一个整数 k(1 ≤ k ≤ 104)—— 向量的个数。随后是 k 行,每行包含两个整数 dxi 和 dyi(满足 ∣dxi∣, ∣dyi∣ ≤ 109,且 ∣dxi∣ + ∣dyi∣ ≥ 1)。
输出格式
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.
输出唯一的数字——即瓦西娅所走的步数。
在 C++ 中,请勿使用 %lld 格式说明符来读取或写入 64 位整数。推荐使用 cin、cout 流,或 %I64d 格式说明符。
输入输出样例
输入#1
4 5 1 1 3 1 1 1 1 0 -2
输出#1
4
输入#2
10 10 1 2 1 -1 0
输出#2
0
说明/提示
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.
在第一个样例中,瓦西娅初始位于方格 (1, 1),并沿第一个向量 (1, 1) 移动 3 步。因此,他依次经过方格 (2, 2)、(3, 3)、(4, 4)。接着,他沿第二个向量 (1, 1) 移动 0 步。随后,他再沿第三个向量 (0, −2) 移动 1 步,最终到达方格 (4, 2)。总计,瓦西娅共移动了 4 步。
在第二个样例中,瓦西娅初始位于方格 (1, 2),由于坐标为 (0, 2) 的方格位于院子外部,因此他沿向量 (−1, 0) 移动 0 步。
输入解题思路,AI测评打分。不知道怎么写?