結果
| 問題 |
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 %= 3
count = 0
for i in range(n + 1):
if i % P == 0:
continue
if ('3' in str(i)) or ((i + cum) % 3 == 0):
count += 1
return count
def fcore(A, P, d, large, small):
mod = 10**9 + 7
inv9 = 111111112
inv10 = 700000005
if len(A) <= d:
return f_naive(int(A), P)
head = A[:-d]
tail = A[-d:]
n = len(head) - 1
pow10 = pow(10, n, mod)
pow9 = pow(9, n, mod)
cum0 = 0
cum1 = 0
has_no_3 = True
for a in head:
m = ord(a) - 48 # ord('0') = 48
cum0 = (cum0 + pow10 * m) % mod
pow10 = (pow10 * inv10) % mod
if has_no_3:
if m == 3:
has_no_3 = False
m -= m > 3
cum1 = (cum1 + pow9 * m) % mod
pow9 = (pow9 * inv9) % mod
if has_no_3:
cum = sum(map(int, head))
return (large * cum0 - small * cum1 + f_naive(int(tail), P, cum)) % mod
else:
return (large * cum0 - small * cum1 + int(tail) - int(tail)//P) % mod
A, B, P = input().split()
print(solve(A, B, int(P)))