結果
問題 | No.315 世界のなんとか3.5 |
ユーザー |
|
提出日時 | 2015-12-09 00:43:27 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 958 ms / 2,000 ms |
コード長 | 1,640 bytes |
コンパイル時間 | 162 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 11,648 KB |
最終ジャッジ日時 | 2024-09-14 20:37:06 |
合計ジャッジ時間 | 5,959 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 36 |
ソースコード
def solve(A, B, P):return (f(B, P) - f(A, P) + is_valid(A, P)) % (10**9 + 7)def is_valid(A, P):if len(A) < 6:a = int(A)return (('3' in A) or (a % 3 == 0)) and (a % P != 0)sumA = sum(map(int, A))lowerA = int(A[-5:])return (('3' in A) or (sumA % 3 == 0)) and (lowerA % P != 0)def f(A, P):if P == 8:return fcore(A, P, 4, 8750, 3750)elif P == 80:return fcore(A, P, 5, 98750, 38742)elif P == 800:return fcore(A, P, 6, 998750, 353670)def f_naive(n, P, cum=0):cum %= 3count = 0for i in range(n + 1):if i % P == 0:continueif ('3' in str(i)) or ((i + cum) % 3 == 0):count += 1return countdef fcore(A, P, d, large, small):mod = 10**9 + 7inv9 = 111111112inv10 = 700000005if len(A) <= d:return f_naive(int(A), P)head = A[:-d]tail = A[-d:]n = len(head) - 1pow10 = pow(10, n, mod)pow9 = pow(9, n, mod)cum0 = 0cum1 = 0has_no_3 = Truefor a in head:m = ord(a) - 48 # ord('0') = 48cum0 = (cum0 + pow10 * m) % modpow10 = (pow10 * inv10) % modif has_no_3:if m == 3:has_no_3 = Falsem -= m > 3cum1 = (cum1 + pow9 * m) % modpow9 = (pow9 * inv9) % modif has_no_3:cum = sum(map(int, head))return (large * cum0 - small * cum1 + f_naive(int(tail), P, cum)) % modelse:return (large * cum0 - small * cum1 + int(tail) - int(tail)//P) % modA, B, P = input().split()print(solve(A, B, int(P)))