Loansome Car Buyer

Interview

Time limit1sMemory limit128 MB

Summary
Simulate monthly depreciation and payments on a car loan, and report the first month when the amount owed drops below the car's value.
Level

Easy3 of 10

Topics
Simulation, Implementation, Math, Array
Solved
No attempts yet

Problem

Kara Van and Lee Sabre are lonesome. A few months ago they took out a loan to buy a new car, but now they are stuck at home on a Saturday night with no wheels and no money. There was a wreck and the car was totaled. Their insurance paid $10,000, the current value of the car. The trouble is that they still owed the bank $15,000, and the bank wanted payment immediately, because there was no longer a car to serve as collateral. In just a few moments this unfortunate couple lost not only their car but an additional $5,000 in cash.

What Kara and Lee failed to account for was depreciation, the loss in value as a car ages. Each month the buyer's payment reduces the amount still owed on the car, but each month the car also loses value as it gets older. Your task is to compute the first month in which the buyer owes less money than the car is worth. Depreciation is given as a percentage of the previous month's value.

Input

The input describes several loans. Each loan starts with one line of four values: the duration of the loan in months, the down payment, the loan amount, and the number of depreciation records that follow. All values are nonnegative; a loan lasts at most 100 months and a car is worth at most $75,000.

Because depreciation is not constant, the changing rates are given as depreciation records. Each record is one line with a month number and a depreciation percentage that is greater than 0 and less than 1. The records are strictly increasing by month and start at month 0. The month-0 record is the depreciation that applies immediately after the car is driven off the lot and is always present. Every other record is the depreciation at the end of that month. Not every month appears; if a month is missing, the most recent listed percentage still applies.

The end of the input is marked by a line whose loan duration is negative; the other three values on that line are present but meaningless.

Assume a 0% interest loan, so the car's initial value equals the loan amount plus the down payment, and each monthly payment equals the loan amount divided by the duration. Both the car's value and the amount owed may be positive numbers smaller than $1.00. Do not round values to whole cents ($7,347.635 must not become $7,347.64).

As an illustration, suppose you borrow $15,000 for 30 months with a $500 down payment. When the buyer drives off the lot he still owes $15,000, but the car has already dropped 10% in value to $13,950. After 4 months the buyer has made four payments of $500 each, and the car has further depreciated 3% in months 1 and 2 and 0.2% in months 3 and 4. At that point the car is worth $13,073.10528 while the borrower owes only $13,000.

Output

For each loan, print the number of complete months that pass before the borrower owes less than the car is worth. Use English pluralization: print 1 month when the value is one, and N months (for example, 5 months) for every other value.

Examples1

  1. Example 1

    Input
    30 500.0 15000.0 3
    0 .10
    1 .03
    3 .002
    12 500.0 9999.99 2
    0 .05
    2 .1
    60 2400.0 30000.0 3
    0 .2
    1 .05
    12 .025
    -99 0 17000 1
    
    Expected output
    4 months
    1 month
    49 months