#P7524. Stable Diagonal Bishops on a Toroidal Chessboard

    ID: 20719 Type: Default 1000ms 256MiB

Stable Diagonal Bishops on a Toroidal Chessboard

Stable Diagonal Bishops on a Toroidal Chessboard

You are given a toroidal chessboard of size n×n. The board is cyclic in both dimensions: the cell on row i and column n is adjacent (to the right) to the cell on row i and column 1, and the cell on row n and column j is below the cell on row 1 and column j. You are to place exactly m pieces called "Great Sinistral Bishop" (大罪司教) on the board. A bishop can move in any of the four diagonal directions by any number of cells (wrapping around the board as necessary). A cell is said to be controlled by a bishop if the bishop can reach that cell in one move. The board layout is stable if no bishop controls a cell occupied by another bishop.

Your task is to count the number of essentially different stable layouts in which exactly m bishops are placed. Two layouts are considered essentially different if there exists at least one cell which has a bishop in one layout and is empty in the other. Output the answer modulo 998244353.

Explanation:

On a toroidal board, the diagonal moves of a bishop imply that if a bishop occupies cell (i,j), then it controls all cells whose coordinates satisfy either (i - j) mod n or (i + j) mod n equal to that of the bishop. Hence, in a stable layout, no two bishops share the same value of i - j (mod n) or i + j (mod n).

Hint: When n is odd, every pair (d,s) (with d=i-j (mod n) and s=i+j (mod n)) corresponds uniquely to one board cell. When n is even, a solution to the system exists if and only if d and s have the same parity, and in that case there are exactly two solutions. Use this observation along with combinatorial counting to compute the answer.

inputFormat

The input consists of a single line containing two integers n and m (1 ≤ m ≤ n), where n is the size of the board, and m is the number of bishops to place.

outputFormat

Output a single integer – the number of stable layouts modulo 998244353.

sample

3 2
18