結果

問題 No.260 世界のなんとか3
ユーザー gew1fw
提出日時 2025-06-12 18:28:23
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,893 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 82,160 KB
実行使用メモリ 88,868 KB
最終ジャッジ日時 2025-06-12 18:29:07
合計ジャッジ時間 4,019 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 MLE * 2
other AC * 1 TLE * 4 MLE * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

def subtract_one(s):
    s_list = list(s)
    i = len(s_list) - 1
    while i >= 0 and s_list[i] == '0':
        i -= 1
    if i < 0:
        return '0'
    s_list[i] = str(int(s_list[i]) - 1)
    for j in range(i + 1, len(s_list)):
        s_list[j] = '9'
    new_s = ''.join(s_list).lstrip('0')
    if not new_s:
        return '0'
    return new_s

def count(s):
    digits = list(map(int, s))
    n = len(digits)
    from collections import defaultdict

    dp = defaultdict(int)
    dp[(True, 0, 0, False, True)] = 1

    for pos in range(n):
        next_dp = defaultdict(int)
        for state, cnt in dp.items():
            tight, mod3, mod8, has_three, leading_zero = state
            max_d = digits[pos] if tight else 9
            for d in range(0, max_d + 1):
                new_tight = tight and (d == max_d)
                new_mod3 = (mod3 + d) % 3
                new_mod8 = (mod8 * 10 + d) % 8
                new_has_three = has_three or (d == 3)
                new_leading_zero = leading_zero and (d == 0)
                key = (new_tight, new_mod3, new_mod8, new_has_three, new_leading_zero)
                next_dp[key] = (next_dp[key] + cnt) % MOD
        dp = next_dp

    result = 0
    for state, cnt in dp.items():
        tight, mod3, mod8, has_three, leading_zero = state
        if leading_zero:
            continue
        aho = (mod3 == 0) or has_three
        if not aho:
            continue
        if mod8 != 0:
            result = (result + cnt) % MOD
    return result

A, B = input().split()

if A == '0':
    a_minus_1 = '-1'
else:
    a_minus_1 = subtract_one(A)

if a_minus_1 == '0':
    ans = count(B)
else:
    if a_minus_1 == '-1':
        total_B = count(B)
        ans = total_B % MOD
    else:
        total_B = count(B)
        total_A_minus_1 = count(a_minus_1)
        ans = (total_B - total_A_minus_1) % MOD

print(ans)
0