This page is still under construction.

Parts of this page are still being built. What you see may change.

Geometric Inflation

Time limit1sMemory limit128 MB

Summary
Simulate the monthly price recurrence with half-up cent rounding and print the Nth month cost with comma grouping.
Level

Easy2 of 10

Topics
Simulation, Math, Implementation
Solved
No attempts yet

Problem

This is a very unusual model of inflation. The cost of an item this month is the cost three months ago multiplied by the cost two months ago, divided by the cost one month ago.

cn=cn−3×cn−2cn−1c_n = \frac{c_{n-3} \times c_{n-2}}{c_{n-1}}

Costs are given in dollars, and the digits after the decimal point are cents, so there are never more than two of them. No cost can include a partial cent, so round the result for every month to the nearest whole cent. A result that lands exactly halfway between two cents rounds up. Rounding happens once per month, after the division. The product is not rounded before it is divided.

Suppose dumplings from the corner shop cost $5.23 in month 1, $5.50 in month 2, and $5.52 in month 3. The cost in month 4 is 5.23 multiplied by 5.50, which is 28.765, divided by 5.52, which is 5.2110..., and that rounds to $5.21.

Input

The input has several lines. Each line starts with three decimal numbers, the costs in month 1, month 2, and month 3. All three costs are positive and are written with one or two digits after the decimal point. The line then holds an integer NN, the month whose cost you have to find, with 1≤N≤1001 \le N \le 100.

A line starting with -1 marks the end of the input, and that line is not processed.

The input never makes the cost of any month from month 1 through month NN equal $0.00.

Output

For each line print the cost of month NN on its own line, in the form "Month N cost: $c". Put the month number from the input where N is, and the cost in dollars where c is.

Print exactly two digits after the decimal point, and group the whole-dollar part with a comma every three digits counting from the right. 1234567.89 dollars prints as 1,234,567.89, and 1000 dollars prints as 1,000.00.

Every cost you are asked for fits in the range of a double precision floating point number.

Examples2

  1. Example 1

    Input
    5.23 5.5 5.52 4
    5.23 5.5 5.52 2
    -1
    
    Expected output
    Month 4 cost: $5.21
    Month 2 cost: $5.50
    
  2. Example 2

    Input
    0.03 0.05 0.02 4
    0.03 0.04 0.02 4
    -1
    
    Expected output
    Month 4 cost: $0.08
    Month 4 cost: $0.06