codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1619C.md (2566B)


      1 Source: https://codeforces.com/problemset/problem/1619/C
      2 
      3 C. Wrong Addition
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Tanya is learning how to add numbers, but so far she is not doing it correctly. She is adding two numbers $a$ and $b$ using the following algorithm:
     22 
     23 1. If one of the numbers is shorter than the other, Tanya adds leading zeros so that the numbers are the same length.
     24 2. The numbers are processed from right to left (that is, from the least significant digits to the most significant).
     25 3. In the first step, she adds the last digit of $a$ to the last digit of $b$ and writes their sum in the answer.
     26 4. At each next step, she performs the same operation on each pair of digits in the same place and writes the result to the left side of the answer.
     27 
     28 For example, the numbers $a = 17236$ and $b = 3465$ Tanya adds up as follows:
     29 
     30 $$ \large{ \begin{array}{r} + \begin{array}{r} 17236\\ 03465\\ \end{array} \\ \hline \begin{array}{r} 1106911 \end{array} \end{array}} $$
     31 
     32 * calculates the sum of $6 + 5 = 11$ and writes $11$ in the answer.
     33 * calculates the sum of $3 + 6 = 9$ and writes the result to the left side of the answer to get $911$.
     34 * calculates the sum of $2 + 4 = 6$ and writes the result to the left side of the answer to get $6911$.
     35 * calculates the sum of $7 + 3 = 10$, and writes the result to the left side of the answer to get $106911$.
     36 * calculates the sum of $1 + 0 = 1$ and writes the result to the left side of the answer and get $1106911$.
     37 
     38 As a result, she gets $1106911$.
     39 
     40 You are given two positive integers $a$ and $s$. Find the number $b$ such that by adding $a$ and $b$ as described above, Tanya will get $s$. Or determine that no suitable $b$ exists.
     41 
     42 Input
     43 
     44 The first line of input data contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
     45 
     46 Each test case consists of a single line containing two positive integers $a$ and $s$ ($1 \le a \lt s \le 10^{18}$) separated by a space.
     47 
     48 Output
     49 
     50 For each test case print the answer on a separate line.
     51 
     52 If the solution exists, print a single positive integer $b$. The answer must be written without leading zeros. If multiple answers exist, print any of them.
     53 
     54 If no suitable number $b$ exists, output -1.
     55 
     56 Example
     57 
     58 Input
     59 
     60 ```
     61 6
     62 17236 1106911
     63 1 5
     64 108 112
     65 12345 1023412
     66 1 11
     67 1 20
     68 ```
     69 
     70 Output
     71 
     72 ```
     73 3465
     74 4
     75 -1
     76 90007
     77 10
     78 -1
     79 ```
     80 
     81 Note
     82 
     83 The first test case is explained in the main part of the statement.
     84 
     85 In the third test case, we cannot choose $b$ that satisfies the problem statement.