codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

152B.md (3181B)


      1 Source: https://codeforces.com/problemset/problem/152/B
      2 
      3 B. Steps
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 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.
     22 
     23 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.
     24 
     25 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).
     26 
     27 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.
     28 
     29 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.
     30 
     31 Input
     32 
     33 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*).
     34 
     35 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).
     36 
     37 Output
     38 
     39 Print the single number — the number of steps Vasya had made.
     40 
     41 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.
     42 
     43 Examples
     44 
     45 Input
     46 
     47 ```
     48 4 5  
     49 1 1  
     50 3  
     51 1 1  
     52 1 1  
     53 0 -2
     54 ```
     55 
     56 Output
     57 
     58 ```
     59 4
     60 ```
     61 
     62 Input
     63 
     64 ```
     65 10 10  
     66 1 2  
     67 1  
     68 -1 0
     69 ```
     70 
     71 Output
     72 
     73 ```
     74 0
     75 ```
     76 
     77 Note
     78 
     79 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.
     80 
     81 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.