TopcoderArchiveVisit Topcoder
SRM5 Jun 2012
SRM 559

HyperKnight

Problem statement, definition, constraints, and public examples.

Problem Statement

Fernando loves to play chess. One day he decided to play chess on an unusually large rectangular board. To compensate for the board's size he also decided to change the distance a knight can move in a single jump.

To describe the moves easily, we will now introduce a coordinate system. Each cell of the chessboard can be described using two integers (r,c): its row number and its column number. Now, if we have a piece at (r,c), the move (x,y) takes the piece to the cell (r+x,c+y).

The new chess piece will be called an (a,b)-hyperknight. The hyperknight always has 8 possible moves: (+a,+b), (+a,-b), (-a,+b), (-a,-b), (+b,+a), (+b,-a), (-b,+a), and (-b,-a). Note that the original chess knight is a (2,1)-hyperknight.

Of course, as the chessboard is finite, it is not always possible to make each of the 8 moves. Some of them may cause our hyperknight to leave the chessboard. A move is called valid if the destination cell is on the chessboard. Fernando would like to know the number of cells on his board such that his hyperknight will have exactly k valid moves from that cell.

You are given the ints a, b, numRows, numColumns and k. The values numRows and numColumns define the number of rows and number of columns on the chessboard, respectively. The other three values were already explained above. Compute and return the number of cells on the chessboard that have exactly k valid (a,b)-hyperknight moves.

Definition

Class:
HyperKnight
Method:
countCells
Parameters:
int, int, int, int, int
Returns:
long
Method signature:
long countCells(int a, int b, int numRows, int numColumns, int k)
(be sure your method is public)

Notes

  • If you wish, you may assume that the rows are numbered 0 through numRows-1 and columns 0 through numColumns-1. However, note that the actual row/column numbers do not matter, as long as they are consecutive.

Constraints

  • a will be between 1 and 1,000,000,000 (10^9), inclusive.
  • b will be between 1 and 1,000,000,000 (10^9), inclusive.
  • a will not be equal to b.
  • numRows will be between 1 and 1,000,000,000 (10^9), inclusive.
  • numColumns will be between 1 and 1,000,000,000 (10^9), inclusive.
  • 2*max(a,b) will be strictly less than min(numRows,numColumns).
  • k will be between 0 and 8, inclusive.

Examples

  1. 2
    1
    8
    8
    4
    Returns: 20
    This is a standard chessboard. We have a traditional chess knight and we are looking for cells such that the knight has exactly 4 valid moves.
  2. 3
    2
    8
    8
    2
    Returns: 16
  3. 1
    3
    7
    11
    0
    Returns: 0
  4. 3
    2
    10
    20
    8
    Returns: 56
  5. 1
    4
    100
    10
    6
    Returns: 564
  6. 2
    3
    1000000000
    1000000000
    8
    Returns: 999999988000000036
Back to all problems