結果
問題 |
No.260 世界のなんとか3
|
ユーザー |
![]() |
提出日時 | 2025-06-12 20:32:40 |
言語 | PyPy3 (7.3.15) |
結果 |
MLE
|
実行時間 | - |
コード長 | 1,848 bytes |
コンパイル時間 | 217 ms |
コンパイル使用メモリ | 81,792 KB |
実行使用メモリ | 103,468 KB |
最終ジャッジ日時 | 2025-06-12 20:33:46 |
合計ジャッジ時間 | 4,870 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 MLE * 1 |
other | AC * 1 MLE * 26 |
ソースコード
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()