結果

問題 No.315 世界のなんとか3.5
ユーザー rpy3cpprpy3cpp
提出日時 2015-12-09 00:20:59
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,610 bytes
コンパイル時間 101 ms
コンパイル使用メモリ 10,948 KB
実行使用メモリ 9,064 KB
最終ジャッジ日時 2023-10-12 22:22:54
合計ジャッジ時間 5,025 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,020 KB
testcase_01 AC 17 ms
8,072 KB
testcase_02 AC 151 ms
8,016 KB
testcase_03 AC 18 ms
8,088 KB
testcase_04 AC 19 ms
8,108 KB
testcase_05 AC 38 ms
8,092 KB
testcase_06 AC 47 ms
8,104 KB
testcase_07 AC 38 ms
8,080 KB
testcase_08 AC 35 ms
8,120 KB
testcase_09 AC 19 ms
8,068 KB
testcase_10 AC 17 ms
8,020 KB
testcase_11 AC 17 ms
8,036 KB
testcase_12 AC 42 ms
8,448 KB
testcase_13 AC 43 ms
8,548 KB
testcase_14 AC 78 ms
8,656 KB
testcase_15 AC 79 ms
8,632 KB
testcase_16 AC 79 ms
8,584 KB
testcase_17 AC 79 ms
8,492 KB
testcase_18 AC 42 ms
8,440 KB
testcase_19 AC 42 ms
8,564 KB
testcase_20 AC 42 ms
8,360 KB
testcase_21 AC 41 ms
8,544 KB
testcase_22 AC 77 ms
8,500 KB
testcase_23 AC 77 ms
8,496 KB
testcase_24 AC 78 ms
8,496 KB
testcase_25 AC 77 ms
8,500 KB
testcase_26 AC 79 ms
8,624 KB
testcase_27 AC 79 ms
8,536 KB
testcase_28 AC 118 ms
8,516 KB
testcase_29 AC 173 ms
8,868 KB
testcase_30 AC 173 ms
8,968 KB
testcase_31 AC 173 ms
8,924 KB
testcase_32 AC 141 ms
8,944 KB
testcase_33 AC 67 ms
8,504 KB
testcase_34 AC 206 ms
8,940 KB
testcase_35 WA -
権限があれば一括ダウンロードができます

ソースコード

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(1, 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:
        return (large * cum0 - small * cum1 + f_naive(int(tail), P, cum0)) % 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