Fence Making

Time limit1sMemory limit128 MB

Summary
For each integer radius and spacing pair, count holes drilled through repeated remelting of the strip, then sum C(d,r,S) over all pairs.
Level

Hard8 of 10

Topics
Math, Implementation, Simulation, Brute force
Solved
No attempts yet

Problem

The city of Haka is famous for its traffic jams. All that idle time in traffic has turned many of its citizens into problem setters. Along the dividers of Haka's wide roads run fences made of perforated steel sheets, and this problem is about how those sheets are made.

Every perforated steel strip has exactly two circular holes in each row. The pattern rules are:

  • Every hole is a circle of radius rr.
  • The two holes in the same row are 2d2d apart, and two consecutive holes in the same column are also 2d2d apart.
  • The distance from every hole to its nearest side of the strip is dd.

Because of these rules the width of every strip is exactly 4d+4r4d + 4r. The length of the initial sheet is SS.

A hole is drilled only where a full circle fits under the rules above. Once a sheet has been drilled, the circular pieces taken from the holes, together with the unused part of the sheet (for example the portion below the finished region), are melted down and cast into a new strip of the same width 4d+4r4d + 4r. Holes are drilled into that new strip under the same rules, and the process repeats. It stops as soon as the freshly cast strip becomes too short to hold even one row of two holes.

Let C(d,r,S)C(d, r, S) be the total number of holes drilled during the entire process. Given the minimum radius rminr_{min}, the maximum radius rmaxr_{max}, the minimum spacing dmind_{min}, the maximum spacing dmaxd_{max} and the length SS, compute

∑r=rminrmax∑d=dmindmaxC(d,r,S)\sum_{r=r_{min}}^{r_{max}}\sum_{d=d_{min}}^{d_{max}} C(d, r, S)

Here dd and rr are always integers. Assume the initial strip and every strip cast afterwards have the same uniform thickness everywhere. Your method must be efficient.

Perforated steel strip

Input

The input contains up to 1000 data sets. Each data set is a single line of five integers rminr_{min}, rmaxr_{max}, dmind_{min}, dmaxd_{max} and SS, with 5000≤rmin≤100005000 \le r_{min} \le 10000, 0≤rmax−rmin≤10000 \le r_{max} - r_{min} \le 1000, 1≤dmin≤211 \le d_{min} \le 21, 0≤dmax−dmin≤1000 \le d_{max} - d_{min} \le 100 and 1000000≤S≤20000000001000000 \le S \le 2000000000. All of rr and dd are integers.

The input ends with a line of five zeros, which must not be processed.

Output

For each data set print a single line containing the integer

∑r=rminrmax∑d=dmindmaxC(d,r,S)\sum_{r=r_{min}}^{r_{max}}\sum_{d=d_{min}}^{d_{max}} C(d, r, S)

The answer fits comfortably in a signed 64-bit integer.

Examples2

  1. Example 1

    Input
    9682 9719 18 29 71757646
    5746 5958 19 24 1942485264
    0 0 0 0 0
    
    Expected output
    15404518
    1918408970
    
  2. Example 2

    Input
    5000 5000 1 1 1000000
    0 0 0 0 0
    
    Expected output
    922