codeforces-scrape

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

101B.md (2637B)


      1 Source: https://codeforces.com/problemset/problem/101/B
      2 
      3 B. Buses
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 265 megabytes
     12 
     13 input
     14 
     15 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 Little boy Gerald studies at school which is quite far from his house. That's why he has to go there by bus every day. The way from home to school is represented by a segment of a straight line; the segment contains exactly *n* + 1 bus stops. All of them are numbered with integers from 0 to *n* in the order in which they follow from Gerald's home. The bus stop by Gerald's home has number 0 and the bus stop by the school has number *n*.
     22 
     23 There are *m* buses running between the house and the school: the *i*-th bus goes from stop *s**i* to *t**i* (*s**i* < *t**i*), visiting all the intermediate stops in the order in which they follow on the segment. Besides, Gerald's no idiot and he wouldn't get off the bus until it is still possible to ride on it closer to the school (obviously, getting off would be completely pointless). In other words, Gerald can get on the *i*-th bus on any stop numbered from *s**i* to *t**i* - 1 inclusive, but he can get off the *i*-th bus only on the bus stop *t**i*.
     24 
     25 Gerald can't walk between the bus stops and he also can't move in the direction from the school to the house.
     26 
     27 Gerald wants to know how many ways he has to get from home to school. Tell him this number. Two ways are considered different if Gerald crosses some segment between the stops on different buses. As the number of ways can be too much, find the remainder of a division of this number by 1000000007 (109 + 7).
     28 
     29 Input
     30 
     31 The first line contains two space-separated integers: *n* and *m* (1 ≤ *n* ≤ 109, 0 ≤ *m* ≤ 105). Then follow *m* lines each containing two integers *s**i*, *t**i*. They are the numbers of starting stops and end stops of the buses (0 ≤ *s**i* < *t**i* ≤ *n*).
     32 
     33 Output
     34 
     35 Print the only number — the number of ways to get to the school modulo 1000000007 (109 + 7).
     36 
     37 Examples
     38 
     39 Input
     40 
     41 ```
     42 2 2  
     43 0 1  
     44 1 2
     45 ```
     46 
     47 Output
     48 
     49 ```
     50 1
     51 ```
     52 
     53 Input
     54 
     55 ```
     56 3 2  
     57 0 1  
     58 1 2
     59 ```
     60 
     61 Output
     62 
     63 ```
     64 0
     65 ```
     66 
     67 Input
     68 
     69 ```
     70 5 5  
     71 0 1  
     72 0 2  
     73 0 3  
     74 0 4  
     75 0 5
     76 ```
     77 
     78 Output
     79 
     80 ```
     81 16
     82 ```
     83 
     84 Note
     85 
     86 The first test has the only variant to get to school: first on bus number one to the bus stop number one; then on bus number two to the bus stop number two.
     87 
     88 In the second test no bus goes to the third bus stop, where the school is positioned. Thus, the correct answer is 0.
     89 
     90 In the third test Gerald can either get or not on any of the first four buses to get closer to the school. Thus, the correct answer is 24 = 16.