Digprime.py — различия между версиями
Материал из DISCOPAL
					
										
					
					StasFomin (обсуждение | вклад)  | 
				StasFomin (обсуждение | вклад)   | 
				||
| Строка 1: | Строка 1: | ||
<source lang="python">  | <source lang="python">  | ||
| − | |||
import sys  | import sys  | ||
| + | digits = 19  | ||
| + | # globals  | ||
input_str = ""  | input_str = ""  | ||
dp = [[[-1, -1], [-1, -1]] for i in range(25)]  | dp = [[[-1, -1], [-1, -1]] for i in range(25)]  | ||
| Строка 16: | Строка 17: | ||
next(tc_)  | next(tc_)  | ||
| − | |||
p10 = [10**i for i in range(digits)]  | p10 = [10**i for i in range(digits)]  | ||
| Строка 49: | Строка 49: | ||
for input_str in tc_:  | for input_str in tc_:  | ||
| − |      for j in range(  | + |      for j in range(digits):  | 
         dp[j][0][0] = -1  |          dp[j][0][0] = -1  | ||
         dp[j][1][0] = -1  |          dp[j][1][0] = -1  | ||
Версия 01:44, 1 мая 2022
import sys digits = 19 # globals 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_) p10 = [10**i for i in range(digits)] def calculate(i, taken: int, ls: int) -> int: ''' i — индекс цифры taken — встречали ли простые цифры раньше ls — в текущем индексе можно делать полный перебор ''' global input_str global dp if i == len(input_str): return taken if dp[i][taken][ls] != -1: return int(dp[i][taken][ls]) if ls and taken: return p10[len(input_str) - i] 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(digits): 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))