← RoseCode

ROSECODE 536

Counting unlock patterns

liuguangxi · Math ·

Given a square tiled n×n key lock screen and two integers k1 and k2, where 1k1k2n2. Here are the rules for a valid pattern:

- Each pattern must connect at least k1 keys and at most k2 keys.
- All the keys must be distinct.
- If the line connecting two consecutive keys in the pattern passes through any other keys, the other keys must have previously selected in the pattern. No jumps through non selected key are allowed.
- The order of keys used matters.

For n=3, here are some invalid and valid patterns:



- Invalid move: 4 -> 1 -> 3 -> 6
Line 1 -> 3 passes through key 2 which had not been selected in the pattern.
- Invalid move: 4 -> 1 -> 9 -> 2
Line 1 -> 9 passes through key 5 which had not been selected in the pattern.
- Valid move: 2 -> 4 -> 1 -> 3 -> 6
Line 1 -> 3 is valid because it passes through key 2, which had been selected in the pattern.
- Valid move: 6 -> 5 -> 4 -> 1 -> 9 -> 2
Line 1 -> 9 is valid because it passes through key 5, which had been selected in the pattern.

Define S(n,k1,k2) be the total number of unlock patterns of the n×n key lock screen, which consist of minimum of k1 keys and maximum k2 keys. You are given S(3,4,9)=389112, which is the well-known total number of possible Android unlock patterns.

Find S(5,4,25).
Thanks to baihacker for the idea.