Hongjun and balanced tables
Time limit2sMemory limit512 MB
Count the ways to fill a 3-by-C grid with nonnegative integers so that every triple of cells satisfying a + c = 2b sums to S.
- Level
Medium6 of 10
- Topics
- Combinatorics, Math, Dynamic programming, Implementation
- Solved
- No attempts yet
Problem
You are given an empty table with 3 rows and columns, together with an integer . Hongjun writes one nonnegative integer in every cell of the table.
Take three cells, one from each of the three rows. If the center of one of them is the midpoint of the segment joining the centers of the other two, the three cells are called balanced. If the three cells lie in column of row 1, column of row 2, and column of row 3, this condition is the same as . The picture below shows one balanced set of three cells when .

Hongjun wants to fill the table so that for every balanced set of three cells, the numbers written in those three cells add up to . For two tables and , let and be the numbers written in row and column . The two tables are different if for at least one pair , . Given and , write a program that counts the tables Hongjun can write.
Input
The first line contains the number of test cases ().
Each of the next lines contains and (, ), separated by a space.
Output
For each test case, print the number of possible tables on its own line.