The Paths of Yin Yang (Large)
Time limit120sMemory limit512 MB
Count the black-and-white colorings of an N by M grid in which each color forms one edge-adjacent path.
- Level
Hard9 of 10
- Topics
- Combinatorics, Graph
- Solved
- No attempts yet
Problem
You are given a grid with rows and columns. Each cell is painted black (yin) or white (yang). Two cells are neighbors when they share an edge of unit length. The grid is valid when the black cells form one path and the white cells form one path. A set of cells is a path when all three of the following hold.
- is connected. From any cell of you reach every other cell of by moving between neighbors inside .
- Exactly two cells of have exactly one neighbor inside . These two cells are the ends of the path.
- Every other cell of has exactly two neighbors inside .
In the picture below, the first grid is valid. The second one is not: its black cells form a path, but its white cells do not.

Given and , count the valid grids. Two valid grids that differ in at least one cell are counted separately, even when a rotation or a reflection turns one into the other.
Input
The first line contains one integer , the number of test cases. Each of the next lines contains two integers and separated by a space.
Output
For each test case, print one line in the form Case #x: A, where is the test case number starting from 1 and is the number of valid grids of the given size.