Safe Zone
InterviewTime limit1sMemory limit256 MB
Count the K-step walks on segments 0 to N-1 with forced turns at the ends that finish inside segments P to Q.
- Level
Easy3 of 10
- Topics
- Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
Saruman's army has laid siege to Helm's Deep and is firing volley after volley of arrows at its walls. The Rohirrim have shields for only one stretch of the wall. You are a soldier in King Theoden's army, and you have to reach that stretch before the next volley lands.
From left to right the wall segments are numbered through , and you are standing on segment . The shields cover segments through . Before the next volley you have time for exactly steps, and each step moves you one segment left or one segment right. The wall shakes too hard for you to stand still.
Count the sequences of steps that leave you on a segment between and . Two sequences that differ in any step are counted separately.
Input
The input holds several test cases. Each test case is one line with , , , , and separated by spaces. is the length of the wall, and are the ends of the safe zone, is the starting position, and is the number of steps. A line whose is marks the end of the input and produces no output.
You cannot walk off the end of the wall. On segment you must step right, and on segment you must step left.
- There are at most 100 test cases.
- The last line is always
0 0 0 0 0.
Output
For each test case, print on its own line the number of ways to end up in the safe zone after steps. The answer fits in a signed 64-bit integer.