結果

問題 No.260 世界のなんとか3
ユーザー lam6er
提出日時 2025-04-15 21:10:56
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,648 bytes
コンパイル時間 474 ms
コンパイル使用メモリ 82,244 KB
実行使用メモリ 205,036 KB
最終ジャッジ日時 2025-04-15 21:16:57
合計ジャッジ時間 4,129 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 MLE * 2
other MLE * 1 -- * 26
権限があれば一括ダウンロードができます

ソースコード

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':
        s_list[i] = '9'
        i -= 1
    if i < 0:
        return '0'
    s_list[i] = str(int(s_list[i]) - 1)
    new_s = ''.join(s_list)
    if new_s[0] == '0' and len(new_s) > 1:
        new_s = new_s.lstrip('0')
        if not new_s:
            return '0'
    return new_s

def count(s):
    if s == '0':
        return 0
    digits = list(map(int, s))
    n = len(digits)
    dp = [{} for _ in range(n + 1)]
    dp[0][(True, False, 0, 0)] = 1
    for i in range(n):
        current = dp[i]
        next_dp = {}
        for (tight, has_three, sum_mod3, mod8), cnt in current.items():
            max_d = digits[i] if tight else 9
            for d in range(0, max_d + 1):
                new_tight = tight and (d == max_d)
                new_has_three = has_three or (d == 3)
                new_sum_mod3 = (sum_mod3 + d) % 3
                new_mod8 = (mod8 * 10 + d) % 8
                key = (new_tight, new_has_three, new_sum_mod3, new_mod8)
                if key in next_dp:
                    next_dp[key] = (next_dp[key] + cnt) % MOD
                else:
                    next_dp[key] = cnt % MOD
        dp[i + 1] = next_dp
    total = 0
    for (tight, has_three, sum_mod3, mod8), cnt in dp[n].items():
        if (sum_mod3 == 0 or has_three) and mod8 != 0:
            total = (total + cnt) % MOD
    return total

A, B = input().split()
A_minus_1 = subtract_one(A)
count_B = count(B)
count_A_minus_1 = count(A_minus_1) if A_minus_1 != '0' else 0
answer = (count_B - count_A_minus_1) % MOD
print(answer)
0