Calculator expressions
Statement
An old calculator is on display in a museum. Its number keys 1 through N are fixed in one row from left to right, and the keys cannot be moved around. To use the calculator you have to press every number key once, in order, from the 1 at the left end to the N at the right end.
One operator key can go into each gap between number keys. There are three choices.
1) Put in the plus key. The numbers on the two sides are added. 2) Put in the minus key. The number on the right is taken away from the number on the left. 3) Put in no operator key at all. In this case the digits on the two sides join into one number. For example, putting nothing between 2 and 3 makes the single number 23.
There are N number keys, so there are N-1 gaps for operator keys. The calculator handles the joining first, then works through the additions and subtractions that are left from left to right.
Write a program that finds every expression that can be built this way whose result is 0.
For example, suppose N=7. There are 6 gaps for operator keys, and there are 6 expressions whose result is 0, as in the following table. A blank cell in the table means that no operator key was put into that gap.
| Expression | 1 and 2 | 2 and 3 | 3 and 4 | 4 and 5 | 5 and 6 | 6 and 7 |
|---|---|---|---|---|---|---|
| 1+2-3+4-5-6+7 | + | - | + | - | - | + |
| 1+2-3-4+5+6-7 | + | - | - | + | + | - |
| 1-2 3+4+5+6+7 | - | + | + | + | + | |
| 1-2 3-4 5+6 7 | - | - | + | |||
| 1-2+3+4-5+6-7 | - | + | + | - | + | - |
| 1-2-3-4-5+6+7 | - | - | - | - | + | + |
Read the third expression in the table. No key was put between 2 and 3, so the two digits make the single number 23. The value of this expression is therefore 1-23+4+5+6+7, which works out to 0. In the same way the fourth expression becomes 1-23-45+67, whose value is 0.
Several signs with a value of N written on them sit in front of the calculator. Find the answer for each sign separately and print it.
Input and Output Examples
Example 1
Input
1 7
Output
1+2-3+4-5-6+7 1+2-3-4+5+6-7 1-2 3+4+5+6+7 1-2 3-4 5+6 7 1-2+3+4-5+6-7 1-2-3-4-5+6+7
Example 2
Input
1 5
Output
1 2-3-4-5
Example 3
Input
2 3 7
Output
1+2-3 1+2-3+4-5-6+7 1+2-3-4+5+6-7 1-2 3+4+5+6+7 1-2 3-4 5+6 7 1-2+3+4-5+6-7 1-2-3-4-5+6+7
Input
The number of signs T in front of the calculator is given first as a natural number. T is a natural number from 1 to 7.
Over the next T lines, the N written on each sign is given one per line. N is a natural number from 3 to 9, and since N is the number of number keys, there are N-1 gaps for operator keys. Two signs may carry the same value of N.
Output
For each sign, print every expression whose result is 0, one per line. Write the digits and the operator symbols in order from the left, and write one space in a gap where no operator key was put in. A gap that joins 2 and 3 is therefore written 2 3, not 23.
The order in which the expressions are printed is settled by comparing the gaps in turn from the left. Within one gap, no operator key at all comes first, the plus key next, and the minus key after that.
No space is left at the end of a line. When a line holds more than one value, the values are separated by a single space.
Requirements
- The number keys cannot be moved around, and every key from 1 through N is pressed in order
- There are N-1 gaps for operator keys, and each gap takes a plus key, a minus key or nothing at all
- In a gap with no key at all, the digits on the two sides join into one number
- When you write the expression, write one space in a gap that was joined
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
2 3 7
1+2-3 1+2-3+4-5-6+7 1+2-3-4+5+6-7 1-2 3+4+5+6+7 1-2 3-4 5+6 7 1-2+3+4-5+6-7 1-2-3-4-5+6+7
1 5
1 2-3-4-5
1 7
1+2-3+4-5-6+7 1+2-3-4+5+6-7 1-2 3+4+5+6+7 1-2 3-4 5+6 7 1-2+3+4-5+6-7 1-2-3-4-5+6+7
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.