eolymp
bolt
Try our new interface for solving problems
Problems

Lock Pattern

Lock Pattern

One of the ways to lock some phones, is the lock pattern. To unlock your phone you have to draw a secret pattern on a grid of some points, the pattern will be some line segments connecting these points. Your phone pattern grid consists of four rows with three points in each row. The following image on the left is the representation of the grid, it can be modeled as a \textbf{2D} plane with \textbf{X} and \textbf{Y} coordinates for each point, for example the top left point is \textbf{(1,4)} and the bottom right point is \textbf{(3,1)}. The image on the right is a valid pattern, which connects the following points in this order: \textbf{(3,4) (2,4) (1,2) (2,1) (2,2) (3,2) (3,1) (1,3)}. \includegraphics{https://static.e-olymp.com/content/8b/8bd93bf967ebe66be1a9a831dae6e7b4fea9f7e9.jpg} A valid pattern has the following properties: \begin{enumerate} \item A pattern can be represented using the sequence of points which it's touching for the rst time (in the same order of drawing the pattern), a pattern going from \textbf{(1,1)} to \textbf{(2,2)} is not the same as a pattern going from\textbf{(2,2)} to \textbf{(1,1)}. \item For every two consecutive points \textbf{A} and \textbf{B} in the pattern representation, if the line segment connecting \textbf{A} and\textbf{B} passes through some other points, these points must be in the sequence also and comes before \textbf{A} and \textbf{B}, otherwise the pattern will be invalid. For example a pattern representation which starts with \textbf{(3,1)} then \textbf{(1,3)} is invalid because the segment passes through \textbf{(2,2)} which didn't appear in the pattern representation before \textbf{(3,1)}, and the correct representation for this pattern is \textbf{(3,1) (2,2) (1,3)}. But the pattern \textbf{(2,2) (3,2) (3,1) (1,3) }is valid because \textbf{(2,2)} appeared before \textbf{(3,1)}. \item In the pattern representation we don't mention the same point more than once, even if the pattern will touch this point again through another valid segment, and each segment in the pattern must be going from a point to another point which the pattern didn't touch before and it might go through some points which already appeared in the pattern. \item The length of a pattern is the sum of the Manhattan distances between every two consecutive points in the pattern representation. The Manhattan distance between two points \textbf{(X_1,Y_1)} and \textbf{(X_2,Y_2)} is \textbf{|X_1-X_2|+|Y_1-Y_2|}(where \textbf{|X|} means the absolute value of X). The length of the pattern in the above image is: \textbf{1 + 3 + 2 + 1 + 1 + 1 + 4 = 13}. \item A pattern must touch at least two points. \end{enumerate} Unfortunately you forgot your phone's pattern, but you remember the length of the pattern and a set of points \textbf{S }which are not touched by the pattern for sure (the points which are not in \textbf{S} might and might not be touched by the pattern), and you decided to try all the valid patterns which satisfy what you remember. Before doing this, you have to write a program to calculate for you the number of different valid patterns. \InputFile Your program will be tested on one or more test cases. The first line of the input will be a single integer \textbf{T}, the number of test cases (\textbf{1} ≤ \textbf{T} ≤ \textbf{100}). Followed by the test cases, the first line of a test case contains two integers \textbf{L} and \textbf{N} separated by a single space. \textbf{L} (\textbf{1} ≤ \textbf{L} ≤ \textbf{1000}) is the length of the pattern (as described above), and \textbf{N} (\textbf{0} ≤ \textbf{N} ≤ \textbf{12}) is the number of points that you are sure they are not touched by the pattern, followed by \textbf{N} lines each line contains two integers \textbf{X} (\textbf{1} ≤ \textbf{X} ≤ \textbf{3}) and \textbf{Y} (\textbf{1} ≤ \textbf{Y} ≤ \textbf{4}) separated by a single space, representing the coordinates of one of the points which are not touched by the pattern for sure, the \textbf{N} points are distinct. \OutputFile For each test case, if there are no valid patterns according to what you remember, print on a single line "\textbf{BAD MEMORY}" (without the quotes), otherwise print the number of different valid patterns.
Time limit 1 second
Memory limit 64 MiB
Input example #1
3
1 0
2 10
1 1
1 2
1 3
2 1
2 2
2 3
2 4
3 2
3 3
3 4
1 3
1 4
2 4
3 4
Output example #1
34
BAD MEMORY
24
Source Arab Collegiate Programming Contest 2012