Under construction

Still being built. Things may change

Keep on Truckin'

Interview

Time limit1sMemory limit128 MB

Summary
Given fixed and added motel distances, count overnight stop sequences where each day covers between A and B km.
Level

Medium4 of 10

Topics
Dynamic programming, Array, Sorting
Solved
No attempts yet

Problem

A truck driver plans to drive along the Trans-Canada Highway from Vancouver to St. John's, a distance of 70007000 km, stopping each night at a motel.

The following motels are always available, listed by their distance in km from the starting point in Vancouver:

0, 990, 1010, 1970, 2030, 2940, 3060, 3930, 4060, 4970, 5030, 5990, 6010, 7000

More motels may be added just before the trip begins.

The driver can complete the journey only if all of the following hold:

  1. each day the driver travels at least AA km (a company rule),
  2. each day the driver travels at most BB km (a legal limit), and
  3. every night is spent at an eligible motel (either from the fixed list above or from the additional locations given in the input).

The trip starts at km 00 (Vancouver) and ends at km 70007000 (St. John's). Count the number of different ways the driver can choose the sequence of overnight motels so that every daily distance is between AA and BB, inclusive.

For instance, with no added motels: if A=1A = 1 and B=500B = 500 the trip is impossible, so the answer is 00; if A=970A = 970 and B=1030B = 1030 there is exactly one plan; and if A=970A = 970 and B=1040B = 1040 there are four plans. If A=970A = 970, B=1030B = 1030 and one extra motel is added at km 49604960, there are two plans.

Input

  • The first line contains the minimum daily distance AA.
  • The second line contains the maximum daily distance BB, with 1≤A≤B≤70001 \le A \le B \le 7000; both are integers.
  • The third line contains an integer NN (0≤N≤200 \le N \le 20), the number of additional motels.
  • Each of the next NN lines contains one integer mm (0<m<70000 < m < 7000), the distance of an additional motel from Vancouver.

No two motels are located at the same distance from Vancouver.

Output

Print a single integer: the number of different ways the driver can choose the overnight motels to complete the trip under the given constraints.

Examples4

  1. Example 1

    Input
    1
    500
    0
    
    Expected output
    0
    
  2. Example 2

    Input
    970
    1030
    0
    
    Expected output
    1
    
  3. Example 3

    Input
    970
    1040
    0
    
    Expected output
    4
    
  4. Example 4

    Input
    970
    1030
    1
    4960
    
    Expected output
    2