結果

問題 No.315 世界のなんとか3.5
ユーザー rpy3cpprpy3cpp
提出日時 2015-12-09 00:43:27
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 778 ms / 2,000 ms
コード長 1,640 bytes
コンパイル時間 103 ms
コンパイル使用メモリ 10,776 KB
実行使用メモリ 9,080 KB
最終ジャッジ日時 2023-10-12 22:23:56
合計ジャッジ時間 5,104 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,024 KB
testcase_01 AC 17 ms
8,080 KB
testcase_02 AC 153 ms
8,132 KB
testcase_03 AC 19 ms
8,176 KB
testcase_04 AC 20 ms
8,004 KB
testcase_05 AC 39 ms
8,116 KB
testcase_06 AC 48 ms
8,192 KB
testcase_07 AC 40 ms
8,160 KB
testcase_08 AC 37 ms
8,188 KB
testcase_09 AC 21 ms
8,028 KB
testcase_10 AC 19 ms
8,044 KB
testcase_11 AC 18 ms
8,216 KB
testcase_12 AC 42 ms
8,424 KB
testcase_13 AC 43 ms
8,420 KB
testcase_14 AC 79 ms
8,556 KB
testcase_15 AC 79 ms
8,592 KB
testcase_16 AC 80 ms
8,496 KB
testcase_17 AC 79 ms
8,632 KB
testcase_18 AC 43 ms
8,460 KB
testcase_19 AC 43 ms
8,368 KB
testcase_20 AC 42 ms
8,412 KB
testcase_21 AC 42 ms
8,500 KB
testcase_22 AC 80 ms
8,492 KB
testcase_23 AC 79 ms
8,600 KB
testcase_24 AC 79 ms
8,588 KB
testcase_25 AC 79 ms
8,644 KB
testcase_26 AC 80 ms
8,476 KB
testcase_27 AC 79 ms
8,548 KB
testcase_28 AC 131 ms
8,500 KB
testcase_29 AC 201 ms
8,884 KB
testcase_30 AC 202 ms
9,080 KB
testcase_31 AC 202 ms
8,952 KB
testcase_32 AC 142 ms
8,948 KB
testcase_33 AC 68 ms
8,476 KB
testcase_34 AC 258 ms
8,960 KB
testcase_35 AC 778 ms
9,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)))
0