Connection Status:
Competition Arena > HyperKnight
SRM 559 · 2012-06-05 · by tehqin · Simple Math
Class Name: HyperKnight
Return Type: long
Method Name: countCells
Arg Types: (int, int, int, int, int)
Problem Statement

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.

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
0)
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.

1)
3
2
8
8
2
Returns: 16
2)
1
3
7
11
0
Returns: 0
3)
3
2
10
20
8
Returns: 56
4)
1
4
100
10
6
Returns: 564

Submissions are judged against all 175 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class HyperKnight with a public method long long countCells(int a, int b, int numRows, int numColumns, int k) · 175 test cases · 2 s / 256 MB per case

Submitting as anonymous