結果

問題 No.1953 8
ユーザー 👑 H20H20
提出日時 2022-04-15 00:05:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 236 ms / 2,000 ms
コード長 1,894 bytes
コンパイル時間 316 ms
コンパイル使用メモリ 87,056 KB
実行使用メモリ 78,672 KB
最終ジャッジ日時 2023-08-26 00:21:25
合計ジャッジ時間 12,194 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 180 ms
77,948 KB
testcase_01 AC 183 ms
78,092 KB
testcase_02 AC 231 ms
78,640 KB
testcase_03 AC 170 ms
77,848 KB
testcase_04 AC 168 ms
77,576 KB
testcase_05 AC 174 ms
77,884 KB
testcase_06 AC 173 ms
77,892 KB
testcase_07 AC 174 ms
77,836 KB
testcase_08 AC 171 ms
77,720 KB
testcase_09 AC 170 ms
77,704 KB
testcase_10 AC 170 ms
77,700 KB
testcase_11 AC 175 ms
78,052 KB
testcase_12 AC 187 ms
78,028 KB
testcase_13 AC 197 ms
78,160 KB
testcase_14 AC 197 ms
78,016 KB
testcase_15 AC 196 ms
78,212 KB
testcase_16 AC 199 ms
77,976 KB
testcase_17 AC 201 ms
78,340 KB
testcase_18 AC 214 ms
78,056 KB
testcase_19 AC 214 ms
78,192 KB
testcase_20 AC 217 ms
77,944 KB
testcase_21 AC 229 ms
78,132 KB
testcase_22 AC 222 ms
78,080 KB
testcase_23 AC 220 ms
78,256 KB
testcase_24 AC 236 ms
78,316 KB
testcase_25 AC 234 ms
78,632 KB
testcase_26 AC 232 ms
78,672 KB
testcase_27 AC 231 ms
78,660 KB
testcase_28 AC 193 ms
77,908 KB
testcase_29 AC 194 ms
77,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from itertools import product

def count(a,id):
    n = len(a)
    #配列は末から
    dp=[[[0] * 20 for _ in range(2)] for _ in range(n+1)]
    dp[0][0][0] = 1

    #条件に合わせてDP
    for i, less, cntd in product(range(n), range(2), range(19)):
        max_d = 9 if less else int(a[i])
        for d in range(max_d+1):
            less_ = less or d < max_d
            cntd_ = cntd + (d==id)
            dp[i + 1][less_][cntd_] += dp[i][less][cntd]

    #合致するものを合算
    ret = 0
    for less, cntd in product((0,1), range(20)):
            ret += dp[n][less][cntd]*cntd
    return ret

def countZero(a):
    n = len(a)
    #配列は末から
    dp=[[[[0] * 20 for _ in range(2)] for _ in range(2)] for _ in range(n+1)]
    dp[0][0][0][0] = 1

    #条件に合わせてDP
    for i, less, leading0, cntZero in product(range(n), range(2), range(2), range(19)):
        max_d = 9 if less else int(a[i])
        for d in range(max_d+1):
            less_ = less or d < max_d
            leading0_ = leading0 or d!=0
            if leading0:
                cntZero_ = cntZero + (d==0)
            else:
                cntZero_ = 0
            dp[i + 1][less_][leading0_][cntZero_] += dp[i][less][leading0][cntZero]

    #合致するものを合算
    ret = 0
    for less, cntZero_ in product((0,1), range(20)):
            ret += dp[n][less][1][cntZero_]*cntZero_
    return ret

def getK(N):
    N = str(N)
    ret = 0
    for d in [4,6,8,8,9]:
        ret += count(N,d)
    ret += countZero(N)
    return ret

#めぐる式二分探索
def is_ok(v):
    return getK(v) >= K

def meguru_bisect(ng, ok):
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid
    return ok

K = int(input())
N = meguru_bisect(0,10**18)
if K!=getK(N):
    print(-1)
else:
    print(N)
0