Maximum Strength (Codeforces 1834B)
Tags: Math, greedy

Fedya is playing a new game called "The Legend of Link", in which one of the character's abilities is to combine two materials into one weapon. Each material has its own strength, which can be represented by a positive integer $$$x$$$. The strength of the resulting weapon is determined as the sum of the absolute differences of the digits in the decimal representation of the integers at each position.Formally, let the first material have strength $$$X = \overline{x_{1}x_{2} \ldots x_{n}}$$$, and the second material have strength $$$Y = \overline{y_{1}y_{2} \ldots y_{n}}$$$. Then the strength of the weapon is calculated as $$$|x_{1} - y_{1}| + |x_{2} - y_{2}| + \ldots + |x_{n} - y_{n}|$$$. If the integers have different lengths, then the shorter integer is padded with leading zeros.Fedya has an unlimited supply of materials with all possible strengths from $$$L$$$ to $$$R$$$, inclusive. Help him find the maximum possible strength of the weapon he can obtain.An integer $$$C = \overline{c_{1}c_{2} \ldots c_{k}}$$$ is defined as an integer obtained by sequentially writing the digits $$$c_1, c_2, \ldots, c_k$$$ from left to right, i.e. $$$10^{k-1} \cdot c_1 + 10^{k-2} \cdot c_2 + \ldots + c_k$$$.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). The description of the test cases follows.The first line of each test case contains two integers $$$L$$$ and $$$R$$$ ($$$1 \le L \le R < 10^{100}$$$) — the decimal representation of the integers representing the minimum and maximum strength of the materials that Fedya has. It is guaranteed that the integers $$$L$$$ and $$$R$$$ do not contain leading zeros.Note that the input data may not fit into standard $$$32$$$-bit or $$$64$$$-bit integer data types.

Output: For each test case print one integer — the maximum possible strength of the weapon that Fedya can obtain from the given materials.

Note: In the first test case, the weapon made from materials with strengths $$$53$$$ and $$$57$$$ will have the maximum possible strength: $$$|5 - 5| + |3 - 7| = 4$$$.In the second test case, the maximum strength is achieved with materials with strengths $$$190$$$ and $$$209$$$: $$$|1 - 2| + |9 - 0| + |0 - 9| = 19$$$.In the fourth test case, there is only one valid strength, so the answer is $$$0$$$.In the sixth test case, the maximum strength is achieved with materials with strengths $$$1909$$$ and $$$90$$$: $$$|1 - 0| + |9 - 0| + |0 - 9| + |9 - 0| = 28$$$. Note that the shorter integer was padded with leading zeros.

Sample Inputs:
6
53 57
179 239
13 37
132228 132228
54943329752812629795 55157581939688863366
88 1914

Sample Outputs:
4
19
11
0
163
28

Solution (Python):
t = int(input())
for _ in range(t):
    (a, b) = map(int, input().split())
    if a == b:
        print(0)
        continue
    astr = str(a)
    bbstr = str(b)
    leng = len(bbstr) - len(astr)
    if leng != 0:
        for k in range(leng):
            if k == 0:
                alpha = '0'
                continue
            alpha += '0'
        aastr = alpha + astr
        print(int(bbstr[0]) - int(aastr[0]) + 9 * (len(bbstr) - 1))
        continue
    ut = 0
    for l in range(len(bbstr)):
        if bbstr[l] == astr[l]:
            ut += 1
            continue
        break
    print(int(bbstr[ut]) - int(astr[ut]) + 9 * (len(astr) - 1 - ut))

Solution (Python):
for tests in range(int(input())):
    (x, y) = map(str, input().split())
    check = 0
    if len(x) == len(y):
        for i in range(len(x)):
            if not x[i] == y[i]:
                check = i
                break
        if not x == y:
            print(9 * (len(x) - check - 1) + abs(int(x[check]) - int(y[check])))
        else:
            print(0)
    elif len(x) > len(y):
        print(9 * (len(x) - 1) + int(x[0]))
    elif len(x) < len(y):
        print(9 * (len(y) - 1) + int(y[0]))

Solution (Python):
def pwr(a, b, mod=0):
    result = 1
    if mod == 0:
        while b:
            if b & 1:
                result *= a
            a *= a
            b = b >> 1
    else:
        while b:
            if b & 1:
                result *= a
                result = result % mod
            a *= a
            a = a % mod
            b = b >> 1
    return result

def solve():
    (s1, s2) = input().split()
    m = len(s2)
    sum_val = 0
    index = 0
    if len(s1) < len(s2):
        s1 = '0' * (m - len(s1)) + s1
    for i in range(m):
        index = i
        if s1[i] != s2[i]:
            w = int(s1[i]) - int(s2[i])
            sum_val += abs(w)
            break
    print(sum_val + 9 * (m - index - 1))
if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        solve()