結果

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

ソースコード

diff #

from functools import lru_cache

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

def count_ahoh_not_young(s):
    n = len(s)
    digits = list(map(int, s))
    
    @lru_cache(maxsize=None)
    def dp(pos, mod3, mod8, has3, tight, leading_zero):
        if pos == n:
            is_ahoh = (mod3 == 0) or has3
            is_young = (mod8 == 0)
            if is_ahoh and not is_young:
                return 1
            else:
                return 0
        limit = digits[pos] if tight else 9
        total = 0
        for d in range(0, limit + 1):
            new_tight = tight and (d == limit)
            new_leading_zero = leading_zero and (d == 0)
            if new_leading_zero:
                new_mod3 = 0
                new_mod8 = 0
                new_has3 = False
            else:
                new_mod3 = (mod3 * 10 + d) % 3
                new_mod8 = (mod8 * 10 + d) % 8
                new_has3 = has3 or (d == 3)
            total += dp(pos + 1, new_mod3, new_mod8, new_has3, new_tight, new_leading_zero)
        return total % MOD
    result = dp(0, 0, 0, False, True, True)
    dp.cache_clear()
    return result

def main():
    A, B = input().split()
    count_B = count_ahoh_not_young(B)
    A_minus_1 = subtract_one(A)
    if A_minus_1 == '-1':
        count_A_minus_1 = 0
    else:
        count_A_minus_1 = count_ahoh_not_young(A_minus_1) if A_minus_1 != '0' else 0
    result = (count_B - count_A_minus_1) % MOD
    print(result)

if __name__ == "__main__":
    main()
0