結果
問題 | No.260 世界のなんとか3 |
ユーザー |
![]() |
提出日時 | 2025-03-20 21:08:05 |
言語 | PyPy3 (7.3.15) |
結果 |
MLE
|
実行時間 | - |
コード長 | 2,272 bytes |
コンパイル時間 | 159 ms |
コンパイル使用メモリ | 82,580 KB |
実行使用メモリ | 236,184 KB |
最終ジャッジ日時 | 2025-03-20 21:08:53 |
合計ジャッジ時間 | 4,345 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 MLE * 1 |
other | AC * 1 TLE * 6 MLE * 20 |
ソースコード
import sysfrom collections import defaultdictMOD = 10**9 + 7def subtract_one(s):s_list = list(s)i = len(s_list) - 1while i >= 0 and s_list[i] == '0':s_list[i] = '9'i -= 1if i >= 0:s_list[i] = str(int(s_list[i]) - 1)new_s = ''.join(s_list).lstrip('0')return new_s if new_s else '0'def count(s):n = len(s)memo = [defaultdict(int) for _ in range(n + 1)]memo[0][(True, 0, False, 0, True)] = 1for i in range(n):current_memo = memo[i]if not current_memo:continuenext_memo = defaultdict(int)for state, cnt in current_memo.items():leading_zero_prev, sum_mod3_prev, has_3_prev, mod8_prev, tight_prev = stateupper_digit = int(s[i]) if tight_prev else 9for d in range(0, upper_digit + 1):new_tight = tight_prev and (d == upper_digit)if leading_zero_prev:if d == 0:new_leading_zero = Truenew_sum_mod3 = sum_mod3_prevnew_has_3 = has_3_prevnew_mod8 = mod8_prevelse:new_leading_zero = Falsenew_sum_mod3 = d % 3new_has_3 = (d == 3)new_mod8 = d % 8else:new_leading_zero = Falsenew_sum_mod3 = (sum_mod3_prev + d) % 3new_has_3 = has_3_prev or (d == 3)new_mod8 = (mod8_prev * 10 + d) % 8new_state = (new_leading_zero, new_sum_mod3, new_has_3, new_mod8, new_tight)next_memo[new_state] = (next_memo[new_state] + cnt) % MODmemo[i + 1] = next_memototal = 0for state, cnt in memo[n].items():leading_zero, sum_mod3, has_3, mod8, _ = stateif leading_zero:continueif (sum_mod3 == 0 or has_3) and mod8 != 0:total = (total + cnt) % MODreturn totaldef main():A, B = sys.stdin.readline().split()a_minus_1 = subtract_one(A)ans = (count(B) - count(a_minus_1)) % MODprint(ans)if __name__ == '__main__':main()