Problem Description
Kara Van and Lee Sabre are lonesome. A few months ago they took out a loan to buy a new car, but now they're stuck at home on Saturday night without wheels and without money. You see, there was a wreck and the car was totaled. Their insurance paid 10,000,thecurrentvalueofthecar.Theonlyproblemisthattheyowedthebank15,000, and the bank wanted payment immediately, since there was no longer a car for collateral. In just a few moments, this unfortunate couple not only lost their car, but lost an additional $5,000 in cash too.
What Kara and Lee failed to account for was depreciation, the loss in value as the car ages. Each month the buyer's loan payment reduces the amount owed on the car. However, each month, the car also depreciates as it gets older. Your task is to write a program that calculates the first time, measured in months, that a car buyer owes less money than a car is worth. For this problem, depreciation is specified as a percentage of the previous month's value.
Input
Input consists of information for several loans. Each loan consists of one line containing the duration in months of the loan, the down payment, the amount of the loan, and the number of depreciation records that follow. All values are nonnegative, with loans being at most 100 months long and car values at most 75,000.Sincedepreciationisnotconstant,thevaryingratesarespecifiedinaseriesofdepreciationrecords.Eachdepreciationrecordconsistsofonelinewithamonthnumberanddepreciationpercentage,whichismorethan0andlessthan1.Theseareinstrictlyincreasingorderbymonth,startingatmonth0.Month0isthedepreciationthatappliesimmediatelyafterdrivingthecaroffthelotandisalwayspresentinthedata.Alltheotherpercentagesaretheamountofdepreciationattheendofthecorrespondingmonth.Notallmonthsmaybelistedinthedata.Ifamonthisnotlisted,thenthepreviousdepreciationpercentageapplies.Theendoftheinputissignalledbyanegativeloanduration−theotherthreevalueswillbepresentbutindeterminate.Forsimplicity,wewillassumea01.00. Do not round values to a whole number of cents (7,347.635shouldnotberoundedto7,347.64).
Consider the first example below of borrowing 15,000for30months.Asthebuyerdrivesoffthelot,hestillowes15,000, but the car has dropped in value by 10% to 13,950.After4months,thebuyerhasmade4payments,eachof500, and the car has further depreciated 3% in months 1 and 2 and 0.2% in months 3 and 4. At this time, the car is worth 13,073.10528andtheborroweronlyowes13,000.
Output
For each loan, the output is the number of complete months before the borrower owes less than the car is worth. Note that English requires plurals (5 months) on all values other than one (1 month).
Sample 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
Sample Output
4 months
1 month
49 months