Hexagonal Routes

Time limit1sMemory limit128 MB

Summary
On a hexagonal grid numbered in a spiral, for each pair of cells find the shortest path length and the number of distinct shortest paths.
Level

Medium7 of 10

Topics
Geometry, Math, Combinatorics, Implementation
Solved
No attempts yet

Problem

Finding the shortest route is a truck driver's everyday task: driving the shortest route saves money. Knowing alternative routes of the same length also matters, in case a route becomes unavailable — for example, due to road works.

To describe a region, we model its map. Triangular and rectangular grids turned out to be insufficient, so we use hexagons instead. On a regular hexagonal grid (like a honeycomb), we number the cells starting from 1 at any chosen cell and spiralling outward. Each cell then has a unique number, and every positive integer names exactly one cell.

Two cells are neighbours if and only if they share a side. In an infinite grid, every cell has exactly six neighbours; for example, cell 2 neighbours cells 1, 3, 7, 8, 9 and 10. A hexagonal route is a non-empty sequence of cells in which every cell except the last is a neighbour of the next one. A route from cell XX to cell YY starts at XX and ends at YY. Its length is the number of cells minus one — the number of steps taken along the route.

For two cells, determine the length of the shortest route between them and the number of distinct routes of that length. Two routes are distinct if they differ in at least one cell; they need not be disjoint.

Input

The input contains several queries, one per line. Each line holds two integers XX and YY separated by a space, with 1≤X,Y≤1061 \le X, Y \le 10^6 and X≠YX \ne Y, naming two different cells. A final line containing two zeros terminates the input and is not a query.

Output

For each query, print one line:

There are N routes of the shortest length L.

where LL is the shortest route length between cells XX and YY, and NN is the number of distinct routes of that length. If there is exactly one such route, write is and route instead of are and routes.

Examples4

  1. Example 1

    Input
    1 2
    1 7
    1 8
    1 19
    7 12
    1000 9000
    0 0
    
    Expected output
    There is 1 route of the shortest length 1.
    There is 1 route of the shortest length 1.
    There are 2 routes of the shortest length 2.
    There is 1 route of the shortest length 2.
    There are 3 routes of the shortest length 3.
    There are 278940769844931007968 routes of the shortest length 73.
    
  2. Example 2

    Input
    2 5
    0 0
    
    Expected output
    There is 1 route of the shortest length 2.
    
  3. Example 3

    Input
    3 6
    2 4
    1 20
    0 0
    
    Expected output
    There is 1 route of the shortest length 2.
    There are 2 routes of the shortest length 2.
    There are 3 routes of the shortest length 3.
    
  4. Example 4

    Input
    8 19
    0 0
    
    Expected output
    There is 1 route of the shortest length 1.