Polynomial

Given an integer polynomial of degree at most 25, find the reduced fractional coefficients of its sum from 0 to n and output the sum of the absolute numerators.

Hard8MathCombinatoricsNumber theoryNo attempts yetTime limit1sMemory limit256 MB

Problem

A polynomial f(k)f(k) of degree tt whose coefficients are all integers is given as

f(k)=c0+c1k+c2k2++ctktf(k) = c_0 + c_1k + c_2k^2 + \cdots + c_tk^t

For a nonnegative integer nn, let S(n)S(n) be the sum of f(0),f(1),,f(n)f(0), f(1), \dots, f(n).

S(n)=k=0nf(k)=f(0)+f(1)++f(n)S(n) = \sum_{k=0}^{n}{f(k)} = f(0) + f(1) + \cdots + f(n)

S(n)S(n) is a polynomial as well. Its degree is t+1t+1 and its coefficients are rational, so it can be written as

S(n)=a0b0+a1b1n+a2b2n2++at+1bt+1nt+1S(n) = \frac{a_0}{b_0} + \frac{a_1}{b_1}n + \frac{a_2}{b_2}n^2 + \cdots + \frac{a_{t+1}}{b_{t+1}}n^{t+1}

where for each i=0,1,,t+1i = 0, 1, \dots, t+1 the integers aia_i and bib_i are relatively prime, that is, they have no common divisor greater than 1. A coefficient that equals zero is written with ai=0a_i = 0 and bi=1b_i = 1.

Given a polynomial f(k)f(k) of degree tt with integer coefficients c0,,ctc_0, \dots, c_t, write a program that computes S(n)S(n) and prints the value

i=0t+1ai\sum_{i=0}^{t+1}{\left| a_i \right|}

You may use the following identity for polynomials. For any positive integer dd and any real xx,

(x+1)dxd=1+(d1)x+(d2)x2++(dd1)xd1(x+1)^d - x^d = 1 + \binom{d}{1}x + \binom{d}{2}x^2 + \dots + \binom{d}{d-1}x^{d-1}

where (di)=d!i!(di)!\binom{d}{i} = \frac{d!}{i!(d-i)!} for any integer ii with 0id0 \le i \le d.

Input

Your program reads from standard input. The input consists of TT test cases, and the number of test cases TT is given on the first line.

Each test case is a single line. The line starts with a nonnegative integer tt (0t250 \le t \le 25), followed by t+1t+1 integers c0,,ctc_0, \dots, c_t with 10c0,,ct10-10 \le c_0, \dots, c_t \le 10 and ct0c_t \ne 0. These values fully describe the polynomial f(k)=c0+c1k+c2k2++ctktf(k) = c_0 + c_1k + c_2k^2 + \cdots + c_tk^t of degree tt with coefficients c0,,ctc_0, \dots, c_t.

Output

Your program writes to standard output. Print exactly one line for each test case. The line contains one integer, the value i=0t+1ai\sum_{i=0}^{t+1}{\left| a_i \right|}.