Task DescriptionDiscussion (0)
Task :: z-expression
Mr Z has a new challenge prepared for you. He will give you:
-digits that you are allowed to use to create certain numbers,
-allowed operators you can use with the created numbers
-a target number that you should evaluate using the numbers created with the allowed digits and the allowed operators.
You need to create the smallest expression in order to evaluate to the target number N.
The expression can contain only the allowed characters.
Example: We need an expression from the digits and operators "023*+" which evaluates to 15. One possible expression that is created using minimal number of characters is: 3*3+3+3.
The multiply "*" and add "+" operands are special that they are always producing at most 1000. Meaning, whenever the result is more than 1000, they are always giving 1000 as result.
The division "/" is always returning the integer part of the division. e.g. 11/3 equals to 3.
The subtraction "-" never returns negative numbers. Meaning, whenever the result is a negative number, they are always converted to 0.
Note: Remember, * and / have precedence over + and - .
Explanation: We can't express anything without any digits
Explanation: 3*3+3+3
Explanation: 20-2-3
Explanation: 32/2
-digits that you are allowed to use to create certain numbers,
-allowed operators you can use with the created numbers
-a target number that you should evaluate using the numbers created with the allowed digits and the allowed operators.
You need to create the smallest expression in order to evaluate to the target number N.
The expression can contain only the allowed characters.
Example: We need an expression from the digits and operators "023*+" which evaluates to 15. One possible expression that is created using minimal number of characters is: 3*3+3+3.
The multiply "*" and add "+" operands are special that they are always producing at most 1000. Meaning, whenever the result is more than 1000, they are always giving 1000 as result.
The division "/" is always returning the integer part of the division. e.g. 11/3 equals to 3.
The subtraction "-" never returns negative numbers. Meaning, whenever the result is a negative number, they are always converted to 0.
Note: Remember, * and / have precedence over + and - .
INPUT:
From the first line of the standard input read the sequence of characters allowed to form the expression. It will contain characters from "0123456789*/+-" . No character will be shown twice.
In the second line will be N, the target number we want to achieve with the expression. (0<=N<=1000)
From the first line of the standard input read the sequence of characters allowed to form the expression. It will contain characters from "0123456789*/+-" . No character will be shown twice.
In the second line will be N, the target number we want to achieve with the expression. (0<=N<=1000)
OUTPUT:
Output the length of the smallest expression.If the target number can't be expressed, return "0" without quotes
Output the length of the smallest expression.If the target number can't be expressed, return "0" without quotes
Input
Output
*/+-
12
12
Output
0
Explanation: We can't express anything without any digits
Input
Output
023*+
15
15
Output
7
Explanation: 3*3+3+3
Input
Output
023*+-
15
15
Output
6
Explanation: 20-2-3
Input
Output
23*+-/
16
16
Output
4
Explanation: 32/2
Input
Output
35679/
393
393
Output
3
Submit Solution
Available Languages
Task info
| Name: | z-expression |
| Time: | 1 sec. |
| Memory: | 32 MB |
| #Tests: | 25 |
| AddedBy: | dejandenib |
Task Ratings
| Difficulty: | 4.3 (3 votes) |
| Quality: | 5 (3 votes) |
Acceptance Rate
Recent Submissions
Fastest Solutions
| User | Time |
|---|---|
| mister | 0.405 s. |
| crusader | 0.45 s. |
| Mr.Ra16bit | 0.54 s. |
| _qwAker_ | 0.84 s. |
| algoboy | 0.855 s. |
| senator | 1.38 s. |
| Adamka | 1.74 s. |
| Amtrix | 6.6 s. |
| dejandenib | 13.245 s. |
Solved By
Home
Training
Competitions
Forum
FAQ