This page is still under construction.

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

Avoiding Financial Nightmare

Interview

Time limit1sMemory limit128 MB

Summary
Given a loan principal, a term in months, and a monthly interest rate, find the fixed monthly payment so the balance hits zero at term end.
Level

Medium5 of 10

Topics
Binary search, Simulation, Math, Implementation
Solved
No attempts yet

Problem

Nowadays, taking out a loan from a bank or a financial company has become very common, whether for business or personal purposes. If you manage your spending well, a loan or a credit card can be a great help; otherwise, it can become your worst financial nightmare. A professor has decided to take out a loan to buy a new house in a pleasant city in Indonesia.

Three values affect his monthly bill:

  • Principal NN: the remaining amount of the loan
  • Period MM: the number of months needed to pay off the loan
  • Rate RR: the monthly interest rate (a percentage)

Banks and financial companies usually offer a fixed-payment plan. Every month the customer pays the same amount TT (the total monthly payment), which is made up of two parts. Let PP be the remaining principal in a given month.

The interest payment II is the rate applied to the remaining principal, rounded up to the nearest integer.

I=⌈R×P100⌉I = \left\lceil \frac{R \times P}{100} \right\rceil

The principal payment is the total monthly payment TT minus the interest payment, and the remaining principal decreases by that amount.

Pnew=P−(T−I)P_{\text{new}} = P - (T - I)

The total monthly payment TT must be the same every month, and it must be chosen so that when the period ends the remaining principal is 0 — or, if reaching exactly 0 is impossible, the negative amount nearest to 0. Find that total monthly payment TT.

For example, with a principal of $42,000, a monthly interest rate of 5%, and a 5-month term, the monthly payment is $9,701.

TermTotal PaymentInterest PaymentPrincipal PaymentRemaining Principal
----$42,000
1$9,701$2,100$7,601$34,399
2$9,701$1,720$7,981$26,418
3$9,701$1,321$8,380$18,038
4$9,701$902$8,799$9,239
5$9,701$462$9,239$0
  • Term 1: interest payment = ⌈5% × $42,000⌉ = $2,100, principal payment = $9,701 − $2,100 = $7,601, remaining principal = $42,000 − $7,601 = $34,399
  • Term 2: interest payment = ⌈5% × $34,399⌉ = $1,720, principal payment = $9,701 − $1,720 = $7,981, remaining principal = $34,399 − $7,981 = $26,418

After the 5th term the remaining principal is exactly $0.

Input

The input consists of several test cases. Each case is given as three integers NN, MM, and RR.

  • NN (1≤N≤100,000,0001 \le N \le 100{,}000{,}000): the initial principal
  • MM (1≤M≤1001 \le M \le 100): the period in months
  • RR (0≤R≤1000 \le R \le 100): the monthly interest rate as a percentage

Process every case until the end of input.

Output

For each case, print the total monthly payment that satisfies the condition on its own line. The value is unique: it is the smallest fixed monthly payment that makes the remaining principal 0, or the negative amount nearest to 0 if 0 is impossible, when the period ends.

Examples1

  1. Example 1

    Input
    42000 5 5
    100000 10 10
    
    Expected output
    9701
    16275