Bob's Operator Puzzle

medium backtracking strings math 3000 ms · 256 MB

Bob's Operator Puzzle

Alice writes a string of n decimal digits on a chalkboard and picks a target value T. She hands the chalk to Bob, who may fill each of the n-1 gaps between consecutive digits with one of the symbols +, -, *, or nothing at all (which simply glues the two digits together into one longer number). The result is an arithmetic expression using the digits in their original order.

Two rules apply. First, no number formed this way may have a leading zero — the number 0 by itself is fine, but 05 or 00 is not. Second, the expression is evaluated with the usual rules of arithmetic: * binds tighter than + and -, and every + or - is a binary operator sitting between two numbers (there is no unary minus).

Alice wants to know how many of Bob's expressions evaluate to exactly T. Since there can be a lot of them, she only asks to see the lexicographically smallest 100 — comparing the resulting expressions as plain strings, character by character, using their usual character codes (so * < + < - < any digit).

Input

The first line contains an integer q (1 ≤ q ≤ 200) — the number of test cases.

Each of the next q lines contains a digit string s and an integer T, separated by a single space: 1 ≤ |s| ≤ 10, every character of s is a digit 0-9, and -10^12 ≤ T ≤ 10^12.

Output

For each test case, in the order given, print a line with a single integer — the number of ways Bob can fill the gaps so the resulting expression equals T. Then print the lexicographically smallest min(count, 100) such expressions, one per line, in increasing order. If the count is 0, print no expression lines for that case.

Notes

In the first example, s = "123" and T = 6. Two of the sixteen possible expressions equal 6: 1*2*3 and 1+2+3. Comparing them as strings, 1*2*3 comes first because * (character code 42) is smaller than + (character code 43) at the second position.

For s = "105" and T = 5, the fillings 1+05, 1-05 and 1*05 are all illegal because 05 has a leading zero. The only two legal fillings that equal 5 are 1*0+5 and 10-5.

Example 1

Input

2
123 6
105 5

Output

2
1*2*3
1+2+3
2
1*0+5
10-5

Example 2

Input

3
0 0
9 5
10 1

Output

1
0
0
2
1+0
1-0

Hints · reveal one at a time if you're stuck

Hint 1

With at most 10 digits there are only 9 gaps, and each gap has 4 choices. Could you simply try every filling, as long as you can check each one quickly?

Hint 2

Evaluate while you scan left to right instead of re-parsing the finished string. The tricky part is precedence: keep the running total, plus the value of the product term that is still open, so a * can be folded in correctly.

Hint 3

Enumerate by depth-first search, trying the operators in the order *, +, - and, inside that, the shorter operands first. Because of the character codes, this order already produces the expressions in lexicographic order, so you can stop after 100 matches. Skip any operand that starts with a 0 unless it is just 0.

report an issue Prefer your own editor? sooks prep 81 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.