結果

問題 No.3316 Make 81181819 with only 0,1,or 8
コンテスト
ユーザー titia
提出日時 2025-10-31 23:12:30
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,419 bytes
コンパイル時間 321 ms
コンパイル使用メモリ 82,644 KB
実行使用メモリ 69,928 KB
最終ジャッジ日時 2025-10-31 23:12:39
合計ジャッジ時間 8,538 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other TLE * 1 -- * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from functools import lru_cache

Q=[1,8]
MAX=81181819

SET=set()

while Q:
    x=Q.pop()
    if x<=81181819 and x not in SET:
        SET.add(x)

        for i in [0,1,8]:
            y=x*10+i

            Q.append(y)

LIST=sorted(SET,reverse=True)
@lru_cache(maxsize=None)
def calc(x):
    if x==0:
        return 0,[]

    ANS=1<<60
    FROM=[]

    for s in LIST:
        if s<=x:
            k,L=calc(x-s)
            if k>=8:
                continue
            if k>=ANS-1:
                continue
                

            if ANS>k+1:
                ANS=k+1
                FROM=L+[s]


    return ANS,FROM
            
#for x in range(1000):
#    print(x,calc(x))

T=int(input())

for tests in range(T):
    x=int(input())
    a=MAX-x
    Q=[(a,[])]
    USE={a}
    flag0=0

    while flag0==0:
        #print(Q)
        NQ=[]

        while Q:
            if len(NQ)>=100:
                break
            x,L=Q.pop()
            for s in LIST:
                if s>x:
                    continue
                if s==x:
                    flag0=1
                    ANS=L+[s]
                    break
                if x-s not in USE:
                    NQ.append((x-s,L+[s]))
            if flag0==1:
                break
        Q=NQ
        if flag0==1:
            break
                
            

    #print(ANS)

    print(len(ANS))
    for ans in ANS:
        print(ans)
0