結果
問題 | No.315 世界のなんとか3.5 |
ユーザー |
![]() |
提出日時 | 2025-04-16 16:29:29 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,727 bytes |
コンパイル時間 | 442 ms |
コンパイル使用メモリ | 82,040 KB |
実行使用メモリ | 86,324 KB |
最終ジャッジ日時 | 2025-04-16 16:31:10 |
合計ジャッジ時間 | 4,888 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 12 TLE * 1 -- * 23 |
ソースコード
import sys from collections import defaultdict 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: return '0' s_list[i] = str(int(s_list[i]) - 1) if s_list[0] == '0' and len(s_list) > 1: return ''.join(s_list).lstrip('0') or '0' return ''.join(s_list) def count_aho(s, P_val): n = len(s) dp_prev = defaultdict(int) dp_prev[(True, 0, False, True)] = 1 for i in range(n): dp_curr = defaultdict(int) digit = int(s[i]) for (tight, mod3, has3, leading_zero), cnt in dp_prev.items(): max_d = digit if tight else 9 for d in range(0, max_d + 1): new_tight = tight and (d == max_d) new_leading_zero = leading_zero and (d == 0) new_mod3 = (mod3 * 10 + d) % 3 new_has3 = has3 or (d == 3) key = (new_tight, new_mod3, new_has3, new_leading_zero) dp_curr[key] = (dp_curr[key] + cnt) % MOD dp_prev = dp_curr res = 0 for (tight, mod3, has3, leading_zero), cnt in dp_prev.items(): if not leading_zero and (mod3 == 0 or has3): res = (res + cnt) % MOD return res def count_aho_p(s, P_val): n = len(s) dp_prev = defaultdict(int) dp_prev[(True, 0, 0, False, True)] = 1 for i in range(n): dp_curr = defaultdict(int) digit = int(s[i]) for (tight, mod3, modP, has3, leading_zero), cnt in dp_prev.items(): max_d = digit if tight else 9 for d in range(0, max_d + 1): new_tight = tight and (d == max_d) new_leading_zero = leading_zero and (d == 0) new_mod3 = (mod3 * 10 + d) % 3 new_modP = (modP * 10 + d) % P_val new_has3 = has3 or (d == 3) key = (new_tight, new_mod3, new_modP, new_has3, new_leading_zero) dp_curr[key] = (dp_curr[key] + cnt) % MOD dp_prev = dp_curr res = 0 for (tight, mod3, modP, has3, leading_zero), cnt in dp_prev.items(): if not leading_zero and (mod3 == 0 or has3) and modP == 0: res = (res + cnt) % MOD return res def main(): A, B, P = sys.stdin.read().split() P_val = int(P) A_minus_1 = subtract_one(A) fB = count_aho(B, P_val) fA = count_aho(A_minus_1, P_val) if A != '0' else 0 total_f = (fB - fA) % MOD gB = count_aho_p(B, P_val) gA = count_aho_p(A_minus_1, P_val) if A != '0' else 0 total_g = (gB - gA) % MOD ans = (total_f - total_g) % MOD print(ans) if __name__ == "__main__": main()