Zig-zag
Time limit12sMemory limit1024 MB
Count the zig-zag compositions of n into positive parts, where sizes alternate up-down or down-up, for many n up to 300000, modulo 998244353.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
Zack's Zergonomics Zegree has taught him that the optimal way to display items in a store is to stack them into a zig-zag pattern.
Zack needs to display boxes lined up on the storefront, each containing an action figure. The boxes can be stacked on top of one another. They are identical and indistinguishable. He must decide the number of stacks, then stack the boxes so that each stack is non-empty and the stack sizes form a zig-zag sequence.
Formally, suppose there are () stacks numbered to from left to right, and stack holds boxes. The following must hold:
- for each from to .
- .
- At least one of the following is true:
For example, when , there are 12 ways, as shown in Figure M.1.

Figure M.1: All 12 possible ways for .
Two ways are the same if and only if they have the same number of stacks and the stacks at each position hold the same number of boxes.
Find the number of different ways Zack can stack boxes, modulo .
Input
The first line contains one integer (), the number of test cases. Each of the next lines contains one integer ().
Output
For each test case, output the number of different ways to stack boxes, modulo , on its own line.