D - Polyomino Editorial by Kiri8128

A solution with bit operations

Similar to this editorial, binary operation is simpler to implement, and has smaller calculation complexity. A more difficult point in this problem is the rotation. One solution is to use a 2-dimentional list, but we have faster solution. The idea is to use binary and 32-ary system to the input.

(Ideas)

  • To use binary numbers to represent a board
  • To put a sentinel to the rightmost column of each row. Then the board can be represented as a \(20\) bit integer
  • Parallel movements (both holizontal and vertical) can be represented by a bit shift operation
  • Bit shift operation up to 18 bits (after removing leading zeroes) is enough
  • To obtain a number after removing trailing zeroes, you can use a // (a & -a)
  • “No overlap” can be confirmed by a & b == 0 etc.
  • Completeness (i.e., whether it covers all of the board) can be confirmed if the number of the board is \(507375\) (\(=1+2+2^{2}+2^{3}+2^{5}+2^{6}+2^{7}+2^{8}+2^{10}+2^{11}+2^{12}+2^{13}+2^{15}+2^{16}+2^{17}+2^{18}\)

AC Code (PyPy3)

posted:
last update: