結果

問題 No.1185 完全な3の倍数
ユーザー もりをもりを
提出日時 2020-08-22 13:23:40
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 146 ms / 2,000 ms
コード長 1,152 bytes
コンパイル時間 230 ms
コンパイル使用メモリ 11,140 KB
実行使用メモリ 57,856 KB
最終ジャッジ日時 2023-08-15 23:57:12
合計ジャッジ時間 9,749 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 146 ms
29,704 KB
testcase_01 AC 113 ms
29,712 KB
testcase_02 AC 112 ms
29,588 KB
testcase_03 AC 112 ms
29,736 KB
testcase_04 AC 113 ms
29,788 KB
testcase_05 AC 112 ms
29,776 KB
testcase_06 AC 112 ms
29,824 KB
testcase_07 AC 120 ms
29,688 KB
testcase_08 AC 113 ms
29,776 KB
testcase_09 AC 110 ms
29,680 KB
testcase_10 AC 113 ms
29,608 KB
testcase_11 AC 114 ms
29,628 KB
testcase_12 AC 114 ms
29,712 KB
testcase_13 AC 117 ms
29,740 KB
testcase_14 AC 121 ms
29,700 KB
testcase_15 AC 115 ms
29,640 KB
testcase_16 AC 112 ms
29,744 KB
testcase_17 AC 112 ms
29,812 KB
testcase_18 AC 116 ms
29,764 KB
testcase_19 AC 115 ms
29,724 KB
testcase_20 AC 111 ms
29,696 KB
testcase_21 AC 111 ms
29,748 KB
testcase_22 AC 111 ms
29,784 KB
testcase_23 AC 119 ms
29,780 KB
testcase_24 AC 112 ms
29,628 KB
testcase_25 AC 112 ms
29,720 KB
testcase_26 AC 113 ms
29,716 KB
testcase_27 AC 116 ms
29,788 KB
testcase_28 AC 115 ms
29,652 KB
testcase_29 AC 115 ms
29,692 KB
testcase_30 AC 113 ms
29,720 KB
testcase_31 AC 117 ms
29,776 KB
testcase_32 AC 113 ms
29,724 KB
testcase_33 AC 114 ms
29,732 KB
testcase_34 AC 116 ms
29,664 KB
testcase_35 AC 113 ms
29,708 KB
testcase_36 AC 115 ms
29,788 KB
testcase_37 AC 114 ms
29,776 KB
testcase_38 AC 114 ms
29,776 KB
testcase_39 AC 114 ms
29,668 KB
testcase_40 AC 111 ms
29,712 KB
testcase_41 AC 116 ms
57,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np

def cond1(x):
    return len(str(x)) >= 2 and x % 3 == 0

def cond2(x):
    x = str(x)
    D = len(x)
    for i in range(D):
        for j in range(i + 1, D):
            p = int(x[i]) + int(x[j])
            if p % 3 != 0:
                return False
    return True

def cond3(x):
    x = str(x)
    D = len(x)
    for i in x:
        if int(i) % 3 == 0:
            return False
    return True

def cond(x):
    return cond1(x) and cond2(x)

n = int(input()) + 1    # 未満

if n <= 1000:
    res = 0
    for i in range(n):
        res += cond(i)
    print(res)
    exit(0)

s = str(n)
d = len(s)

res = 0
for j in range(min(100, n)):
    if cond(j) and cond3(j):
        res += 1

dp = np.zeros((d + 1, 2), dtype='i')
dp[0, 0] = 1

for i in range(d):
    dig = int(s[i])
    # dp[.][0] : 小さいことが未確定
    for j in range(0, 10, 3):
        if j > dig:
            break
        if j == dig:
            dp[i + 1][0] += dp[i][0]
        else:
            dp[i + 1][1] += dp[i][0]
    # dp[.][1] : 小さいことが確定
    for j in range(0, 10, 3):
        dp[i + 1][1] += dp[i][1]

print(dp[d, 1] + res - 4)
0