AtCoder Beginner Contest 007

Submission #4240923

Source codeソースコード

def solve(s):
    """
    桁DP
    dp[keta][isBanned][isEqual]
    """
    length = len(s)
    dp = [[[0] * 2 for _ in range(2)] for _ in range(length + 1)]
    dp[-1][0][1] = 1
    for i in range(length):
        num = int(s[i])
        if num <= 4:
            dp[i][0][0] = dp[i - 1][0][0] * 8 + dp[i - 1][0][1] * num
            dp[i][0][1] = dp[i - 1][0][1]
            dp[i][1][0] = dp[i - 1][0][0] * 2 + dp[i - 1][1][0] * 10 + dp[i - 1][1][1] * num
            dp[i][1][1] = dp[i - 1][1][1]
            if num == 4:
                dp[i][0][1] = 0
                dp[i][1][1] = 1
            else:
                dp[i][0][1] = dp[i - 1][0][1]
                dp[i][1][1] = dp[i - 1][1][1]
        elif num <= 9:
            dp[i][0][0] = dp[i - 1][0][0] * 8 + dp[i - 1][0][1] * (num - 1)
            dp[i][1][0] = dp[i - 1][0][0] * 2 + dp[i - 1][0][1] + dp[i - 1][1][0] * 10 + dp[i - 1][1][1] * num
            if num == 9:
                dp[i][0][1] = 0
                dp[i][1][1] = 1
            else:
                dp[i][0][1] = dp[i - 1][0][1]
                dp[i][1][1] = dp[i - 1][1][1]
    return dp[length - 1][1][0] + dp[length - 1][1][1]

def solve2(s):
    """
    solveの次元を落とす
    dp[isBanned][isEqual]
    """
    length = len(s)
    dp = [[0] * 2 for _ in range(2)]
    dp[0][1] = 1
    for c in s:
        num = int(c)
        if num <= 4:
            dp[1][0] = dp[0][0] * 2 + dp[1][0] * 10 + dp[1][1] * num
            dp[0][0] = dp[0][0] * 8 + dp[0][1] * num
            if num == 4:
                dp[0][1] = 0
                dp[1][1] = 1
        else:
            dp[1][0] = dp[0][0] * 2 + dp[0][1] + dp[1][0] * 10 + dp[1][1] * num
            dp[0][0] = dp[0][0] * 8 + dp[0][1] * (num - 1)
            if num == 9:
                dp[0][1] = 0
                dp[1][1] = 1
    return dp[1][0] + dp[1][1]

a, b = input().split()
#print(solve(b) - solve(str(int(a) - 1)))
print(solve2(b) - solve2(str(int(a) - 1)))

Submission

Task問題 D - 禁止された数字
User nameユーザ名 orca
Created time投稿日時
Language言語 Python3 (3.4.3)
Status状態 AC
Score得点 100
Source lengthソースコード長 2021 Byte
File nameファイル名
Exec time実行時間 18 ms
Memory usageメモリ使用量 3268 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - subtask0_sample01.txt,subtask0_sample02.txt,subtask0_sample03.txt,subtask0_sample04.txt
Subtask1 30 / 30 subtask1_01.txt,subtask1_02.txt,subtask1_03.txt,subtask1_04.txt,subtask1_05.txt,subtask1_06.txt,subtask1_07.txt,subtask1_08.txt,subtask1_09.txt,subtask1_10.txt,subtask1_11.txt,subtask1_12.txt,subtask1_13.txt,subtask0_sample01.txt,subtask0_sample02.txt,subtask0_sample03.txt
Subtask2 70 / 70 subtask0_sample01.txt,subtask0_sample02.txt,subtask0_sample03.txt,subtask0_sample04.txt,subtask1_01.txt,subtask1_02.txt,subtask1_03.txt,subtask1_04.txt,subtask1_05.txt,subtask1_06.txt,subtask1_07.txt,subtask1_08.txt,subtask1_09.txt,subtask1_10.txt,subtask1_11.txt,subtask1_12.txt,subtask1_13.txt,subtask2_01.txt,subtask2_02.txt,subtask2_03.txt,subtask2_04.txt,subtask2_05.txt,subtask2_06.txt,subtask2_07.txt,subtask2_08.txt,subtask2_09.txt,subtask2_10.txt,subtask2_11.txt,subtask2_12.txt,subtask2_13.txt,subtask2_14.txt,subtask2_15.txt,subtask2_16.txt,subtask2_17.txt,subtask2_18.txt,subtask2_19.txt,subtask2_20.txt,subtask2_21.txt,subtask2_22.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
subtask0_sample01.txt AC 17 ms 3268 KB
subtask0_sample02.txt AC 17 ms 3268 KB
subtask0_sample03.txt AC 17 ms 3268 KB
subtask0_sample04.txt AC 17 ms 3268 KB
subtask1_01.txt AC 17 ms 3268 KB
subtask1_02.txt AC 17 ms 3268 KB
subtask1_03.txt AC 18 ms 3268 KB
subtask1_04.txt AC 18 ms 3268 KB
subtask1_05.txt AC 17 ms 3268 KB
subtask1_06.txt AC 18 ms 3268 KB
subtask1_07.txt AC 17 ms 3268 KB
subtask1_08.txt AC 17 ms 3268 KB
subtask1_09.txt AC 17 ms 3268 KB
subtask1_10.txt AC 17 ms 3268 KB
subtask1_11.txt AC 18 ms 3268 KB
subtask1_12.txt AC 18 ms 3268 KB
subtask1_13.txt AC 17 ms 3268 KB
subtask2_01.txt AC 17 ms 3268 KB
subtask2_02.txt AC 17 ms 3268 KB
subtask2_03.txt AC 17 ms 3268 KB
subtask2_04.txt AC 17 ms 3268 KB
subtask2_05.txt AC 17 ms 3268 KB
subtask2_06.txt AC 17 ms 3268 KB
subtask2_07.txt AC 17 ms 3268 KB
subtask2_08.txt AC 17 ms 3268 KB
subtask2_09.txt AC 17 ms 3268 KB
subtask2_10.txt AC 17 ms 3268 KB
subtask2_11.txt AC 17 ms 3268 KB
subtask2_12.txt AC 17 ms 3268 KB
subtask2_13.txt AC 17 ms 3268 KB
subtask2_14.txt AC 17 ms 3268 KB
subtask2_15.txt AC 17 ms 3268 KB
subtask2_16.txt AC 17 ms 3268 KB
subtask2_17.txt AC 18 ms 3268 KB
subtask2_18.txt AC 17 ms 3268 KB
subtask2_19.txt AC 17 ms 3268 KB
subtask2_20.txt AC 17 ms 3268 KB
subtask2_21.txt AC 17 ms 3268 KB
subtask2_22.txt AC 17 ms 3268 KB