UnsealTheSafe
SRM 354 · 2007-06-14 · by Andrew_Lazarev
Problem Statement
This problem contains an image that can be viewed in the applet.
A door of a safe is locked by a password. Josh witnessed an employee opening the safe. Here is the information Josh spied.
- The password is a sequence containing exactly N digits..
- The password is entered using the keypad shown in the picture below.
- Every pair of neighboring digits in the password is adjacent on the keypad. Two digits are adjacent on the keypad if they are distinct and have a common edge.
Josh has evil intentions of unsealing the safe, but before he can realize his plan, he wants to know how many different passwords exist. Given the value for N, return the number of possible passwords that satisfy all the constraints given above.
Constraints
- N will be between 2 and 30, inclusive.
2 Returns: 26
if the first button is 1, the second button can be either 2 or 4 if the first button is 2, the second button can be either 1, 3 or 5 if the first button is 3, the second button can be either 2 or 6 if the first button is 4, the second button can be either 1, 5 or 7 if the first button is 5, the second button can be either 2, 4, 6 or 8 if the first button is 6, the second button can be either 3, 5 or 9 if the first button is 7, the second button can be either 4, 8 or 0 if the first button is 8, the second button can be either 5, 7 or 9 if the first button is 9, the second button can be either 6 or 8 if the first button is 0, the second button can be 7 only
3 Returns: 74
25 Returns: 768478331222
4 Returns: 210
5 Returns: 600
Submissions are judged against all 31 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class UnsealTheSafe with a public method long long countPasswords(int N) · 31 test cases · 2 s / 256 MB per case