300796: CF152B. Steps
Memory Limit:256 MB
Time Limit:2 S
Judge Style:Text Compare
Creator:
Submit:0
Solved:0
Description
Steps
题目描述
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.输入输出格式
输入格式
The first input line contains two integers $ n $ and $ m $ ( $ 1<=n,m<=10^{9} $ ) — 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<=10^{4} $ ) — the number of vectors. Then follow $ k $ lines, each of them contains two integers $ dx_{i} $ and $ dy_{i} $ $ (|dx_{i}|,|dy_{i}|<=10^{9},|dx|+|dy|>=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.
输入输出样例
输入样例 #1
4 5
1 1
3
1 1
1 1
0 -2
输出样例 #1
4
输入样例 #2
10 10
1 2
1
-1 0
输出样例 #2
0