Investment
Time limit:1000 ms |
|
Memory limit:30000 K |
Total submissions:5362 |
|
Accepted:1843 |
Description
John never knew he had a grand-uncle, until he got the notary's letter. he learned that his late grand-uncle had gathered a lot of money, somewhere in South-America, and that John was the only inheritor.
John did not need that much money for the moment. but he realized that it wocould be a good idea to store this capital in a safe place, and have it grow until he decided to retire. the bank convinced him that a certain kind of bond was interesting for him.
This kind of bond has a fixed value, and gives a fixed amount of Yearly interest, payed to the owner at the end of each year. the bond has no fixed term. bonds are available in different sizes. the larger ones usually give a better interest. soon John realized that the optimal set of bonds to buy was not trivial to figure out. moreover, after a few years his capital wowould have grown, and the schedule had to be re-evaluated.
Assume the following bonds are available:
Value |
Annual Interest |
4000 3000 |
400 250 |
With a capital of E10 000 one cocould buy two bonds of $4 000, giving a yearly interest of $800. buying two bonds of $3 000, and one of $4 000 is a better idea, as it gives a yearly interest of $900. after two years the capital has grown to $11 800, and it makes sense to launch a $3 000 one and buy a $4 000 one, so the annual interest grows to $1 050. this is where this story grows unlikely: the bank does not charge for buying and selling bonds. next year the total sum is $12 850, which allows for three times $4 000, giving a yearly interest of $1 200.
Here is your problem: given an amount to begin with, a number of years, and a set of bonds with their values and interests, find out how big the amount may grow in the given period, using the best schedule for buying and selling bonds.
Input
The first line contains a single positive integer n which is the number of test cases. The test cases follow.
The first line of a test case contains two positive integers: the amount to start with (at most $1 000 000 ), and the number of years the capital may grow (at most 40 ).
The following line contains a single number: The number D (1 <= d <= 10) of available bonds.
The next D lines each contain the description of a bond. the description of a bond consists of two positive integers: the value of the bond, and the yearly interest for that bond. the value of a bond is always a multiple of $1 000. the interest of a bond is never more than 10% of its value.
Output
For each test case, output-on a separate line-the capital at the end of the period, after an optimal schedule of buying and selling.
Sample Input
110000 424000 4003000 250
Sample output
14050
Source
Northwestern Europe 2004 has a large data range, which is much larger than the estimate. The value of a bond is always a multiple of $1 000. this statement tells us that when we calculate the complete backpack, we can divide the total money by 1000 and the price of each bond by 1000, which can reduce the time. Note that the original money is not necessarily an integer multiple of 1000. This is a bit pitfall, and the interest is not an integer multiple of 1000. In addition, the original price and interest must be recorded. Make a full backpack every year .. Code 1:
# Include <stdio. h> # Include <Iostream> # Include < String . H> # Include <Algorithm> Using Namespace STD; Int F [ 5000000 ]; Int C [ 20 ], W [ 20 ]; Int Fun ( Int V, Int N) {v /= 1000 ; For ( Int I =0 ; I <n; I ++ ) For ( Int J = C [I]; j <= V; j ++ ) F [J] = Max (F [J], F [J-C [I] + W [I]); Return F [v];} Int Main (){ Int V, P; Int T; Int N; scanf ( " % D " ,& T ); While (T -- ) {Memset (F, 0 , Sizeof (F); scanf ( " % D " , & V ,& P); scanf ( " % D " ,& N ); For ( Int I = 0 ; I <n; I ++ ) {Scanf ( " % D " , & C [I], & W [I]); C [I] /= 1000 ;} Int Ans =V; For ( Int I = 0 ; I <p; I ++) ans + = Fun (ANS, n); printf ( " % D \ n " , ANS );} Return 0 ;}
Code 2:
# Include <stdio. h># Include <Iostream> # Include < String . H> # Include <Algorithm> Using Namespace STD; Int F [ 5010000 ]; Int V; Int C [ 200 ], W [ 200 ]; Void Completepack ( Int Cost, Int Weight ){ For ( Int V = cost; v <= V; V ++ ) F [v] = Max (F [v], F [V-cost] + Weight );} Int Main (){ // Freopen ("in.txt", "r", stdin ); // Freopen ("out.txt", "W", stdout ); Int T; Int N; scanf ( " % D " ,& T ); Int P; While (T -- ) {Scanf ( " % D " , & V ,&P ); Int Tempv = V; V /= 1000 ; Scanf ( " % D " ,& N ); For ( Int I = 0 ; I <n; I ++ ) {Scanf ( " % D " , & C [I], & W [I]); C [I] /= 1000 ;} Memset (F, 0 , Sizeof (F )); For ( Int I = 0 ; I <n; I ++ ) Completepack (C [I], W [I]); Int Sumtemp = F [v]; Int Temp = F [v]; Int Temp2 = (tempv + temp )/ 1000 ; // Printf ("% d \ n", temp, V, temp2 ); Int Tt = temp2- V; For ( Int I = 1 ; I <p; I ++ ){ If (Tt>0 ){ For ( Int J = 0 ; J <n; j ++ ) For ( Int V = C [J]; v <= temp2; V ++ ) F [v] = Max (F [v], F [V-C [J] + W [J]);} temp + = F [temp2]; TT = (Tempv + temp )/ 1000 -Temp2; temp2 = (Tempv + temp )/ 1000 ; // Printf ("% d \ n", temp, temp2, V ); } If (P = 0 ) Printf ( " % D \ n " , Tempv ); Else Printf ( " % D \ n " , Tempv + Temp );} Return 0 ;}