#P1011. Train Passenger Count
Train Passenger Count
Train Passenger Count
A train departs from the first station (station (1)) with (a) passengers boarding. When the train reaches the second station, some passengers board and the same number disembark, so that the number of passengers remains unchanged (i.e. still (a)) when the train departs from station (2) (before arriving at station (3)). From the third station onward (starting at station (3)) up to station (n-1), the boarding and disembarking follow a specific pattern: the number of passengers boarding at station (i) equals the sum of the boarding counts of the previous two stations, i.e., [ b_i = b_{i-1} + b_{i-2},\quad \text{for } 3 \le i \le n-1,] while the number of passengers disembarking at station (i) is equal to the number of passengers who boarded at the previous station, i.e., [ d_i = b_{i-1}.] Consequently, if we denote (p_i) as the number of passengers on board immediately after leaving station (i), we have [ p_1 = a,\quad p_2 = a,\quad p_i = p_{i-1} + b_{i-2} \quad \text{for } 3 \le i \le n-1.] In particular, if we assume that (b_1 = a) and (b_2 = a), then the recurrence yields [ p_3 = 2a,; p_4 = 3a,; p_5 = 5a,\text{ etc.}] At the final station (station (n)), all remaining passengers disembark. It is given that (m) passengers get off at station (n), so [ m = p_{n-1}.] Given the total number of stations (n), the number of passengers boarding at the first station (a), the number of passengers disembarking at the last station (m), and an integer (x) representing a station number, your task is to calculate the number of passengers on board immediately after the train departs from station (x).
Note: Since at station (n) the train is emptied, if (x = n) then the answer is (0).
inputFormat
The input consists of four space-separated integers:
n
(the total number of stations),a
(the number of passengers boarding at the first station),m
(the number of passengers disembarking at the last station, which equals the number of passengers on board just before stationn
), andx
(the station number after which the number of on-board passengers is to be reported).
You can assume that the provided values satisfy the relation \(m = a \times F_{n-1}\) where \(F_{n-1}\) is defined by \(F_1 = 1,\; F_2 = 1,\; F_3 = 2, \ldots\).
outputFormat
Print a single integer representing the number of passengers on board immediately after the train departs from station x
. If \(x = n\), output 0
because all passengers disembark at the last station.
sample
5 2 6 3
4