CF1834B.Maximum Strength

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 xx. 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=x1x2…xn‾X = \overline{x_{1}x_{2} \ldots x_{n}}, and the second material have strength Y=y1y2…yn‾Y = \overline{y_{1}y_{2} \ldots y_{n}}. Then the strength of the weapon is calculated as ∣x1−y1∣+∣x2−y2∣+…+∣xn−yn∣|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 LL to RR, inclusive. Help him find the maximum possible strength of the weapon he can obtain.

An integer C=c1c2…ck‾C = \overline{c_{1}c_{2} \ldots c_{k}} is defined as an integer obtained by sequentially writing the digits c1,c2,…,ckc_1, c_2, \ldots, c_k from left to right, i.e. 10k−1⋅c1+10k−2⋅c2+…+ck10^{k-1} \cdot c_1 + 10^{k-2} \cdot c_2 + \ldots + c_k.

费多亚正在玩一款名为《塞尔达传说》的新游戏,其中角色的一项能力是将两种材料合成一件武器。每种材料都有其自身的力量值,该值可用一个正整数 xx 表示。合成武器的力量值定义为:两个整数在十进制表示下,对应数位上的数字之差的绝对值之和。

形式化地,设第一种材料的力量值为 X=x1x2…xn‾X = \overline{x_{1}x_{2} \ldots x_{n}},第二种材料的力量值为 Y=y1y2…yn‾Y = \overline{y_{1}y_{2} \ldots y_{n}}。则武器的力量值计算为 ∣x1−y1∣+∣x2−y2∣+…+∣xn−yn∣|x_{1} - y_{1}| + |x_{2} - y_{2}| + \ldots + |x_{n} - y_{n}|。若两整数位数不同,则位数较短者在高位补零。

费多亚拥有从 LL 到 RR(含端点)所有可能力量值的材料,且数量无限。请帮助他找出所能获得的武器的最大可能力量值。

整数 C=c1c2…ck‾C = \overline{c_{1}c_{2} \ldots c_{k}} 定义为将数字 c1,c2,…,ckc_1, c_2, \ldots, c_k 从左到右依次写出所得的整数,即 10k−1⋅c1+10k−2⋅c2+…+ck10^{k-1} \cdot c_1 + 10^{k-2} \cdot c_2 + \ldots + c_k。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤5001 \le t \le 500). The description of the test cases follows.

The first line of each test case contains two integers LL and RR (1≤L≤R<101001 \le L \le R \lt 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 LL and RR do not contain leading zeros.

Note that the input data may not fit into standard 3232-bit or 6464-bit integer data types.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤5001 \le t \le 500)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 LL 和 RR(1≤L≤R<101001 \le L \le R \lt 10^{100}),分别表示 Fedya 所拥有的材料的最小强度与最大强度(以十进制形式给出)。保证整数 LL 和 RR 不含前导零。

注意:输入数据可能无法放入标准的 3232 位或 6464 位整数数据类型中。

输出格式

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

对于每个测试用例,输出一个整数——Fedya 利用给定材料所能获得的武器的最大可能强度。

输入输出样例

  • 输入#1

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

    输出#1

    4
    19
    11
    0
    163
    28

说明/提示

In the first test case, the weapon made from materials with strengths 5353 and 5757 will have the maximum possible strength: ∣5−5∣+∣3−7∣=4|5 - 5| + |3 - 7| = 4.

In the second test case, the maximum strength is achieved with materials with strengths 190190 and 209209: ∣1−2∣+∣9−0∣+∣0−9∣=19|1 - 2| + |9 - 0| + |0 - 9| = 19.

In the fourth test case, there is only one valid strength, so the answer is 00.

In the sixth test case, the maximum strength is achieved with materials with strengths 19091909 and 9090: ∣1−0∣+∣9−0∣+∣0−9∣+∣9−0∣=28|1 - 0| + |9 - 0| + |0 - 9| + |9 - 0| = 28. Note that the shorter integer was padded with leading zeros.

在第一个测试用例中,由强度为 5353 和 5757 的材料制成的武器将具有最大可能的强度:∣5−5∣+∣3−7∣=4|5 - 5| + |3 - 7| = 4。

在第二个测试用例中,强度最大的组合是强度为 190190 和 209209 的材料:∣1−2∣+∣9−0∣+∣0−9∣=19|1 - 2| + |9 - 0| + |0 - 9| = 19。

在第四个测试用例中,仅存在一个有效的强度值,因此答案为 00。

在第六个测试用例中,强度最大的组合是强度为 19091909 和 9090 的材料:∣1−0∣+∣9−0∣+∣0−9∣+∣9−0∣=28|1 - 0| + |9 - 0| + |0 - 9| + |9 - 0| = 28。注意,较短的整数已在前面补零。

输入解题思路,AI测评打分。不知道怎么写?

首页