AMR10E - Stocks Prediction

no tags 

The department store where my family goes shopping is trying to predict how much of each item they stock they will sell each month.  Keeping a large amount of inventory is costly, and running out of items is also not a good idea.  Since the manager asked for my help as a sales consultant, I decided to formulate a model for predicting each month's sales S of an item based on its sales during the previous R months.  After a lot of trial and error, I came up with such a model, where
S(n) = a1*S(n-1) + a2*S(n-2) + ... + aR*S(n-R)  
where S(n) is the predicted sales for the nth month for n > R, and S(1) to S(R) are seed values.
 
The store manager was pleased with my model's ability to help him in controlling his inventory.  
He asked me to list out every Kth month's sales, and give him the sum of the first N values from this list.  For example he wanted every Christmas month's sales summed up for the next 10 years (N=10 and K=12, month 1 being January), or every end-of-quarter month's sales for the next 2 years (N=2, K=3).
 
Can you please help me write a program that does all the above?
 
INPUT
The first line of the input T, the number of test cases. Each test case consists of three lines.
The first line of each test case contains N, R, K.
The second line of each test case contains R integers denoting S(1), S(2), ..., S(R).
The third line of each test case contains R integers denoting the coefficients a1, a2, ..., aR of the predictive model.
 
OUTPUT
For each test case, output the sum requested by the manager as given in the problem statement, modulo 1,000,000,007.
 
CONSTRAINTS
T <= 40
1 <= N <= 1000000000
1 <= R <= 8
1 <= K <= 8
0 <= All other input values < 1000000007
 
SAMPLE INPUT
2
4 1 1
1  
2  
3 2 3
1 1  
1 1  
 
SAMPLE OUTPUT
15
44
 
EXPLANATION
In the first test case, it is given that S(1) = 1 and the relation is S(n)=2*S(n-1). The list asked by the store manager consists of all the terms of S since K is 1. Hence, the answer is just the sum of the first 4 terms of S.
In the second test case, the sequence S is the fibonacci sequence which is: 1, 1, 2, 3, 5, 8, 13, 21, 34. The list consists of 2, 8, 34 which sum up to 44.


hide comments
Aswin Akhilesh: 2011-09-08 13:50:44

Not able to figure out how the sequence F given in the o/p explanation is being worked out. Any help?

Last edit: 2011-09-08 13:51:45
Anton Lunyov: 2010-12-16 07:20:41

Why R,K are so small? This problem can be easily solved when R<=50 and K<=1000000000.


Added by:Varun Jalan
Date:2010-12-13
Time limit:8s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:ICPC Asia regionals, Amritapuri 2010