Ultimate magic rectangles (Hard)
Time limit1sMemory limit1024 MB
Count the ways to fill a 3-row, c-column grid with nonnegative integers so every triplet on a line across all three rows sums to s, modulo 10^9 + 9.
- Level
Hard8 of 10
- Topics
- Math, Combinatorics, Dynamic programming
- Solved
- No attempts yet
Problem
Bob is busy today, so Alice has found a single-player game.
In this game, Alice is given an integer and an empty table with rows and columns. Alice has to fill some nonnegative integers into the cells of the table.
Three cells are called a triplet if they lie in different rows and their centers lie on a straight line. The goal of the game is to fill the table so that each triplet has the same sum.
You are given the number of columns and the desired sum of each triplet . Count the ways to fill the table in the desired way. The count may be large, so print it modulo .

Above: one of the many triplets on a board with columns.
Input
The first line of the input contains an integer , the number of test cases. Each test case is preceded by a blank line.
Each test case consists of one line with two space-separated integers and .
Output
For each test case, print the number of solutions modulo on a separate line.
Constraints
Hint
In the first test case there are five triplets: each column and both main diagonals of the square. The sum of each triplet must be 1, so each triplet must contain two 0s and a 1. These are the five solutions:
111 000 000 101 010
000 111 000 000 000
000 000 111 010 101
In the second test case, one of the 34 valid solutions is a rectangle full of 1s.