Investment

Investment
Time Limit: 1000MS
Memory Limit: 30000K
Total Submissions: 8398
Accepted: 2869

Description

John never knew he had a grand-uncle, until he received 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 would 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 would have grown, and the schedule had to be re-evaluated. 
Assume the following bonds are available: 
ValueAnnual
interest
4000
3000
400
250

With a capital of e10 000 one could 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 sell 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

1
10000 4
2
4000 400
3000 250

Sample Output

14050

<span style="font-size:18px;">#include<stdio.h>
#include<string.h>
int max(int a,int b)
{
    return a>b?a:b;
}
int ben[10001],li[10001];
int dp[100001];
int main()
{
    int t,n,m,k,i,j,p;
    int sum;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&sum,&n);
        scanf("%d",&k);
        for(i=0; i<k; i++)
        {
            scanf("%d%d",&ben[i],&li[i]);
            ben[i]/=1000;
        }

        m=sum;
        for(p=0; p<n; p++)
        {
            m=sum;
            m/=1000;
            memset(dp,0,sizeof(dp));
            for(i=0; i<k; i++)
            {
                for(j=ben[i]; j<=m; j++)
                {
                    dp[j]=max(dp[j],dp[j-ben[i]]+li[i]);
                }
            }
            sum+=dp[m];
        }
        printf("%d\n",sum);
    }

}
</span>


%% capital coefficient matrix % I get the data of capital stock, investment, and depreciation from % EUKLEMS Capital input database, the raw data is valued in national currency %% major variables obtained in this code file: % euklems_capital_stock, euklems_investment, euklems_depreciation % all in the dimension of 1230 * 1230 * 13 cd ..\data\EUKLEMS\capital %% import data from EUKLEMS Capital Input excels % create matrices to load capital stock data capital_stock_euklems_bycountry (loading the raw data from excel) % each excel representing one country; within each excel file, each sheet representing one asset classes % in each matrix of one sheet, conlumn is for year and row is for using industry % m is a list for assets classes of 8 in euklems data, residential structure is not used, need to check how to deal with the 'other' % gross capital formation is obtained here. % the depreciation, named "consumption of fixed capital in EUKLEMS", labeled as D_ in the excel file, is also obtained here. % all three variables are in 1995 prices %% Asset class for capital stock (real), real investment (capital formation), % nominal investment, depreciation (real), and capital compensation % The code for the countries is based on the alphabetical order of the % acronom of the countries. There are 14 countries contained in the EUKLEMS database. % those excels contains viarous sheet tabs. Those in the list are just the % tab names in all of those excel files. capital_stock_sheets = ["K_IT", "K_CT", "K_Soft", "K_TraEq", "K_OMach", "K_OCon", "K_RStruc", "K_Other"]; real_investment_sheets = ["Iq_IT", "Iq_CT", "Iq_Soft", "Iq_TraEq", "Iq_OMach", "Iq_OCon", "Iq_RStruc", "Iq_Other"]; depreciation_sheets = ["D_IT", "D_CT", "D_Soft", "D_TraEq", "D_OMach", "D_OCon", "D_RStruc", "D_Other"]; nominal_investment_sheets = ["I_IT", "I_CT", "I_Soft", "I_TraEq", "I_OMach", "I_OCon", "I_RStruc", "I_Other"]; capital_compensation_sheets = ["CAP_IT", "CAP_CT", "CAP_Soft", "CAP_TraEq", "CAP_OMach", "CAP_OCon", "CAP_RStruc", "CAP_Other"]; % Country indices, 21 and 24 represent Japan and Korea which have some % different data structure country_excel_indices = [1, 2, 4, 9, 10, 11, 12, 14, 22, 23, 30, 36, 37, 40]; num_countries_euklems = length(country_excel_indices); num_assetsclass = 8; num_years = 13; % need to check where I deal with country 21 and 24 % Initialize 4-dimensional matrices to store data % Calculate the rate of return: compensation over capital stock capital_stock_bycountry=zeros(37, num_years, num_assetsclass, num_countries_euklems); real_investment_bycountry=zeros(37, num_years, num_assetsclass, num_countries_euklems); depreciation_bycountry=zeros(37, num_years, num_assetsclass, num_countries_euklems); nominal_investment_bycountry=zeros(37, num_years, num_assetsclass, num_countries_euklems); capital_compensation_bycountry=zeros(37, num_years, num_assetsclass, num_countries_euklems); % import data from excel using the function table2array for i=1:num_countries_euklems % Create the country-specific Excel file name str_country = [ '',num2str(country_excel_indices(i)),'.xls']; for w=1:num_assetsclass capital_stock_bycountry(:,:,w,i) = readmatrix(str_country,'Sheet',capital_stock_sheets(w),'Range','AB3:AN39'); % AB3:AN39 is from 1995 to 2007 for all industries real_investment_bycountry(:,:,w,i) = readmatrix(str_country,'Sheet',real_investment_sheets(w),'Range','AB3:AN39'); depreciation_bycountry(:,:,w,i)= readmatrix(str_country,'Sheet',depreciation_sheets(w),'Range','AB3:AN39'); nominal_investment_bycountry(:,:,w,i) = readmatrix(str_country,'Sheet',nominal_investment_sheets(w),'Range','AB3:AN39'); capital_compensation_bycountry(:,:,w,i) = readmatrix(str_country,'Sheet',capital_compensation_sheets(w),'Range','AB3:AN39'); end end %% replace the missing value with 0 % Replace infinite values and NaN values in the created matrices capital_stock_bycountry(isinf(capital_stock_bycountry))=0; capital_stock_bycountry(isnan(capital_stock_bycountry))=0; real_investment_bycountry(isinf(real_investment_bycountry))=0; real_investment_bycountry(isnan(real_investment_bycountry))=0; depreciation_bycountry(isinf(depreciation_bycountry))=0; depreciation_bycountry(isnan(depreciation_bycountry))=0; nominal_investment_bycountry(isinf(nominal_investment_bycountry))=0; nominal_investment_bycountry(isnan(nominal_investment_bycountry))=0; capital_compensation_bycountry(isinf(capital_compensation_bycountry))=0; capital_compensation_bycountry(isnan(capital_compensation_bycountry))=0; % now the dimension of the matrix: industry, year, assets class, country % for example, capital_stock_bycountry(:,:,1,1) gives how much IT capital stock that 37 sectors use in production over 13 years in Austria. % Check for NaN or Inf values in the matrices if any(isnan(capital_stock_bycountry(:))) || any(isinf(capital_stock_bycountry(:))) disp('capital_stock_bycountry contains NaN or Inf values'); end if any(isnan(depreciation_bycountry(:))) || any(isinf(depreciation_bycountry(:))) disp('depreciation_bycountry contains NaN or Inf values'); end if any(isnan(real_investment_bycountry(:))) || any(isinf(real_investment_bycountry(:))) disp('real_investment_bycountry contains NaN or Inf values'); end if any(isnan(nominal_investment_bycountry(:))) || any(isinf(nominal_investment_bycountry(:))) disp('real_investment_bycountry contains NaN or Inf values'); end if any(isnan(capital_compensation_bycountry(:))) || any(isinf(capital_compensation_bycountry(:))) disp('capital_compensation_bycountry contains NaN or Inf values'); end %% permutate the capital stock data to one sorted by country and then year and lastly a matrix of asset category and industry % Move the 3rd dimension (asset classes) to the 1st position. % Move the 1st dimension (using industries) to the 2nd position. % Move the 4th dimension (countries) to the 3rd position. % Move the 2nd dimension (years) to the 4th position. % after permutation, dimension: [assets classes, using-industries, using-countries, years] capital_stock_37=permute(capital_stock_bycountry,[3 1 4 2]); real_investment_37=permute(real_investment_bycountry,[3 1 4 2]); depreciation_37=permute(depreciation_bycountry,[3 1 4 2]); nominal_investment_37=permute(nominal_investment_bycountry,[3 1 4 2]); capital_compensation_37=permute(capital_compensation_bycountry,[3 1 4 2]); %% select 30 industries form 37, leaving out the already aggregated thus repeating industries % Create a mapping array for the columns to be selected col_mapping = [1:2, 4:7, 9:19, 21:24, 26:27, 29, 31:32, 34:37]; % select data for capital_stock, investment, and depreciation % selecting_industries is a function defined in a separate file in the folder called function capital_stock_30 = select_industries(capital_stock_37, col_mapping); investment_30 = select_industries(real_investment_37, col_mapping); depreciation_30 = select_industries(depreciation_37, col_mapping); nominal_investment_30 = select_industries(nominal_investment_37, col_mapping); capital_compensation_30 = select_industries(capital_compensation_37, col_mapping); % % clear the temporary variables % clear m i % clear capital_stock_bycountry capital_stock_37 % clear investment_bycountry real_investment_37 % clear depreciation_bycountry depreciation_37 %% extend the capital stock matrix to include countries that are not in the EUKLEMS data % the countries not in the EUKLEMS data are: 3, 5, 6, 7, 8, 13, 15, 16, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 29, 31, 32, 33, 34, 35, 38, 39 % the countries in the EUKLEMS data are: 1, 2, 4, 9, 10, 11, 12, 14, 22, 23, 30, 36, 37, 40 % create a new matrix to include all countries and then either EUKLEMS data OR non-EUKLEMS data in it % define constants and generate temperary matrice to store data num_all_countries = 41; n_industries = 30; capital_stock_euklems_complete_temp = zeros(num_assetsclass, n_industries, num_all_countries, num_years); investment_euklems_complete_temp = zeros(num_assetsclass, n_industries, num_all_countries, num_years); depreciation_euklems_complete_temp = zeros(num_assetsclass, n_industries, num_all_countries, num_years); nominal_investment_euklems_complete_temp = zeros(num_assetsclass, n_industries, num_all_countries, num_years); capital_compensation_euklems_complete_temp = zeros(num_assetsclass, n_industries, num_all_countries, num_years); for i = 1:num_countries_euklems country_idx = country_excel_indices(i); capital_stock_euklems_complete_temp(:, :, country_idx, :) = capital_stock_30(:, :, i, :); investment_euklems_complete_temp(:, :, country_idx, :) = investment_30(:, :, i, :); depreciation_euklems_complete_temp(:, :, country_idx, :) = depreciation_30(:, :, i, :); nominal_investment_euklems_complete_temp(:, :, country_idx, :) = nominal_investment_30(:, :, i, :); capital_compensation_euklems_complete_temp(:, :, country_idx, :) = capital_compensation_30(:, :, i, :); end % create the matrices to store the complete data which extends the asset classes to 30 num_assetsclass_all=30; capital_stock_euklems_complete = zeros(num_assetsclass_all,n_industries,num_all_countries,num_years); investment_euklems_complete = zeros(num_assetsclass_all,n_industries,num_all_countries,num_years); depreciation_euklems_complete = zeros(num_assetsclass_all,n_industries,num_all_countries,num_years); nominal_investment_euklems_complete = zeros(num_assetsclass_all,n_industries,num_all_countries,num_years); capital_compensation_euklems_complete = zeros(num_assetsclass_all,n_industries,num_all_countries,num_years); % need to confirm that communications device is also produced at % electrical, computing equipment industry % update_capital_stock is a function defined in a separate file in the folder called function, which is to allocate the 7 asset classes in EUKLEMS data to 30 asset classes vector in the complete matrices, leaving out the residential structure since it is not used in the production. This leads to the sum of each column the capital matrix not same as the total capital stock noted in the EUKLEMS data. The discrepancy is due to the residential structure. for year = 1:13 for i = 1:14 capital_stock_euklems_complete(:, :, country_excel_indices(i), year) = update_capital_stock_include_resi_structure(capital_stock_euklems_complete_temp, country_excel_indices(i), year); investment_euklems_complete(:, :, country_excel_indices(i), year) = update_capital_stock_include_resi_structure(investment_euklems_complete_temp, country_excel_indices(i), year); depreciation_euklems_complete(:, :, country_excel_indices(i), year) = update_capital_stock_include_resi_structure(depreciation_euklems_complete_temp, country_excel_indices(i), year); nominal_investment_euklems_complete(:, :, country_excel_indices(i), year) = update_capital_stock_include_resi_structure(nominal_investment_euklems_complete_temp, country_excel_indices(i), year); capital_compensation_euklems_complete(:, :, country_excel_indices(i), year) = update_capital_stock_include_resi_structure(capital_compensation_euklems_complete_temp, country_excel_indices(i), year); end end % clear capital_stock_euklems_30 depreciation_euklems_30 investment_euklems_30 % clear capital_stock_euklems_complete_temp depreciation_euklems_complete_temp investment_euklems_complete_temp %% using the capital coefficient matrix of italy and the adjusting factor (division of total capital stock intensity of the country over italy, numbered 22 % to get the capital coefficent matrix of other countries. The capital % stock total is from WIOD SEA. % adding the invesmtent and deprecation to the loop or there is other % better ways of doing this, check later cd ..\..\ wiod_k = readmatrix("k_gfcf_all.xlsx","Range",'E2:Q1441'); othercountries=[3,5,6,7,8,13,15,16,17,18,19,20,21,24,25,26,27,28,29,31,32,33,34,35,38,39]; for m=1:13 k_temp = wiod_k(:,m); for j=othercountries % Calculate the adjustment factor factor_temp=k_temp(36*(j-1)+2:36*(j-1)+35,:).'./ k_temp(36*21+2:36*21+35,:).'; factor=[factor_temp(:,1:3) sum(factor_temp(:,4:5),2)/2 factor_temp(:,6:22) sum(factor_temp(:,23:26),2)/4 factor_temp(:,27:34)]; % Update the capital coefficient matrix for other countries capital_stock_euklems_complete(:,:,j,m) =capital_stock_euklems_complete(:,:,22,m).*factor; investment_euklems_complete(:,:,j,m) = investment_euklems_complete(:,:,22,m).*factor; depreciation_euklems_complete(:,:,j,m) = depreciation_euklems_complete(:,:,22,m).*factor; capital_compensation_euklems_complete(:,:,j,m) = capital_compensation_euklems_complete(:,:,22,m).*factor; end end % until now, types of the assets is disaggregated for each using industry. %% adding the origin of each asset types in the capital coefficient matrix % Using the information of the final destination of the investment goods % in the WIOT data to . % with additional information, I convert the stock matrix complete3 into % 30*1230*13, need some some codes below to implement this %% adding the origin of each asset types in the capital coefficient matrix % Using the information of the final destination of the investment goods % in the WIOT data to . %with additional information, I convert the stock matrix complete3 into %30*1230*13, need some some codes below to implement this capital_stock_euklems_complete4=reshape(capital_stock_euklems_complete,[30,1230,13]); cd matlab\ load('wiots') year=(1995:2009); % convert each column into a 41*30 matrix capital_stock_euklems_complete_factor=zeros(30,41,1230,13); for i=1:13 wiot_nominal=WIOTs.(strcat('WIOT_',num2str(year(i)))); wiot_nominal_final = wiot_nominal(1:1435,1436:1640); % I care more about column here rather than row wiot_nominal_investment = wiot_nominal_final(:,4:5:end); %+ wiot_nominal_final(:,5:5:end) ; % now I have 41 columns for each year for 41 countries 41*1230 % above is a 1230*41 matrix, first I need to transpose this matrix to % for each country, and each asset class, I know the share of the % sourcing coutry % before, for each country, and each using industry, I know the share % of assets classes used wiot_nominal_investment_new=reshape(wiot_nominal_investment,[35,41,41]); wiot_nominal_investment_new_new= permute(wiot_nominal_investment_new,[2,1,3]); wiot_nominal_investment_new3=zeros(41,30,41); for m=1:41 wiot_nominal_investment_new_new_temp=wiot_nominal_investment_new_new(:,:,m); wiot_nominal_investment_new_new_temp2=[wiot_nominal_investment_new_new_temp(:,1:3) sum(wiot_nominal_investment_new_new_temp(:,4:5),2) wiot_nominal_investment_new_new_temp(:,6:22) sum(wiot_nominal_investment_new_new_temp(:,23:26),2) wiot_nominal_investment_new_new_temp(:,27:34)]; wiot_nominal_investment_new3(:,:,m)= wiot_nominal_investment_new_new_temp2; end wiot_nominal_investment_new_3_sum=sum(wiot_nominal_investment_new3,1); wiot_nominal_investment_new4=repelem(wiot_nominal_investment_new_3_sum,41,1,1); wiot_nominal_investment_new_ratio=wiot_nominal_investment_new3./wiot_nominal_investment_new4; wiot_nominal_investment_new5=repelem(wiot_nominal_investment_new_ratio,1,1,30); wiot_nominal_investment_new6= permute(wiot_nominal_investment_new5,[2,1,3]); capital_stock_euklems_complete_factor(:,:,:,i)=wiot_nominal_investment_new6; end capital_stock_euklems_complete_factor_new=reshape(capital_stock_euklems_complete_factor,[1230,1230,13]); capital_stock_euklems_complete5 = repelem(capital_stock_euklems_complete4,41,1,1); capital_stock_euklems_complete5_temp=reshape(capital_stock_euklems_complete5,[41,30,1230,13]); capital_stock_euklems_complete5_temp2 = permute(capital_stock_euklems_complete5_temp,[2,1,3,4]); capital_stock_euklems_complete6=reshape(capital_stock_euklems_complete5_temp2,[1230,1230,13]); euklems_capital_stock=capital_stock_euklems_complete6.*capital_stock_euklems_complete_factor_new; % here is the gross capital formation, which is essential investment % Real gross fixed capital formation, 1995 prices % following the same factor to extend the matrix for investment and % depreciation % investment investment_euklems_complete4=reshape(investment_euklems_complete,[30,1230,13]); investment_euklems_complete_factor_new=reshape(capital_stock_euklems_complete_factor,[1230,1230,13]); investment_euklems_complete5 = repelem(investment_euklems_complete4,41,1,1); investment_euklems_complete5_temp=reshape(investment_euklems_complete5,[41,30,1230,13]); investment_euklems_complete5_temp2 = permute(investment_euklems_complete5_temp,[2,1,3,4]); investment_euklems_complete6=reshape(investment_euklems_complete5_temp2,[1230,1230,13]); euklems_investment=investment_euklems_complete6.*investment_euklems_complete_factor_new; % depreciation depreciation_euklems_complete4=reshape(depreciation_euklems_complete,[30,1230,13]); depreciation_euklems_complete_factor_new=reshape(capital_stock_euklems_complete_factor,[1230,1230,13]); depreciation_euklems_complete5 = repelem(depreciation_euklems_complete4,41,1,1); depreciation_euklems_complete5_temp=reshape(depreciation_euklems_complete5,[41,30,1230,13]); depreciation_euklems_complete5_temp2 = permute(depreciation_euklems_complete5_temp,[2,1,3,4]); depreciation_euklems_complete6=reshape(depreciation_euklems_complete5_temp2,[1230,1230,13]); euklems_depreciation=depreciation_euklems_complete6.*depreciation_euklems_complete_factor_new; euklems_capital_stock(isinf(euklems_capital_stock))=0; euklems_capital_stock(isnan(euklems_capital_stock))=0; euklems_investment(isinf(euklems_investment))=0; euklems_investment(isnan(euklems_investment))=0; euklems_depreciation(isinf(euklems_depreciation))=0; euklems_depreciation(isnan(euklems_depreciation))=0; % clear i j m % clear capital_stock_euklems_complete capital_stock_euklems_complete4 capital_stock_euklems_complete5 capital_stock_euklems_complete5_temp capital_stock_euklems_complete5_temp2 capital_stock_euklems_complete6 % clear capital_stock_euklems_complete_factor capital_stock_euklems_complete_factor_new % clear col_mapping % clear depreciation_euklems_complete depreciation_euklems_complete4 depreciation_euklems_complete5 % clear depreciation_euklems_complete5_temp depreciation_euklems_complete5_temp2 depreciation_euklems_complete6 % clear depreciation_euklems_complete_factor_new factor factor_temp % clear investment_euklems_complete investment_euklems_complete4 investment_euklems_complete5 % clear investment_euklems_complete5_temp investment_euklems_complete5_temp2 investment_euklems_complete6 % clear investment_euklems_complete_factor_new k_other k_temp temp % clear wiod_k wiot_new wiot_new2 wiot_new3 wiot_nominal wiot_nominal_final % clear wiot_nominal_investment_new wiot_nominal_investment_new3 wiot_nominal_investment_new4 wiot_nominal_investment_new5 % clear wiot_nominal_investment_new6 wiot_nominal_investment_new_3_sum wiot_nominal_investment_new_new wiot_nominal_investment_new_new_temp % clear wiot_nominal_investment_new_new_temp2 wiot_nominal_investment_new_ratio wiot_tm wiot_tm_temp1 % clear wiotable cd '..\..\code'
最新发布
06-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值