結果

問題 No.1185 完全な3の倍数
ユーザー matsu7874matsu7874
提出日時 2020-08-22 14:05:44
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,146 bytes
コンパイル時間 97 ms
コンパイル使用メモリ 10,960 KB
実行使用メモリ 8,604 KB
最終ジャッジ日時 2023-08-16 00:05:24
合計ジャッジ時間 2,057 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
8,404 KB
testcase_01 AC 16 ms
8,460 KB
testcase_02 AC 17 ms
8,404 KB
testcase_03 AC 16 ms
8,452 KB
testcase_04 AC 15 ms
8,408 KB
testcase_05 AC 14 ms
8,404 KB
testcase_06 AC 14 ms
8,456 KB
testcase_07 AC 15 ms
8,448 KB
testcase_08 AC 16 ms
8,456 KB
testcase_09 AC 17 ms
8,544 KB
testcase_10 AC 16 ms
8,536 KB
testcase_11 AC 15 ms
8,464 KB
testcase_12 AC 15 ms
8,396 KB
testcase_13 AC 15 ms
8,532 KB
testcase_14 AC 16 ms
8,540 KB
testcase_15 AC 15 ms
8,456 KB
testcase_16 AC 14 ms
8,112 KB
testcase_17 AC 15 ms
8,548 KB
testcase_18 AC 16 ms
8,408 KB
testcase_19 AC 15 ms
8,404 KB
testcase_20 AC 15 ms
8,396 KB
testcase_21 AC 15 ms
8,448 KB
testcase_22 AC 15 ms
8,404 KB
testcase_23 AC 14 ms
8,404 KB
testcase_24 AC 15 ms
8,524 KB
testcase_25 AC 15 ms
8,460 KB
testcase_26 AC 13 ms
8,596 KB
testcase_27 AC 15 ms
8,404 KB
testcase_28 AC 14 ms
8,528 KB
testcase_29 AC 15 ms
8,604 KB
testcase_30 AC 14 ms
8,572 KB
testcase_31 AC 14 ms
8,580 KB
testcase_32 AC 16 ms
8,452 KB
testcase_33 AC 15 ms
8,580 KB
testcase_34 AC 15 ms
8,584 KB
testcase_35 AC 14 ms
8,592 KB
testcase_36 AC 14 ms
8,392 KB
testcase_37 AC 14 ms
8,580 KB
testcase_38 AC 15 ms
8,408 KB
testcase_39 AC 14 ms
8,552 KB
testcase_40 AC 14 ms
8,536 KB
testcase_41 AC 15 ms
8,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import math


def bf(n):
    d = int(math.log10(n)) + 1
    digits = [int(c) for c in str(n)]

    cnt = 0
    for i in range(12, n+1, 3):
        if i < 100:
            cnt += 1
        elif all([c in '0369' for c in str(i)]):
            cnt += 1
    return cnt


def solve(n):
    d = int(math.log10(n)) + 1
    digits = [int(c) for c in str(n)]

    leading_zero = [0, 0, 30] + [3*4**i for i in range(2, 10)]
    if n < 100:
        cnt = 0
        for i in range(12, n+1, 3):
            cnt += 1
        return (cnt)
    else:
        dp = [[0]*2 for i in range(d)]
        GREATEST = 0
        LESS = 1
        dp[0][GREATEST] = 1 if digits[0] % 3 == 0 else 0
        dp[0][LESS] = max(digits[0]-1, 0)//3
        for i in range(1, d):
            dp[i][GREATEST] = dp[i-1][GREATEST] * \
                (1 if digits[i] % 3 == 0 else 0)
            if digits[i] > 0:
                dp[i][LESS] = dp[i-1][GREATEST] * (max(digits[i]-1, 0)//3 + 1)
            dp[i][LESS] += dp[i-1][LESS] * 4
        return (sum(dp[d-1]) + sum(leading_zero[:d]))


def main():
    n = int(input())
    print(solve(n))


if __name__ == "__main__":
    main()
0