PolandBall and Many Other Balls
Contest: 8VC Venture Cup 2017 - Elimination Round
Difficulty rating: 3200
Tags: combinatorics, divide and conquer, dp, fft, math, number theory

PolandBall is standing in a row with Many Other Balls. More precisely, there are exactly n Balls. Balls are proud of their home land — and they want to prove that it's strong.

The Balls decided to start with selecting exactly m groups of Balls, each consisting either of single Ball or two neighboring Balls. Each Ball can join no more than one group.

The Balls really want to impress their Enemies. They kindly asked you to calculate number of such divisions for all m where 1 ≤ m ≤ k. Output all these values modulo 998244353, the Enemies will be impressed anyway.

Input format:
There are exactly two numbers n and k (1 ≤ n ≤ 109, 1 ≤ k < 215), denoting the number of Balls and the maximim number of groups, respectively.

Output format:
You should output a sequence of k values. The i-th of them should represent the sought number of divisions into exactly i groups, according to PolandBall's rules.

Constraints:
Time limit: 6.0 s
Memory limit: 256.0 MB

Note:
In the first sample case we can divide Balls into groups as follows:

{1}, {2}, {3}, {12}, {23}.

{12}{3}, {1}{23}, {1}{2}, {1}{3}, {2}{3}.

{1}{2}{3}.

Therefore, output is: 5 5 1.

Example input:
3 3

Example output:
5 5 1

Example input:
1 1

Example output:
1

Editorial:
The modulo instantly suggests an FFT-like approach.
First, let's solve this problem using simple dynamic programming.
DP[N][K] = DP[N-1][K] + DP[N-1][K-1] + DP[N-2][K-1], because we can either do nothing or add one interval of length 1 or 2. As for start, this works fine.
We can calculate DP[1], DP[2], DP[3] and DP[4] using this simple formula. Calculating DP[x] means calculating DP[x][k] for all values k in range <1, K>, where K is the number from statement.
We will perform binary exponentation of DPs. In order to do so, we should be able to count DP[x + 1] and DP[2 * x]. If we could do this operations, then we can obtain the result in O(logN).
Important property: DP[a + b] can be counted using DP[a] * DP[b] and DP[a - 1] * DP[b - 1], when we treat DP[x] as polynomial with coefficients DP[x][0], DP[x][1], .... It is based by the fact that we can divide the sequence of length (a + b) in two parts of length a and b.
If there is no segment of length 2 starting from position a, then we can choose some segments from the first part, and the remaining segments from the second part. For fixed number of intervals k, this is a sum of DP[a][i] * DP[b][k - i]. So, this is convolution of polynomials DP[A] and DP[B].
However, there may also be such a segment. In this case, everything stays the same, but we have k - 1 intervals instead of k and convolution of DP[a - 1] and DP[b - 1]. So, with some calculations we can count DP[a + b] using those DPs.
Assume we have DP[n], DP[n - 1] and DP[n - 2]. Now we are trying to count DP[2 * n], DP[2 * n - 1], DP[2 * n - 2].
DP[2 * n] = DP[n + n], which can be counted using multiplication of DP[n] and DP[n-1].
DP[2*n-1] = DP[n + (n-1)], which can be counted using multiplication of DP[n], DP[n-1] and DP[n-2].
DP[2*n-2] = DP[(n-1) + (n-1)], which can be counted using multiplication of DP[n-1] and DP[n-2].
Therefore, we can obtain another, two times bigger, triplet of DPs. Of course, having DP[n], DP[n-1] and DP[n-2] in memory we can also easily count DP[n + 1] in O(K).
We can also calculate DP[2*n], DP[2*n-1] and DP[2*n-2] in O(K log K). This is enough to perform the binary exponentation.
Overall solution works in O(K log N log K).
Statement says "Balls are proud of their home land - and they want to prove that it's strong." It originated, of course, from Independence Marches. On November 11th, Poland celebrates an anniversary of getting independence in year 1918, after the World War I and 123 years of enslavement. Those Marches are spectacular.

Unfortunately, this is the end of your meeting with PolandBall. I hope that you liked the problems. Good luck during next contests. I hope to see you soon! Any comments, especially with feedback are really appreciated.
Have a nice day!
PolandBall