Opening the Safe
InterviewTime limit1sMemory limit128 MB
Decide for each given quadruple of digits whether its four numbers can be combined with arithmetic operations and parentheses to make 24.
- Level
Medium5 of 10
- Topics
- Backtracking, Brute force
- Solved
- No attempts yet
Problem
Sanggeun, a secret agent, wants to open a safe that holds classified documents. The safe opens only after the right code is entered, and a code is four digits.
Sanggeun carries a list of codes to try. The list is very long, so he wants to cross out the entries that cannot be the code before he starts.
The correct code satisfies the 24 condition. A code satisfies the 24 condition when you can put addition, subtraction, multiplication, division and parentheses among its four numbers so that the result is 24. You may reorder the four numbers, and each number is used exactly once. Division is real division, so an intermediate value does not have to be an integer, but you may not divide by zero.
For example, (4, 7, 8, 8) satisfies the 24 condition because . On the other hand, (1, 1, 2, 4) and (1, 1, 1, 1) do not satisfy the 24 condition, so there is no reason to try them.
Given every candidate code, write a program that decides for each one whether it satisfies the 24 condition.
Input
The first line contains the number of test cases. The number of test cases is at most 100. Each test case is one line holding four integers , , , () that describe a candidate code.
Output
For each test case, print YES on its own line if the given code satisfies the 24 condition, and NO if it does not.