Digprime.py

Материал из DISCOPAL
Перейти к: навигация, поиск
# globals
import sys
 
input_str = ""
dp = [[[-1, -1], [-1, -1]] for i in range(25)]
 
 
def get_input():
    lines = sys.stdin.readlines()
    content = " ".join(lines).strip()
    for token in content.split():
        yield token
 
tc_ = get_input()
next(tc_)
 
 
 
def calculate(i, taken: int, ls: int) -> int:
    global input_str
    global dp
    if i == len(input_str):
        return taken
    if dp[i][taken][ls] != -1:
        return int(dp[i][taken][ls])
    res = 0
    if ls:
        limit = 9
    else:
        limit = int(input_str[i])
    for x in range(limit + 1):
        res += calculate(i + 1, taken | (x == 2) | (x == 3) | (x == 5) | (x == 7), ls | (x != limit))
 
    dp[i][taken][ls] = int(res)
    return int(res)
 
 
for input_str in tc_:
    for j in range(25):
        dp[j][0][0] = -1
        dp[j][1][0] = -1
        dp[j][1][1] = -1
        dp[j][0][1] = -1
    print(calculate(0, 0, 0))

[ Хронологический вид ]Комментарии

(нет элементов)

Войдите, чтобы комментировать.