NR1 - Kapti and Balu


Kapti and Balu are friends. Kapti is very good in math and is always saying that no one can challenge him in math. One day Balu decided to check Kapti that he is really good in math or he is just making fool.

Balu gave Kapti a polynomial of degree “n” and ask Kapti to find constant term “L” if the polynomial was first expressed in Factorial Notation and then subjected to Forward Difference operator for “k” imes.

If

      f(x) = a1xn + a2xn-1 + ... + anx + l

then its factorial notation will be:

      fFN(x) = A1[x]n + A2[x]n-1 + ... + An[x] + L

Where

      [x]n = x(x-1)(x-2) ... (x-n+1)

And forward difference operator Δ is just simple differentiation of fFN(x).

For example

      f(x) = 2x3 - 3x2 + 3x - 10
      fFN(x) = 2[x]3 + 3[x]2 + 2[x] - 10
      ΔfFN(x) = 6[x]2 + 6[x] + 2 here constant term L = 2 and k = 1;

Help kapti in proving himself that he is good in math.

Input

Input start with integer T (< 30) denoting the number of test cases.

Each test case will contains two lines,

First line contains n (<= 25) and k (<= n)

Second line contains n+1 integers a1 a2 a3 ... an l, -50 <= ai <= 50

Output

For each test case print one line containing L.

Example

Input:
1
3 2
2 -3 3 -10

Output:
6

hide comments
RIVU DAS: 2014-06-04 09:15:27

Nice one!! Learned a lot!

Bhavik: 2014-06-04 09:15:27

@Nishant Raj: I must thank you for making this problem:) I learned many new mathematical concepts to solve this problem

Jumpy: 2014-06-04 09:15:27

finally after lots of WA got TLE the question would have been more interesting if there was modulo concept...

રચિત (Rachit): 2014-06-04 09:15:27

A suggestion: To prevent overflows in C/CPP, can you please modify your problem statement to print the answer modulus some prime.

NISHANT RAJ: 2014-06-04 09:15:27

@anurag : in c++ during calculation overflow may occure.:

Last edit: 2014-04-21 21:16:53
anurag garg: 2014-06-04 09:15:27

@nishant raj
can you have a look at my submission id:11273889
is WA due to overflow or some other mistake
is it possible to solve this question in c++
thanks

---edit--->
my c++ logic was absolutely correct..
I implement the same logic in java and got AC...

Last edit: 2014-04-19 11:34:52
Francky: 2014-06-04 09:15:27

You should not set some time limit near zero, there's a little time for start interpreter. Here, you could increase time limit and constraints, on T or on maxN.
edit : I've tested my code with T=100 poly n~=1000, and spoj-time would have been 0.5s. You could have set such a file with time limit near 5s and/or reduce T at 30. This problem is interesting, and cases seems a bit small...
@francky: yes test cases are small.

Last edit: 2014-03-10 21:04:23
Anant Kumar: 2014-06-04 09:15:27

Nice question! Knowledge of factorial notation of polynomial is prerequisite.

Bhavik: 2014-06-04 09:15:27

@Nishant: how did coefficient of x changes to 2 from 3 in fFN(x)??
fFN(x)=2[x]3 + 3[x]2 + 2[x] - 10

i mean fFN(x) will be written as 2*(x)*(x-1)*(x-2)-3*(x)*(x-1)+3*(x)-10 according to your rule and that does not give coefficient of x as 2 on solving?? kindly clarify my doubt

@Bhavik: you have written fFN(x)=2*(x)*(x-1)*(x-2)-3*(x)*(x-1)+3*(x)-10.
but it will be fFN(x)=2*(x)*(x-1)*(x-2) + 3*(x)*(x-1)+ 2*(x)-10. now solve this this is equal to f(x).

EDIT:ok i got it.First i need to learn about factorial notation then i will try this..thank you for reply

Last edit: 2014-03-10 07:08:22

Added by:NISHANT RAJ
Date:2014-03-08
Time limit:0.100s-0.200s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:own