結果
| 問題 |
No.260 世界のなんとか3
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 13:17:08 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 1,893 bytes |
| コンパイル時間 | 194 ms |
| コンパイル使用メモリ | 82,656 KB |
| 実行使用メモリ | 81,920 KB |
| 最終ジャッジ日時 | 2025-06-12 13:20:12 |
| 合計ジャッジ時間 | 39,845 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 MLE * 1 |
| other | AC * 1 TLE * 5 MLE * 21 |
ソースコード
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)
gew1fw