Submission #3728134


Source Code Expand

from functools import lru_cache

A, K = input().split()
K = int(K)

@lru_cache(maxsize=1000)
def digit_search(k, tight, number_s, X):
    if k == len(X):
        diff = abs(int(number_s) -int(A))
        return diff

    digit = int(X[k])
    lim = digit if tight else 9
    res = int(A)
    number_without_0 = number_s
    while True:
        if len(number_without_0)==0 or number_without_0[0]!="0":
            break
        else:
            number_without_0 = number_without_0[1:]
    used = set(number_without_0)
    used_n = len(used)
    if used_n < K:
        res = min(res, digit_search(k+1, tight and digit==X[k],\
                         number_s+str(X[k]), X))
        if X[k]!="0": res = min(res, digit_search(k+1, tight and digit==str(int(X[k])-1),\
                         number_s+str(int(X[k])-1), X))
        if X[k]!="9": res = min(res, digit_search(k+1, tight and digit==str(int(X[k])+1),\
                         number_s+str(int(X[k])+1), X))
    elif used_n == K:
        done = False
        if X[k] in used:
            res = min(res, digit_search(k+1, tight and digit==X[k],\
                         number_s+str(X[k]), X))
            done = True
        if X[k]!="0" and str(int(X[k])-1) in used:
            res = min(res, digit_search(k+1, tight and digit==str(int(X[k])-1),\
                             number_s+str(int(X[k])-1), X))
            done = True
        if X[k]!="9" and str(int(X[k])+1) in used:
            res = min(res, digit_search(k+1, tight and digit==str(int(X[k])+1),\
                             number_s+str(int(X[k])+1), X))
            done = True

        if not done:
            for i in used:
                res = min(res, digit_search(k+1, tight and digit==int(i),\
                                            number_s+str(i), X))
    else:
        pass


    return res

print(digit_search(0, True, "", A))

Submission Info

Submission Time
Task D - 壊れた電卓
User shirosha2
Language Python (3.4.3)
Score 0
Code Size 1931 Byte
Status WA
Exec Time 2104 ms
Memory 4196 KB

Judge Result

Set Name sub All
Score / Max Score 0 / 30 0 / 70
Status
AC × 30
WA × 2
AC × 45
WA × 6
TLE × 6
Set Name Test Cases
sub test_01A.txt, test_02A.txt, test_04A.txt, test_05A.txt, test_07A.txt, test_09A.txt, test_10A.txt, test_11A.txt, test_12A.txt, test_13A.txt, test_15A.txt, test_17A.txt, test_18A.txt, test_19A.txt, test_21A.txt, test_22A.txt, test_23A.txt, test_25A.txt, test_27A.txt, test_28A.txt, test_29A.txt, test_31A.txt, test_33A.txt, test_34A.txt, test_35A.txt, test_37A.txt, test_38A.txt, test_40A.txt, test_42A.txt, test_44A.txt, test_46A.txt, test_48A.txt
All sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, test_01A.txt, test_02A.txt, test_03.txt, test_04A.txt, test_05A.txt, test_06.txt, test_07A.txt, test_08.txt, test_09A.txt, test_10A.txt, test_11A.txt, test_12A.txt, test_13A.txt, test_14.txt, test_15A.txt, test_16.txt, test_17A.txt, test_18A.txt, test_19A.txt, test_20.txt, test_21A.txt, test_22A.txt, test_23A.txt, test_24.txt, test_25A.txt, test_26.txt, test_27A.txt, test_28A.txt, test_29A.txt, test_30.txt, test_31A.txt, test_32.txt, test_33A.txt, test_34A.txt, test_35A.txt, test_36.txt, test_37A.txt, test_38A.txt, test_39.txt, test_40A.txt, test_41.txt, test_42A.txt, test_43.txt, test_44A.txt, test_45.txt, test_46A.txt, test_47.txt, test_48A.txt, test_49.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
Case Name Status Exec Time Memory
sample_01.txt AC 23 ms 3572 KB
sample_02.txt AC 23 ms 3572 KB
sample_03.txt AC 39 ms 4068 KB
sample_04.txt AC 24 ms 3572 KB
test_01A.txt AC 23 ms 3572 KB
test_02A.txt WA 23 ms 3572 KB
test_03.txt WA 24 ms 3684 KB
test_04A.txt AC 23 ms 3572 KB
test_05A.txt AC 24 ms 3572 KB
test_06.txt AC 1400 ms 4196 KB
test_07A.txt AC 23 ms 3572 KB
test_08.txt AC 23 ms 3572 KB
test_09A.txt AC 23 ms 3572 KB
test_10A.txt AC 23 ms 3572 KB
test_11A.txt WA 23 ms 3572 KB
test_12A.txt AC 23 ms 3572 KB
test_13A.txt AC 24 ms 3572 KB
test_14.txt TLE 2104 ms 4196 KB
test_15A.txt AC 23 ms 3572 KB
test_16.txt AC 46 ms 4068 KB
test_17A.txt AC 24 ms 3572 KB
test_18A.txt AC 25 ms 3572 KB
test_19A.txt AC 26 ms 3684 KB
test_20.txt AC 49 ms 4068 KB
test_21A.txt AC 24 ms 3572 KB
test_22A.txt AC 23 ms 3572 KB
test_23A.txt AC 23 ms 3572 KB
test_24.txt WA 26 ms 3684 KB
test_25A.txt AC 23 ms 3572 KB
test_26.txt TLE 2104 ms 4068 KB
test_27A.txt AC 24 ms 3572 KB
test_28A.txt AC 23 ms 3572 KB
test_29A.txt AC 23 ms 3572 KB
test_30.txt WA 57 ms 4196 KB
test_31A.txt AC 23 ms 3572 KB
test_32.txt TLE 2104 ms 4068 KB
test_33A.txt AC 23 ms 3572 KB
test_34A.txt AC 25 ms 3684 KB
test_35A.txt AC 25 ms 3572 KB
test_36.txt WA 35 ms 4068 KB
test_37A.txt AC 23 ms 3572 KB
test_38A.txt AC 24 ms 3572 KB
test_39.txt AC 977 ms 4196 KB
test_40A.txt AC 26 ms 3684 KB
test_41.txt TLE 2104 ms 4196 KB
test_42A.txt AC 25 ms 3684 KB
test_43.txt TLE 2104 ms 4196 KB
test_44A.txt AC 23 ms 3572 KB
test_45.txt AC 23 ms 3572 KB
test_46A.txt AC 24 ms 3572 KB
test_47.txt AC 496 ms 4196 KB
test_48A.txt AC 24 ms 3572 KB
test_49.txt TLE 2104 ms 4196 KB