結果

問題 No.8133 ‮Reversed‪
コンテスト
ユーザー K2
提出日時 2026-04-01 22:41:06
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
WA  
実行時間 -
コード長 957 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 153 ms
コンパイル使用メモリ 85,280 KB
実行使用メモリ 400,676 KB
最終ジャッジ日時 2026-04-01 22:41:39
合計ジャッジ時間 5,865 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other WA * 1 TLE * 1 -- * 4
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

# from functools import cache

def rev(n: int):
    return int(str(n)[::-1])


pow10 = [1] * 20
for i in range(1, 20):
    pow10[i] = pow10[i - 1] * 10

memo = {}


def solve(n: int):
    assert n >= 0
    if n in memo:
        return memo[n]
    if n < 100:
        res = 0
        for i in range(1, n + 1):
            res += rev(i)
        memo[n] = res
        return res

    s = str(n)
    L = len(s)
    res = 45

    for i in range(2, L):
        x = pow10[i - 1] - 1
        y = pow10[i - 2] - 1
        res += 45 * pow10[i - 1] * (x - y)

    x = int(s[:-1]) - 1
    y = pow10[L - 2] - 1
    res += 10 * solve(x)
    res += 45 * pow10[L - 1] * (x - y)
    
    v = rev(int(s[:-1]))
    for i in range(int(s[-1]) + 1):
        res += v
        res += i * pow10[L - 1]

    memo[n] = res
    return res


N = int(input())
for _ in range(N):
    l, r = map(int, input().split())
    res = solve(r) - solve(l - 1)
    print(res)
    # print(rev(res))
0