Count how many shortest king-move paths join two given squares on an N by N board, modulo 5318008.
Medium6CombinatoricsMathNo attempts yetTime limit4sMemory limit256 MBChess is a game where two sides move pieces and try to capture the opposing king. The pieces differ in how far they travel. Early in a game the king is weak. He moves more slowly than most other pieces and usually hides behind his own pawns. Once both queens have left the board it is time for the king to act. Almost nothing threatens him any more, so he can walk around the board safely, and at this stage his mobility makes him one of the more dangerous pieces. This problem measures that mobility.
The board has N×N squares and the king is the only piece on it. A square is written as a pair (X,Y) with 1≤X,Y≤N. In one move the king steps to a square that touches his current square orthogonally or diagonally, so from (X,Y) he moves to one of the at most eight squares (X+a,Y+b) with a,b∈{−1,0,1} and (a,b)=(0,0). He may never leave the board.
The king starts on one square and wants to reach another square in as few moves as possible. Count the walks that take him to the destination in that minimum number of moves. Two walks are different if the king stands on a different square after some move.
The first line has an integer T, the number of test cases. Each test case is given on two lines.
For each test case, print one line with a single integer: the number of walks that take the king to the destination in the minimum number of moves, reduced modulo 5318008.