Digprime.py — различия между версиями

Материал из DISCOPAL
Перейти к: навигация, поиск
(Новая страница: «<source lang="python"> # globals input_str = "" dp = [[[-1, -1], [-1, -1]] for i in range(25)] def calculate(i, taken: int, ls: int) -> int: global input_…»)
 
Строка 1: Строка 1:
 
<source lang="python">
 
<source lang="python">
 
# globals
 
# globals
 +
import sys
 +
 
input_str = ""
 
input_str = ""
 
dp = [[[-1, -1], [-1, -1]] for i in range(25)]
 
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:
 
def calculate(i, taken: int, ls: int) -> int:
Строка 24: Строка 37:
 
   
 
   
 
   
 
   
test_count = int(input())
+
for input_str in tc_:
for i in range(test_count):
+
    input_str = input()
+
 
     for j in range(25):
 
     for j in range(25):
 
         dp[j][0][0] = -1
 
         dp[j][0][0] = -1

Версия 00:21, 1 мая 2022

# 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))