ROSECODE 536
Counting unlock patterns
Given a square tiled key lock screen and two integers and , where . Here are the rules for a valid pattern:
- Each pattern must connect at least keys and at most 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 , 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 be the total number of unlock patterns of the key lock screen, which consist of minimum of keys and maximum keys. You are given , which is the well-known total number of possible Android unlock patterns.
Find .
Thanks to baihacker for the idea.
- Each pattern must connect at least
- 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

- 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
Find
Thanks to baihacker for the idea.