結果

問題 No.315 世界のなんとか3.5
ユーザー gew1fw
提出日時 2025-06-12 19:41:45
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,844 bytes
コンパイル時間 150 ms
コンパイル使用メモリ 82,460 KB
実行使用メモリ 86,784 KB
最終ジャッジ日時 2025-06-12 19:41:55
合計ジャッジ時間 9,457 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12 TLE * 3 -- * 21
権限があれば一括ダウンロードができます

ソースコード

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

def count_aho_and_p(s, p_val):
    n = len(s)
    dp = [[[[[0] * p_val for _ in range(3)] for __ in range(2)] for ___ in range(2)] for ____ in range(2)]
    dp[1][1][0][0][0] = 1
    for i in range(n):
        next_dp = [[[[[0] * p_val for _ in range(3)] for __ in range(2)] for ___ in range(2)] for ____ in range(2)]
        max_digit = int(s[i])
        for tight in [0, 1]:
            for leading_zero in [0, 1]:
                for has_three in [0, 1]:
                    for sum_mod3 in [0, 1, 2]:
                        for mod_p in range(p_val):
                            count = dp[tight][leading_zero][has_three][sum_mod3][mod_p]
                            if count == 0:
                                continue
                            upper = max_digit if tight else 9
                            for d in range(upper + 1):
                                new_tight = 1 if (tight and d == upper) else 0
                                new_leading_zero = 1 if (leading_zero and d == 0) else 0
                                new_has_three = 1 if (has_three or d == 3) else 0
                                new_sum_mod3 = (sum_mod3 + d) % 3 if not new_leading_zero else 0
                                new_mod_p = (mod_p * 10 + d) % p_val if not new_leading_zero else 0
                                next_dp[new_tight][new_leading_zero][new_has_three][new_sum_mod3][new_mod_p] = (
                                    next_dp[new_tight][new_leading_zero][new_has_three][new_sum_mod3][new_mod_p] + count
                                ) % MOD
        dp = next_dp
    total = 0
    for tight in [0, 1]:
        for leading_zero in [0, 1]:
            for has_three in [0, 1]:
                for sum_mod3 in [0, 1, 2]:
                    for mod_p in range(p_val):
                        if leading_zero:
                            continue
                        if (has_three or sum_mod3 == 0) and mod_p != 0:
                            total = (total + dp[tight][leading_zero][has_three][sum_mod3][mod_p]) % MOD
    return total

def main():
    import sys
    input = sys.stdin.read().split()
    A = input[0]
    B = input[1]
    P = int(input[2])
    
    A_minus_1 = subtract_one(A)
    
    def f(s):
        if s == '0':
            return 0
        return count_aho_and_p(s, P)
    
    result_B = f(B)
    result_A_minus_1 = f(A_minus_1)
    answer = (result_B - result_A_minus_1) % MOD
    print(answer)

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