Under construction

Still being built. Things may change

Safe Zone

Interview

Time limit1sMemory limit256 MB

Summary
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 00 through N−1N-1, and you are standing on segment JJ. The shields cover segments PP through QQ. Before the next volley you have time for exactly KK 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 KK steps that leave you on a segment between PP and QQ. Two sequences that differ in any step are counted separately.

Input

The input holds several test cases. Each test case is one line with NN, PP, QQ, JJ, and KK separated by spaces. NN is the length of the wall, PP and QQ are the ends of the safe zone, JJ is the starting position, and KK is the number of steps. A line whose NN is 00 marks the end of the input and produces no output.

You cannot walk off the end of the wall. On segment 00 you must step right, and on segment N−1N-1 you must step left.

  • 2≤N≤10002 \le N \le 1000
  • 0≤P≤Q≤N−10 \le P \le Q \le N-1
  • 0≤J≤N−10 \le J \le N-1
  • 1≤K≤601 \le K \le 60
  • 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 KK steps. The answer fits in a signed 64-bit integer.

Examples2

  1. Example 1

    Input
    4 0 1 2 2
    4 0 1 2 3
    16 2 3 7 13
    0 0 0 0 0
    
    Expected output
    1
    3
    702
    
  2. Example 2

    Input
    2 0 0 0 1
    2 0 0 1 1
    2 1 1 0 1
    0 0 0 0 0
    
    Expected output
    0
    1
    1