CMPLS - Complete the Sequence!

no tags 

You probably know those quizzes in Sunday magazines: given the sequence 1, 2, 3, 4, 5, what is the next number? Sometimes it is very easy to answer, sometimes it could be pretty hard. Because these "sequence problems" are very popular, ACM wants to implement them into the "Free Time" section of their new WAP portal.

ACM programmers have noticed that some of the quizzes can be solved by describing the sequence by polynomials. For example, the sequence 1, 2, 3, 4, 5 can be easily understood as a trivial polynomial. The next number is 6. But even more complex sequences, like 1, 2, 4, 7, 11, can be described by a polynomial. In this case, 1/2.n2-1/2.n+1 can be used. Note that even if the members of the sequence are integers, polynomial coefficients may be any real numbers.

Polynomial is an expression in the following form:

P(n) = aD.nD+aD-1.nD-1+...+a1.n+a0

If aD <> 0, the number D is called a degree of the polynomial. Note that constant function P(n) = C can be considered as polynomial of degree 0, and the zero function P(n) = 0 is usually defined to have degree -1.

Input

There is a single positive integer T on the first line of input (equal to about 5000). It stands for the number of test cases to follow. Each test case consists of two lines. First line of each test case contains two integer numbers S and C separated by a single space, 1 <= S < 100, 1 <= C < 100, (S+C) <= 100. The first number, S, stands for the length of the given sequence, the second number, C is the amount of numbers you are to find to complete the sequence.

The second line of each test case contains S integer numbers X1, X2, ... XS separated by a space. These numbers form the given sequence. The sequence can always be described by a polynomial P(n) such that for every i, Xi = P(i). Among these polynomials, we can find the polynomial Pmin with the lowest possible degree. This polynomial should be used for completing the sequence.

Output

For every test case, your program must print a single line containing C integer numbers, separated by a space. These numbers are the values completing the sequence according to the polynomial of the lowest possible degree. In other words, you are to print values Pmin(S+1), Pmin(S+2), .... Pmin(S+C).

It is guaranteed that the results Pmin(S+i) will be non-negative and will fit into the standard integer type.

Example

Sample Input:

4
6 3
1 2 3 4 5 6
8 2
1 2 4 7 11 16 22 29
10 2
1 1 1 1 1 1 1 1 1 2
1 10
3

Sample Output:

7 8 9
37 46
11 56
3 3 3 3 3 3 3 3 3 3
Warning: large Input/Output data, be careful with certain languages

hide comments
drunkenstein: 2022-02-20 21:14:18

AC in one go!!
The idea is to use the method of Difference Table for Generating Terms for Polynomials

raman_95: 2021-10-25 10:36:50

Few patterns are understandable , but others cant understand . Can anyone provide any links to understand the problem with more clarity?

sabkx: 2021-09-21 16:06:08

Lagrange interpolation formula should work here right?

guru_shreyansh: 2021-08-04 15:38:55

Solved it in Java using the Difference Method. Many people shared the wiki link for the Diff Method but that was not useful to me at all. You can refer to: https://brilliant.org/wiki/method-of-differences/
for a clearer approach.

king_rohan12: 2020-07-21 16:25:12

i got the problem but can anyone explain me about
3rd test case i.e 10 2
1 1 1 1 1 1 1 1 1 2

Last edit: 2020-07-21 16:30:02
jsthiago: 2020-05-03 22:26:15

i think the 1 1 1 1 1 1 1 1 1 2 case is when you do the diff table you got
0 0 0 0 0 0 0 0 1,
0 0 0 0 0 0 0 1,
0 0 0 0 0 0 0 1
0 0 0 0 0 0 1
0 0 0 0 0 1
0 0 0 0 1
0 0 0 1
0 0 1
0 1
1
needs 8 recursive diff to get there. Then you sum it recursive and the total of difference is 9(because it starts in 0th) and
9 + 2 = 11.
if you do it for the second number you got:
0 0 0 0 0 0 0 0 1 9
0 0 0 0 0 0 0 1 8
0 0 0 0 0 0 1 7
0 0 0 0 0 1 6
0 0 0 0 1 5
0 0 0 1 4
0 0 1 3
0 1 2
1 1
1
total is 46 and so on. i don't know if it is right but my algorithm gave me this answer

rahul_t_7: 2020-03-21 13:14:40

check this test case 1 1 1 1 1 1 1

Last edit: 2020-03-21 13:15:30
ngtrhieu: 2020-01-29 12:18:46

Hmm I think Lanrange poly only give a polynomial that guarantee to fit at the input points, but does not guarantee other points..

sumit_989692: 2019-12-24 08:07:59

Can someone explain me this testcase?1 1 1 1 1 1 1 1 1 2

alok_0366: 2019-06-10 14:56:15

what does this ques wants to say?? i understood the method of difference.


Added by:adrian
Date:2004-05-08
Time limit:5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL6 VB.NET
Resource:ACM Central European Programming Contest, Prague 2000