Average Annual Return ------- ------ ------ You have a brokerage account and you let your broker buy and sell stocks and bonds in the account for your benefit. You want to know the average annual return of the account between any two times. So what do we mean by `average annual return'? Suppose we have an account that at the beginning of year y contains V(y) dollars, and suppose we do NOT make any deposits or withdrawals in the account. Then the average annual return A(y1,y2) from the beginning of year y1 to the beginning of year y2 is such that: V(y2) = V(y1) * ( 1 + A(y1,y2)/100 )**(y2 - y1) Note that A(.,.) is a percentage. we can solve for it by taking the logarithms of both sides of this equation. However, what if we make deposits and withdrawals? Suppose at the beginning of year y when the account has value V(y) we deposit D(y) dollars and withdraw W(y) dollars. Immediately afterwards the account has V(y) + D(y) - W(y) dollars. Then how do we define A(y1,y2). We define it by defining a virtual account that has v(y) dollars at the beginning of each year (before deposits and withdrawals), and is such that v(y1) = V(y1) for y1 <= y < y2: v(y+1) = (1 + A(y1,y2)/100) * (v(y) + D(y) - W(y)) v(y2) = V(y2) Thus the virtual account earns at a constant rate A(y1,y2) that is independent of the year in between y1 and y2, and in this sense is the average annual rate from y1 to y2. Note that for y1 < y < y2, v(y) is NOT required to equal V(y). When there are deposits and withdrawals, it is hard to calculate A(y1,y2) without the aid of a computer. So of course we want you to compute A(y1,y2). However, you actually make deposits and withdrawals on the first of each month, so we want you to deal in months m1, m2 and compute the average monthly growth rate M(m1,m2) instead of the annual rate. But as monthly rates are hard to understand, we want you to report the annual rate A(m1,m2) defined by solving the equation: 1 + A(m1,m2)/100 = (1 + M(m1,m2)/100)**12 Input ----- A sequence of test cases. Each test case begins with a line containing the test case name. The next line has the form N Q where N is the number of months and Q the number of queries. This is followed by N lines of the form: V D W where for the I'th line V is the actual value of the account at the beginning of month I just before you deposit D and withdraw W. For simplicity, V, D, and W are non-negative integers. This is followed by Q lines each of the form: m1 m2 which represents a query asking for the average annual rate of return from the beginning of month m1 to the beginning of the month m2. 2 <= N <= 241 1 <= Q <= 20 0 <= V,D,W <= 1,000,000 V + D - W >= 0 1 <= m1 < m1 <= N Input ends with an end of file. The test case name is at most 80 characters. Input will be such that the virtual value will never be negative and -20% <= A(m1,m2) <= 20% (which implies that -2% <= M(m1,m2) <= +2%). Output ------ For each test case, first an exact copy of the test case name line. Then for each query one line containing: m1 m2 A where `m1 m2' copies the query input line and A is the average annual rate of return from the beginning of month m1 to the beginning of month m2. `A' should be accurate to two decimal places. Note that A can be negative. Sample Input ------ ----- -- SAMPLE 1 -- 13 2 10000 0 0 10100 0 0 10000 100 0 9800 0 100 10000 0 0 10100 50 0 10000 0 0 9700 0 50 10400 0 200 10000 200 0 9900 0 0 10100 0 0 10500 0 0 1 13 2 11 [ see sample.in for more sample input ] Sample Output ------ ------ -- SAMPLE 1 -- 1 13 5.00 2 11 -2.63 [ see sample.test for more sample output ] File: annual.txt Author: Bob Walton Date: Sun Oct 13 20:50:47 EDT 2019 The authors have placed this file in the public domain; they make no warranty and accept no liability for this file.