結果

問題 No.315 世界のなんとか3.5
ユーザー rpy3cpprpy3cpp
提出日時 2015-12-08 23:54:25
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 1,555 bytes
コンパイル時間 575 ms
コンパイル使用メモリ 11,132 KB
実行使用メモリ 9,020 KB
最終ジャッジ日時 2023-10-12 22:14:36
合計ジャッジ時間 2,876 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,356 KB
testcase_01 RE -
testcase_02 RE -
testcase_03 AC 18 ms
8,316 KB
testcase_04 AC 18 ms
8,404 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 AC 19 ms
8,312 KB
testcase_10 AC 17 ms
8,404 KB
testcase_11 AC 17 ms
8,440 KB
testcase_12 AC 42 ms
8,392 KB
testcase_13 AC 42 ms
8,440 KB
testcase_14 AC 77 ms
8,488 KB
testcase_15 AC 79 ms
8,596 KB
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 78 ms
8,484 KB
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 AC 174 ms
8,984 KB
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
権限があれば一括ダウンロードができます

ソースコード

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 f8(A)
    elif P == 80:
        return f80(A)
    elif P == 800:
        return f800(A)

def f80(A):
    pass

def f800(A):
    pass

def f8(A):
    mod = 10**9 + 7
    inv9 = 111111112
    inv10 = 700000005
    if len(A) < 5:
        return f8_naive(int(A))
    head = A[:-4]
    tail = A[-4:]
    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:
        return (8750 * cum0 - 3750 * cum1 + f8_naive(int(tail), cum0)) % mod
    else:
        return (8750 * cum0 - 3750 * cum1 + int(tail) - int(tail)//8) % mod


def f8_naive(n, cum=0):
    cum %= 3
    count = 0
    for i in range(1, n + 1):
        if i % 8 == 0:
            continue
        if ('3' in str(i)) or ((i + cum) % 3 == 0):
            count += 1
    return count

A, B, P = input().split()
print(solve(A, B, int(P)))
0