luogu#P6950. [ICPC2018 WF] Uncrossed Knight’s Tour

[ICPC2018 WF] Uncrossed Knight’s Tour

题目描述

A well-known puzzle is to tour all the squares of an 8×88 \times 8 chessboard using a knight, which is a piece that can move only by jumping one square in one direction and two squares in an orthogonal direction. The knight must visit every square of the chessboard, without repeats, and then return to its starting square. There are many ways to do this, and the chessboard size is manageable, so it is a reasonable puzzle for a human to solve.

However, you have access to a computer, and some coding skills! So, we will give you a harder version of this problem on a rectangular m×nm \times n chessboard with an additional constraint: the knight may never cross its own path. If you imagine its path consisting of straight line segments connecting the centers of squares it jumps between, these segments must form a simple polygon; that is, no two segments intersect or touch, except that consecutive segments touch at their common end point. This constraint makes it impossible to visit every square, so instead you must maximize the number of squares the knight visits. We keep the constraint that the knight must return to its starting square. Figure J.1 shows an optimal solution for the first sample input, a 6×66 \times 6 board.

Figure J.1 : An optimal solution for a 6×66 \times 6 board.

输入格式

The input consists of a single line containing two integers m(1m8)m (1 \le m \le 8) and n(1n1015),n (1 \le n \le 10^{15}), giving the dimensions of the rectangular chessboard.

输出格式

Display the largest number of squares that a knight can visit in a tour on an m×nm \times n chessboard that does not cross its path. If no such tour exists, display 00 .

题目大意

题目描述

马在 mm ×\times nn 大小的矩形棋盘上跳跃(走日字)。求从棋盘上一点开始,在保证【性质】的情况下,它最多经过几个格子(包括起点,且终点不算),可以回到初始点?

【性质】:想象马的路径由直线段组成,这些直线段连接着它所跳跃的正方形的中心(如图),这些直线段必须形成一个简单的多边形。也就是说,没有两个线段相交或接触,除非连续线段在其公共端点处接触。

输入格式

一行,两个正整数 mm (1m8)(1 \le m \le 8)nn (1n1015)(1 \le n \le 10^{15})

输出格式

一行,马最多经过的格子数。 若没有这样的路径,输出 00

6 6

12

8 3

6

7 20

80

2 6

0

提示

Time limit: 2 s, Memory limit: 1024 MB.